text-table
table->string
7.7

text-table

orseau

A simple package to display utf-8 textual tables.

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

Two examples:

#lang racket
(require text-table)
 
;; Minimalistic example:
(displayln
 (table->string
  '((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
(displayln
 (table->string
  '((a b c d e f gggg h)
    (123 456 77 54 1  5646547987 41 1)
    (111 22 3333 44 5 6 7 8888))
  #:border-style 'double
  #:framed? #f
  #:row-sep? #t
  #:align '(left center center center center center center right)))

This outputs:

┌───┬───┬────┬──┬─┬──────────┬────┬────┐

│a  │b  │c   │d │e│f         │gggg│h   

├───┼───┼────┼──┼─┼──────────┼────┼────┤

│123│456│77  │54│1│5646547987│41  │1   

├───┼───┼────┼──┼─┼──────────┼────┼────┤

│111│22 │3333│44│5│6         │7   │8888│

└───┴───┴────┴──┴─┴──────────┴────┴────┘

a  ║ b ║ c  ║d ║e║    f     ║gggg║   h

═══╬═══╬════╬══╬═╬══════════╬════╬════

123║456║ 77 ║54║1║5646547987║ 41 ║   1

═══╬═══╬════╬══╬═╬══════════╬════╬════

111║22 ║3333║44║5║    6     ║ 7  ║8888

procedure

(table->string table    
  [#:->string to-string    
  #:border-style border-style    
  #:framed? framed?    
  #:row-sep? row-sep?    
  #:align align])  string?
  table : (listof list?)
  to-string : procedure? = ~a
  border-style : (or/c 'single 'space 'space-single 'rounded 'double)
   = 'single
  framed? : boolean? = #t
  row-sep? : boolean? = #t
  align : 
(or/c (listof (or/c 'left 'center 'right))
      (or/c 'left 'center 'right))
 = 'left
Accepts a table specified as a list of lists, and returns a string representing the table. The lists must all be of the same length.

The to-string procedure is used to convert cell values to strings.

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.

When row-sep? is false, no separators are drawn between the rows.

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.