From 6d9c5b8d317a3c06c33f050f0ed16fa012358eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Wed, 22 Mar 2023 15:52:46 +0100 Subject: [PATCH] Columns normalization. --- table.scm | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/table.scm b/table.scm index d384da9..91eea23 100644 --- a/table.scm +++ b/table.scm @@ -110,8 +110,32 @@ rcws)) rcws))))) + ;; Pads all lines of this cell to required width + (define (table-normalize-cell c w) + (let loop ((c c) + (r '())) + (if (null? c) + (reverse r) + (loop (cdr c) + (cons (let* ((cs (car c)) + (csl (string-length cs))) + (if (< csl w) + (string-append cs (make-string (- w csl) #\space)) + cs)) + r))))) + + ;; Returns a row (list) of cells (list of strings) with all strings + ;; padded to given column width. (define (table-row-normalize-cells row cwidths) - row) + (let loop ((cwidths cwidths) + (cells row) + (res '())) + (if (null? cells) + (reverse res) + (loop (cdr cwidths) + (cdr cells) + (cons (table-normalize-cell (car cells) (car cwidths)) + res))))) ;; Normalizes cells in all rows to match the widths of the wides cell ;; in each column. @@ -123,10 +147,11 @@ ;; Ensures the table is rectangular and each cell is a list of strings. (define (table-prepare tbl) - (table-normalize-rows - (table-prepare-cells - (table-stringify - (table-rectangularize tbl))))) + (table-normalize-columns + (table-normalize-rows + (table-prepare-cells + (table-stringify + (table-rectangularize tbl)))))) (define (table->string tbl . args) "") @@ -160,6 +185,20 @@ (table-column-widths '((("x") ("y") ("zz") ("")) (("a") ("bcde") ("") ("")) (("123") ("2") ("3") ("4")))) '(3 4 2 1)) + (test-equal? table-normalize-cell + (table-normalize-cell '("a" "bb" "ccc" "") 4) + '("a " "bb " "ccc " " ")) + (test-equal? table-row-normalize-cells + (table-row-normalize-cells + '(("a") ("bb") ("ccc") ("")) + '(1 2 3 4)) + '(("a") ("bb") ("ccc") (" "))) + (test-equal? table-normalize-columns + (table-normalize-columns + '((("a") ("bb") ("ccc") ("")) + (("") ("b") ("z") ("x")))) + '((("a") ("bb") ("ccc") (" ")) + ((" ") ("b ") ("z ") ("x")))) )) )