]> gitweb.factorcode.org Git - factor.git/commitdiff
strings.tables: adding box formatting
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 26 May 2022 21:06:28 +0000 (14:06 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 26 May 2022 21:06:45 +0000 (14:06 -0700)
basis/strings/tables/tables.factor

index 2acf11c2cae9896402b8fecfb0ff6c95f9fd18a9..375bc0d6ff01575860bb9ff8e4d56fd8f0fd077f 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2009, 2010 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel sequences splitting ;
+USING: io kernel make math sequences splitting ;
 IN: strings.tables
 
 <PRIVATE
@@ -11,11 +11,30 @@ IN: strings.tables
 : format-column ( seq -- seq )
     dup longest length '[ _ CHAR: \s pad-tail ] map! ;
 
-PRIVATE>
-
-: format-table ( table -- seq )
+: format-cells ( seq -- seq )
     [ [ split-lines ] map format-row flip ] map concat flip
     [ { } ] [
         [ but-last-slice [ format-column ] map! drop ] keep
-        flip [ join-words ] map!
     ] if-empty ;
+
+PRIVATE>
+
+: format-table ( table -- seq )
+    format-cells flip [ join-words ] map! ;
+
+: format-table. ( table -- )
+    format-table [ print ] each ;
+
+: format-box ( table -- seq )
+    format-cells [ { } ] [
+        dup length 1 - over [ format-column ] change-nth flip [
+            [ [ " │ " join "│ " " │" surround ] map ]
+            [ first [ length CHAR: ─ <repetition> ] map ] bi
+            [ "─┬─" join "┌─" "─┐" surround , ]
+            [ "─┼─" join "├─" "─┤" surround '[ _ , ] [ , ] interleave ]
+            [ "─┴─" join "└─" "─┘" surround , ] tri
+        ] { } make
+    ] if-empty ;
+
+: format-box. ( table -- )
+    format-box [ print ] each ;