]> gitweb.factorcode.org Git - factor.git/blob - basis/strings/tables/tables.factor
Switch to https urls
[factor.git] / basis / strings / tables / tables.factor
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 ;
4 IN: strings.tables
5
6 <PRIVATE
7
8 : format-row ( seq -- seq )
9     dup longest length '[ _ "" pad-tail ] map! ;
10
11 : format-column ( seq -- seq )
12     dup longest length '[ _ CHAR: \s pad-tail ] map! ;
13
14 : format-cells ( seq -- seq )
15     [ [ split-lines ] map format-row flip ] map concat flip
16     [ { } ] [
17         [ but-last-slice [ format-column ] map! drop ] keep
18     ] if-empty ;
19
20 PRIVATE>
21
22 : format-table ( table -- seq )
23     format-cells flip [ join-words ] map! ;
24
25 : format-table. ( table -- )
26     format-table [ print ] each ;
27
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
36         ] { } make
37     ] if-empty ;
38
39 : format-box. ( table -- )
40     format-box [ print ] each ;