Laurent Orseau
License: APACHE2+MIT
To install:
raco pkg install text-table
See the example in the main submodule of the "main.rkt" file. You can observe the results by running:
racket -l text-table
;Minimalistic example:┌───┬───┬────┬──┬─┬──────────┬────┬────┐
│a│b│c │d │e│f │gggg│h │
├───┼───┼────┼──┼─┼──────────┼────┼────┤
│123│456│77│54│1│5646547987│41│1 │
├───┼───┼────┼──┼─┼──────────┼────┼────┤
│111│22 │3333│44│5│6 │7 │8888│
└───┴───┴────┴──┴─┴──────────┴────┴────┘
;With more bells and whistles'((abcdefggggh)(123456775415646547987411)(111223333445678888))#:border-style'double#:framed?#f#:row-sep?#t#:align'(leftcenterright))a║ b ║ c║ d║e║ f║gggg║ h
═══╬═══╬════╬══╬═╬══════════╬════╬════
123║456║77║54║1║5646547987║41║ 1
═══╬═══╬════╬══╬═╬══════════╬════╬════
111║22 ║3333║44║5║ 6║ 7║8888
;Custom border style using border-style-frame/c(abcdefababcdef)(aabcdefabc))#:border-style'("╭─┬╮""│.││""├─┼┤""╰─┴╯")#:align'(center)#:framed?#t#:row-sep?#t)╭──────┬──────┬──────╮
│.abc..│.abc..│.abc..│
├──────┼──────┼──────┤
│abcdef│..ab..│abcdef│
├──────┼──────┼──────┤
│..a...│abcdef│.abc..│
╰──────┴──────┴──────╯
;Custom border style using border-style2/c(abcdefababcdef)(aabcdefabc))#:border-style'(("<table>""""""")("<tr><td> "" "" </td><td> "" </td></tr>")("""""""")("</table>"""""""))#:framed?#t#:row-sep?#f)<table>
<tr><td> abc</td><td> abc</td><td> abc</td></tr>
<tr><td> abcdef </td><td> ab </td><td> abcdef </td></tr>
<tr><td> a</td><td> abcdef </td><td> abc</td></tr>
</table>
;LaTeX style\begin{tabular}{|l|l|l|}
\hline
abc& abc& abc\\
\hline
abcdef & ab & abcdef \\
\hline
a& abcdef & abc\\
\hline
\end{tabular}
;Aligning numbers (incorrectly then well)#:row-sep?'(#t#f...)#:col-sep?'(#t#f...)#:align'(leftright...center)#:->string(list;The table:`((NameSpeed...Unit)(Alice10..."km/h")(Charlie+inf.0+nan.0......n/a)┌───────┬─────────────────────────────────────────────────────────────────┐
│Name │ SpeedSpeed SpeedSpeed Unit│
├───────┼─────────────────────────────────────────────────────────────────┤
│Alice│10 10 10.001e+01 km/h│
│Bob│1.4142135623730951 1.4142141.41 1.414214e+00 m/s │
│Charlie│+inf.0 +nan.0+inf.0 +nan.0 n/a │
│light│299792458000 299792458000 299792458000.00 2.997925e+11 mm/s│
└───────┴─────────────────────────────────────────────────────────────────┘
;Empty style and doubly repeating alignments#:border-style'empty#:align'(rightleft......)(make-list10'**)(make-list10'***)(make-list10'****)(make-list10'*****)(make-list10"|")))**********
********************
******************************
****************************************
**************************************************
||||||||||
;Multiple separators#:row-sep?'(#t#f......)#:col-sep?'(#t#f......))┌─┬─────┬─────┬─────┬─────┬──┐
│1│23 │45 │67 │89 │10│
├─┼─────┼─────┼─────┼─────┼──┤
│2│46 │810│12 14│16 18│20│
│3│69 │12 15│18 21│24 27│30│
├─┼─────┼─────┼─────┼─────┼──┤
│4│812│16 20│24 28│32 36│40│
│5│10 15│20 25│30 35│40 45│50│
├─┼─────┼─────┼─────┼─────┼──┤
│6│12 18│24 30│36 42│48 54│60│
└─┴─────┴─────┴─────┴─────┴──┘
procedure
[ #:->stringto-string#:border-styleborder-style#:framed?framed?#:row-sep?row-sep?#:col-sep?col-sep?#:alignalign= ~a= 'top
The to-string procedure is used to convert cell values to strings, or a pattern-list of such procedures. Note that strings are not converted.
The border-style specifies the style of lines to be used in drawing the table.
When framed? is #true, a frame is drawn around the outside of the table.
The row-sep? and col-sep? arguments specify whether separators are added between rows or columns.
The align specification indicates how the contents of the cells are to be aligned within their cells. A single-symbol specification applies to all cells, or a list of symbols of the same length as the rows can be applied in order to specify the alignment of each column independently. When align is a list, it is trimmed to the length of the columns if it is too long, or the last element of the list is used for the remaining columns if it is too short.
The row-align specification indicates how the contents of the cells are aligned in a row, when cells are strings with multiple lines.
The to-string, align and row-align, row-sep? and col-sep? arguments accept pattern lists.
procedure
[ #:->stringto-string#:border-styleborder-style#:framed?framed?#:row-sep?row-sep?#:col-sep?col-sep?#:alignalign= ~a= 'top
#:align'(leftright)'((abcdefggggh)(123456775415646547987411)(111223333445678888))))a bcd ef ggggh
123 456 77 54 1 5646547987 411
11122 3333 44 567 8888
procedure
[ #:->stringto-string#:border-styleborder-style#:framed?framed?#:row-sep?row-sep?#:col-sep?col-sep?#:alignalign= ~a= 'top
procedure
[ #:->stringto-string#:border-styleborder-style#:framed?framed?#:row-sep?row-sep?#:col-sep?col-sep?#:alignalign= ~a= 'top
value
=(or/c'empty'latex'space'space-single'single'rounded'double'heavy
value
value
'((______)(____)(_"_\n__"__))#:border-style'(("╭""^""┬""╮")("{"".""│""}")("├""─""+""┤")("╰""v""┴""╯")))╭^^┬^^┬^^^^┬^╮
{_.│_.│____│_}
├──+──+────+─┤
{_.│_.│_...│_}
├──+──+────+─┤
{__│_.│_...│_}
{..│__│....│.}
╰vv┴vv┴vvvv┴v╯
value
=
(abcdefababcdef)(aabcdefabc))#:border-style'("╭─┬╮""│.││""├─┼┤""╰─┴╯")#:align'(center))╭──────┬──────┬──────╮
│.abc..│.abc..│.abc..│
├──────┼──────┼──────┤
│abcdef│..ab..│abcdef│
├──────┼──────┼──────┤
│..a...│abcdef│.abc..│
╰──────┴──────┴──────╯
procedure
(( string-length=/c n)x)→boolean?
n:integer?x:any/c
procedure
(( pattern-list-of pred?)x)→boolean?
x:any/c
procedure
[ #:truncate-ok?truncate-ok?]result-length:exact-nonnegative-integer?
'(aaa)
'(aaa)
'(abbbb)
'(abbbb)
'(abcbcbcbcb)
'(abcdbcdbef)
Minimum length of list l exceeds n-elt '(a b c d ... ... ...
e f) 2
'(ae)
'((a1)(b2)(c3))
procedure
( group-by-lengths llengths)→(listof list? )
l:list?
'(102301))'((a)()(bc)(def)()(g))
procedure
( string-repeat strlen)→string?
str:string?
"abcab"
"ab"
procedure
#:basebase#:precisionprecision#:notationnotation#:format-exponentformat-exponent#:min-widthmin-width#:pad-stringpad-string#:groupsgroups#:group-sepgroup-sep#:decimal-sepdecimal-sep])sign := #f= 10= 'positional= #f
┌──────────────────┬─────────────┬────────────┬────────────────┬────────┬─────────────┐
│ 1│2│ 3│4 (good)│5 (good)│6│
│header│ header│header│header│header│ header....│
├──────────────────┼─────────────┼────────────┼────────────────┼────────┼─────────────┤
│ 1111.11│1111.11│ 1.11111e+03│ 1111.11│1.11e+03│ ...1111.11│
│22.222│ 22.222│2.2222e+01│ 22.22│2.22e+01│ ....22.222│
│ 3333000.0│3333000│ 3.333e+06│3333000.00│3.33e+06│ ...3333000│
│ 4440000000000.0│4440000000000│4.44e+12│4440000000000.00│4.44e+12│4440000000000│
│1.4142135623730951│ 1.414214│1.414214e+00│1.41│1.41e+00│ ..1.414214│
└──────────────────┴─────────────┴────────────┴────────────────┴────────┴─────────────┘