Implement row delimiters.

This commit is contained in:
Dominik Pantůček 2023-03-22 19:10:38 +01:00
parent e01a6db5a6
commit 64ff4255e3
4 changed files with 58 additions and 3 deletions

View file

@ -182,7 +182,8 @@ progress.so: progress.o
progress.o: progress.import.scm progress.o: progress.import.scm
progress.import.scm: $(PROGRESS-SOURCES) 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.so: table.o
table.o: table.import.scm table.o: table.import.scm

View file

@ -453,3 +453,11 @@ any algorithms used.
Returns a list containing only elements matching given ```pred?``` Returns a list containing only elements matching given ```pred?```
predicate. 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.

View file

@ -31,6 +31,10 @@
*table-border-style* *table-border-style*
table->string table->string
table-tests! table-tests!
;;---
table-row-delimiter
table-row-delimiter/styled
table-borders-lookup
) )
(import scheme (import scheme
@ -38,8 +42,10 @@
(chicken string) (chicken string)
(chicken format) (chicken format)
(chicken keyword) (chicken keyword)
(chicken irregex)
ansi ansi
testing) testing
utils)
;; Default table border style to use if not explicitly specified. ;; Default table border style to use if not explicitly specified.
(define *table-border-style* (make-parameter 'ascii)) (define *table-border-style* (make-parameter 'ascii))
@ -175,7 +181,10 @@
;; Compiled table borders for rendering ;; Compiled table borders for rendering
(define table-borders-lookup (define table-borders-lookup
(map (lambda (src) (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)) table-borders-lookup-source))
;; Accepts a table row - list of list of strings - and returns a list ;; Accepts a table row - list of list of strings - and returns a list
@ -196,6 +205,27 @@
right-border) right-border)
res)))))) 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) (define (table->string tbl . args)
(let ((table-border (get-keyword #:table-border args (lambda () #f))) (let ((table-border (get-keyword #:table-border args (lambda () #f)))
(cell-border (get-keyword #:cell-border args (lambda () #f))) (cell-border (get-keyword #:cell-border args (lambda () #f)))
@ -254,6 +284,9 @@
(test-equal? table-row->lines (test-equal? table-row->lines
(table-row->lines '(("a ") ("bb") ("ccc") (" ")) "]" "|" "[") (table-row->lines '(("a ") ("bb") ("ccc") (" ")) "]" "|" "[")
'("]a |bb|ccc| [")) '("]a |bb|ccc| ["))
(test-equal? table-row-delimiter
(table-row-delimiter '(1 2 3 1) "/" "-" "+" "\\")
"/-+--+---+-\\")
)) ))
) )
@ -261,7 +294,9 @@
(import table) (import table)
(table-tests!) (table-tests!)
(print (table-row-delimiter '(1 2 3 1) "/" "-" "+" "\\"))
(print (print
(table->string (table->string
'(("a" "bb" "ccc" "") '(("a" "bb" "ccc" "")
("" "b" "z" "x")))) ("" "b" "z" "x"))))
(print (table-row-delimiter/styled '(1 2 3 1) (cdr (assq 'unicode table-borders-lookup)) 3))

View file

@ -29,11 +29,13 @@
utils utils
( (
filter filter
string-repeat
utils-tests! utils-tests!
) )
(import scheme (import scheme
(chicken base) (chicken base)
(chicken string)
testing) testing)
;; Returns a list with elements matching pred? predicate. ;; Returns a list with elements matching pred? predicate.
@ -48,6 +50,15 @@
(loop (cdr lst) (loop (cdr lst)
res))))) 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. ;; Performs utils module self-tests.
(define (utils-tests!) (define (utils-tests!)
(run-tests (run-tests