Implement row delimiters.
This commit is contained in:
parent
e01a6db5a6
commit
64ff4255e3
4 changed files with 58 additions and 3 deletions
3
Makefile
3
Makefile
|
@ -182,7 +182,8 @@ progress.so: progress.o
|
|||
progress.o: progress.import.scm
|
||||
progress.import.scm: $(PROGRESS-SOURCES)
|
||||
|
||||
TABLE-SOURCES=table.scm ansi.import.scm testing.import.scm
|
||||
TABLE-SOURCES=table.scm ansi.import.scm testing.import.scm \
|
||||
utils.import.scm
|
||||
|
||||
table.so: table.o
|
||||
table.o: table.import.scm
|
||||
|
|
|
@ -453,3 +453,11 @@ any algorithms used.
|
|||
|
||||
Returns a list containing only elements matching given ```pred?```
|
||||
predicate.
|
||||
|
||||
(string-repeat str rep)
|
||||
|
||||
* ```str``` - string to repeat
|
||||
* ```rep``` - number of repeats
|
||||
|
||||
Returns a string created by repeating the string ```str``` exactly
|
||||
```rep``` number of times.
|
||||
|
|
39
table.scm
39
table.scm
|
@ -31,6 +31,10 @@
|
|||
*table-border-style*
|
||||
table->string
|
||||
table-tests!
|
||||
;;---
|
||||
table-row-delimiter
|
||||
table-row-delimiter/styled
|
||||
table-borders-lookup
|
||||
)
|
||||
|
||||
(import scheme
|
||||
|
@ -38,8 +42,10 @@
|
|||
(chicken string)
|
||||
(chicken format)
|
||||
(chicken keyword)
|
||||
(chicken irregex)
|
||||
ansi
|
||||
testing)
|
||||
testing
|
||||
utils)
|
||||
|
||||
;; Default table border style to use if not explicitly specified.
|
||||
(define *table-border-style* (make-parameter 'ascii))
|
||||
|
@ -175,7 +181,10 @@
|
|||
;; Compiled table borders for rendering
|
||||
(define table-borders-lookup
|
||||
(map (lambda (src)
|
||||
(cons (car src) (string-intersperse (cdr src) "")))
|
||||
(cons (car src)
|
||||
(list->vector
|
||||
(irregex-extract (irregex "." 'u)
|
||||
(string-intersperse (cdr src) "")))))
|
||||
table-borders-lookup-source))
|
||||
|
||||
;; Accepts a table row - list of list of strings - and returns a list
|
||||
|
@ -196,6 +205,27 @@
|
|||
right-border)
|
||||
res))))))
|
||||
|
||||
;; Creates table row delimiter based on column widths.
|
||||
(define (table-row-delimiter cws left line cross right)
|
||||
(string-append
|
||||
left
|
||||
(string-intersperse
|
||||
(map
|
||||
(lambda (cw)
|
||||
(string-repeat line cw))
|
||||
cws)
|
||||
cross)
|
||||
right))
|
||||
|
||||
;; Returns table row delimiter based on column widths, extracting
|
||||
;; line style from particular row of border style vector.
|
||||
(define (table-row-delimiter/styled cws svec srow)
|
||||
(apply table-row-delimiter cws
|
||||
(map (lambda (idx)
|
||||
(vector-ref svec (+ idx (* srow 4))))
|
||||
'(0 1 2 3))))
|
||||
|
||||
;; Converts given table to a string suitable for printing.
|
||||
(define (table->string tbl . args)
|
||||
(let ((table-border (get-keyword #:table-border args (lambda () #f)))
|
||||
(cell-border (get-keyword #:cell-border args (lambda () #f)))
|
||||
|
@ -254,6 +284,9 @@
|
|||
(test-equal? table-row->lines
|
||||
(table-row->lines '(("a ") ("bb") ("ccc") (" ")) "]" "|" "[")
|
||||
'("]a |bb|ccc| ["))
|
||||
(test-equal? table-row-delimiter
|
||||
(table-row-delimiter '(1 2 3 1) "/" "-" "+" "\\")
|
||||
"/-+--+---+-\\")
|
||||
))
|
||||
|
||||
)
|
||||
|
@ -261,7 +294,9 @@
|
|||
(import table)
|
||||
(table-tests!)
|
||||
|
||||
(print (table-row-delimiter '(1 2 3 1) "/" "-" "+" "\\"))
|
||||
(print
|
||||
(table->string
|
||||
'(("a" "bb" "ccc" "")
|
||||
("" "b" "z" "x"))))
|
||||
(print (table-row-delimiter/styled '(1 2 3 1) (cdr (assq 'unicode table-borders-lookup)) 3))
|
||||
|
|
11
utils.scm
11
utils.scm
|
@ -29,11 +29,13 @@
|
|||
utils
|
||||
(
|
||||
filter
|
||||
string-repeat
|
||||
utils-tests!
|
||||
)
|
||||
|
||||
(import scheme
|
||||
(chicken base)
|
||||
(chicken string)
|
||||
testing)
|
||||
|
||||
;; Returns a list with elements matching pred? predicate.
|
||||
|
@ -48,6 +50,15 @@
|
|||
(loop (cdr lst)
|
||||
res)))))
|
||||
|
||||
;; Repeats given string.
|
||||
(define (string-repeat str rep)
|
||||
(let loop ((rep rep)
|
||||
(res '()))
|
||||
(if (> rep 0)
|
||||
(loop (sub1 rep)
|
||||
(cons str res))
|
||||
(string-intersperse res ""))))
|
||||
|
||||
;; Performs utils module self-tests.
|
||||
(define (utils-tests!)
|
||||
(run-tests
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue