From 3f50d85412ba247280f8d3d440fd34db02076fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pant=C5=AF=C4=8Dek?= Date: Tue, 28 Mar 2023 22:58:50 +0200 Subject: [PATCH] Work on cell lines alignment. --- table.scm | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/table.scm b/table.scm index 6ea52e2..cfbd559 100644 --- a/table.scm +++ b/table.scm @@ -139,24 +139,26 @@ ;; Normalizes cell line to required width and handles leading and ;; trailing tabs to allow for right and center alignment. (define (table-normalize-cell-line line w) - (let ((lst (string->list line)) - (first-char (if (null? lst) #f (car lst))) - (last-char (if (null? lst) #f (car (reverse lst)))) - ;; Line without tabs - (len (ansi-string-length line))) - (if (< csl w) - (string-append cs (string-repeat " " (- w csl))) - cs))) + (let* ((lst (string->list line)) + (first-char (if (null? lst) #f (car lst))) + (last-char (if (null? lst) #f (car (reverse lst)))) + (first-tab (eq? first-char #\tab)) + (last-tab (eq? last-char #\tab)) + (line0 (if first-tab (substring line 1) line)) + (line1 (if last-tab (substring line0 0 (sub1 (string-length line0))) line0)) + (len (ansi-string-length line1))) + (if (< len w) + (let* ((miss (- w len)) + (do-left-pad first-tab) + (do-right-pad (or (not first-tab) last-tab))) + (string-append line1 (string-repeat " " miss))) + line1))) ;; 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 (table-normalize-cell-line (car c) w) - r))))) + (map (lambda (line) + (table-normalize-cell-line line w)) + c)) ;; Returns a row (list) of cells (list of strings) with all strings ;; padded to given column width.