1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: io kernel make math sequences splitting ;
8 : format-row ( seq -- seq )
9 dup longest length '[ _ "" pad-tail ] map! ;
11 : format-column ( seq -- seq )
12 dup longest length '[ _ CHAR: \s pad-tail ] map! ;
14 : format-cells ( seq -- seq )
15 [ [ split-lines ] map format-row flip ] map concat flip
17 [ but-last-slice [ format-column ] map! drop ] keep
22 : format-table ( table -- seq )
23 format-cells flip [ join-words ] map! ;
25 : format-table. ( table -- )
26 format-table [ print ] each ;
28 : format-box ( table -- seq )
29 format-cells [ { } ] [
30 dup length 1 - over [ format-column ] change-nth flip [
31 [ [ " │ " join "│ " " │" surround ] map ]
32 [ first [ length CHAR: ─ <repetition> ] map ] bi
33 [ "─┬─" join "┌─" "─┐" surround , ]
34 [ "─┼─" join "├─" "─┤" surround '[ _ , ] [ , ] interleave ]
35 [ "─┴─" join "└─" "─┘" surround , ] tri
39 : format-box. ( table -- )
40 format-box [ print ] each ;