On this page:
4.1 Table Rendering
table-row->sgr-lines
table->sgr-lines
print-table
table->string
4.2 Table Borders
horizontal-cell-pair-has-bar?
horizontal-cell-pair->bar
table-row->column-bars
table->column-bars
table-row->cell-bars
vertical-cell-pair-has-bar?
table-rows-pair->row-bars
table->row-bars
vertical-cell-pair->bar
cell-quad->junction
table-rows-pair->bar
4.3 SGR Formatting
sgr-list-width
sgr-lines-width
sgr-list-prefix
sgr-list-split-at
sgr-line-clip
sgr-line-wrap
sgr-list-split-at-char
sgr-list-stretch-tab
sgr-list-split
sgr-list-words-take-width
sgr-line-reflow
sgr-lines-fill-block-vertical
sgr-lines-fill-block-horizontal
sgr-lines-fill-block
8.16

4 RenderingπŸ”—β„Ή

These modules handle the table contents and its borders rendering. They use the table transformations to prepare the pre-rendered cells and then output the cells interleaved with Unicode borders as required.

4.1 Table RenderingπŸ”—β„Ή

 (require uni-table/private/table-render)
  package: uni-table

This module implements the actual rendering of two-dimensional table - a list of lists - into final multi-line string.

procedure

(table-row->sgr-lines row-cells    
  column-bars    
  border-sgr-state) β†’ sgr-lines?
  row-cells : table-row?
  column-bars : (listof boolean?)
  border-sgr-state : sgr-state?
Renders a single table-row? into a sgr-lines? including optional vertical bars between cells - although without preceeding and following horizontal bars.

procedure

(table->sgr-lines tbl border-style-spec) β†’ sgr-lines?

  tbl : ut:table?
  border-style-spec : sgr-style-spec/c
Converts given table? into a sgr-lines? for output to terminal.

procedure

(print-table tbl    
  [#:cell-borders cell-borders-spec    
  #:row-borders rows-borders-spec    
  #:column-borders columns-borders-spec    
  #:border-style border-style-spec    
  #:cell-align cell-align-spec    
  #:row-align row-align-spec    
  #:column-align column-align-spec    
  #:cell-style cell-style-spec    
  #:row-style row-style-spec    
  #:column-style column-style-spec    
  #:table-border table-borders-spec    
  #:suppress-newline suppress-newline    
  #:column-widths column-widths-spec]) β†’ void?
  tbl : table/c
  cell-borders-spec : borders-spec/c = '()
  rows-borders-spec : (spec-template-of borders-spec/c) = '()
  columns-borders-spec : (spec-template-of borders-spec/c) = '()
  border-style-spec : sgr-style-spec/c = '()
  cell-align-spec : alignment-spec/c = '()
  row-align-spec : (spec-template-of alignment-spec/c) = '()
  column-align-spec : (spec-template-of alignment-spec/c) = '()
  cell-style-spec : sgr-style-spec/c = '()
  row-style-spec : (spec-template-of sgr-style-spec/c) = '()
  column-style-spec : (spec-template-of sgr-style-spec/c) = '()
  table-borders-spec : borders-spec/c = '()
  suppress-newline : boolean? = #t
  column-widths-spec : column-widths-spec/c = '()
Prints given table/c two-dimensional table to (current-output-port). It is possible to use ECMA-48 SGR codes in the cells independently.

procedure

(table->string tbl    
  [#:cell-borders cell-borders-spec    
  #:row-borders rows-borders-spec    
  #:column-borders columns-borders-spec    
  #:border-style border-style-spec    
  #:cell-align cell-align-spec    
  #:row-align row-align-spec    
  #:column-align column-align-spec    
  #:cell-style cell-style-spec    
  #:row-style row-style-spec    
  #:column-style column-style-spec    
  #:table-border table-borders-spec    
  #:column-widths column-widths-spec]) β†’ string?
  tbl : table/c
  cell-borders-spec : borders-spec/c = '()
  rows-borders-spec : (spec-template-of borders-spec/c) = '()
  columns-borders-spec : (spec-template-of borders-spec/c) = '()
  border-style-spec : sgr-style-spec/c = '()
  cell-align-spec : alignment-spec/c = '()
  row-align-spec : (spec-template-of alignment-spec/c) = '()
  column-align-spec : (spec-template-of alignment-spec/c) = '()
  cell-style-spec : sgr-style-spec/c = '()
  row-style-spec : (spec-template-of sgr-style-spec/c) = '()
  column-style-spec : (spec-template-of sgr-style-spec/c) = '()
  table-borders-spec : borders-spec/c = '()
  column-widths-spec : column-widths-spec/c = '()
Converts given table/c two-dimensional table to a formatted string?. It is possible to use ECMA-48 SGR codes in the cells independently.

4.2 Table BordersπŸ”—β„Ή

 (require uni-table/private/table-borders)
  package: uni-table

This module implements rendering border bars between individual cells in a row and between rows as well. It handles line style and weight merging and when to include space for the border or make cells immediately adjacent.

procedure

(horizontal-cell-pair-has-bar? left right) β†’ boolean?

  left : (or/c ut:cell? #f)
  right : (or/c ut:cell? #f)
Returns #t if there is some kind of border bar present between the cells left and right. Border cells can be checked by providing #f instead of the cell outside of the table.

procedure

(horizontal-cell-pair->bar left right) β†’ sgr-list?

  left : (or/c ut:cell? #f)
  right : (or/c ut:cell? #f)
Returns the character - actually a sgr-list? with sgr-state? snapshot at the beginning - of border line between left and right cells. Providing #f instead of actual ut:cell? allows for easy checking of cells at the table borders.

procedure

(table-row->column-bars row) β†’ (listof boolean?)

  row : table-row?
Returns a list? with boolean? values representing existence of table border bar at given location. The resulting list has always one value more than the number of cells in the input table-row?.

procedure

(table->column-bars tbl) β†’ (listof boolean?)

  tbl : table?
Returns a list? with boolean? values representing existence of table border bar at given location across all rows of given table?.

procedure

(table-row->cell-bars row column-bars) β†’ (listof sgr-list?)

  row : table-row?
  column-bars : (listof boolean?)
Produces a list of vertical bars between the cells of given row based on given column-bars (the presence of border bars around table columns). If no bar is to be emitted at particular location, the sgr-list? created is empty.

procedure

(vertical-cell-pair-has-bar? upper lower) β†’ boolean?

  upper : (or/c #f ut:cell?)
  lower : (or/c #f ut:cell?)
Returns #t if there is some kind of border bar present between the cells upper and lower. Border cells can be checked by providing #f instead of the cell outside of the table.

procedure

(table-rows-pair->row-bars upper lower) β†’ boolean?

  upper : (or/c #f table-row?)
  lower : 
(if (false? upper)
    table-row?
    (or/c #f table-row?))
Returns #t if ther is a bar present between any cells of the two rows given. Bars before the first and after the last row can be checked by providing #f as the other row argument.

procedure

(table->row-bars tbl) β†’ (listof boolean?)

  tbl : table?
Returns a list? of boolean? values representing the existence of horizontal bar requirement at the borders of all rows of given table.

procedure

(vertical-cell-pair->bar upper lower width) β†’ string?

  upper : (or/c ut:cell? #f)
  lower : (or/c ut:cell? #f)
  width : exact-nonnegative-integer?
Creates a string? of given width containing copies of a character representing the border between the two cells given. The character is determined by straight-line-char in 'horizontal mode.

procedure

(cell-quad->junction r0c0 r0c1 r1c0 r1c1) β†’ string?

  r0c0 : (or/c ut:cell? #f)
  r0c1 : (or/c ut:cell? #f)
  r1c0 : (or/c ut:cell? #f)
  r1c1 : (or/c ut:cell? #f)
Uses merge-line-weight on adjacent edges of given cells to construct a junction? and passes the result to junction->char to obtain the junction Unicode character.

The arguments represent cells in the following relative positions:

r0c0

|

r0c1

----

+

----

r1c0

|

r1c1

procedure

(table-rows-pair->bar upper    
  lower    
  column-widths    
  column-bars) β†’ sgr-list?
  upper : (or/c #f table-row?)
  lower : 
(if (false? upper)
    table-row?
    (or/c #f table-row?))
  column-widths : (listof exact-nonnegative-integer?)
  column-bars : (listof boolean?)
Given two rows or #f in place of one of them, returns the bar that should be displayed between them. Both the straight lines and junctions are generated correctly.

4.3 SGR FormattingπŸ”—β„Ή

This module handles formatting blocks of texts parsed from CSI SGR streams.

procedure

(sgr-list-width lst) β†’ exact-nonnegative-integer?

  lst : sgr-list?
Returns the width (in characters) of given sgr-list?. Should be the same as summing lengths of all string contained in the list.

procedure

(sgr-lines-width lines) β†’ exact-nonnegative-integer?

  lines : sgr-lines?
Returns the width in characters of given list of sgr-list? lines.

procedure

(sgr-list-prefix lst width) β†’ sgr-list?

  lst : sgr-list?
  width : exact-nonnegative-integer?
Returns sgr-list? which is a prefix of given width of given lst.

procedure

(sgr-list-split-at lst width) β†’ 
sgr-list? sgr-list?
  lst : sgr-list?
  width : exact-nonnegative-integer?
Splits given sgr-list? lst at position width returning prefix and suffix sgr-list?s where prefix is width characters long.

procedure

(sgr-line-clip line    
  width    
  [#:continuation continuation]) β†’ sgr-lines?
  line : sgr-list?
  width : exact-nonnegative-integer?
  continuation : sgr-list? = '()
Converts single sgr-list? to a single-line sgr-lines? containing at most width printable characters from the source line.

procedure

(sgr-line-wrap line    
  width    
  [#:continuation continuation]) β†’ sgr-lines?
  line : sgr-list?
  width : exact-nonnegative-integer?
  continuation : sgr-list? = '()
Converts single sgr-list? to a possibly multi-line sgr-lines? where each line contains at most width characters from the source line.

Introduces line-breaks as required whenever the next character would make the line longer than width.

procedure

(sgr-list-split-at-char lst char) β†’ 
sgr-list? sgr-list?
  lst : sgr-list?
  char : char?
Splits given sgr-list? into two delimited at first occurence of given char.

procedure

(sgr-list-stretch-tab lst target-width) β†’ sgr-list?

  lst : sgr-list?
  target-width : exact-nonnegative-integer?
Splits given sgr-list? at #\tab character and inserts enough space characters between the prefix and suffix that remain to create a sgr-list? of target-width width.

procedure

(sgr-list-split lst) β†’ (listof sgr-list?)

  lst : sgr-list?
Splits given sgr-list? lst at words’ boundaries returning a list of sgr-list?s representing individual words.

procedure

(sgr-list-words-take-width words 
  wanted-width 
  [#:split-lone split-lone]) 
 β†’ 
sgr-list? (listof sgr-list?)
  words : (listof sgr-list?)
  wanted-width : exact-nonnegative-integer?
  split-lone : boolean? = #f
Takes a list of sgr-list?s representing words and returns a sgr-list? of at most wanted-width width created by joining words from the beginning of the list. The other value returned is a list of remaining words.

procedure

(sgr-line-reflow line    
  width    
  [#:continuation continuation]) β†’ sgr-lines?
  line : sgr-list?
  width : exact-nonnegative-integer?
  continuation : sgr-list? = '()
Transforms given line into a possibly multi-line sgr-lines? wher eeach line contains at most width characters from the source line.

Line-breaks are introduced only at word boundaries.

procedure

(sgr-lines-fill-block-vertical lines 
  [#:width width 
  #:height height 
  #:vertical-align valign 
  #:compact do-compact]) 
 β†’ sgr-lines?
  lines : sgr-lines?
  width : (or/c #f exact-nonnegative-integer?) = #f
  height : (or/c #f exact-nonnegative-integer?) = #f
  valign : valign/c = #f
  do-compact : boolean? = #f
Ensures given sgr-lines? are at least height lines long (high), properly aligned based on given valign and any sgr-list?s introduced are width characters long. It also ensures the sgr-state? at the beginning of each line matches the adjacent line SGR state as needed.

procedure

(sgr-lines-fill-block-horizontal lines 
  [#:width width 
  #:horizontal-align halign 
  #:overflow overflow 
  #:compact do-compact 
  #:continuation continuation]) 
 β†’ sgr-lines?
  lines : sgr-lines?
  width : (or/c #f exact-nonnegative-integer?) = #f
  halign : halign/c = #f
  overflow : overflow/c = 'wrap
  do-compact : boolean? = #t
  continuation : sgr-list? = '()
Ensures that the width of all given lines is exactly the width provided.

procedure

(sgr-lines-fill-block orig-lines 
  [#:width width 
  #:height height 
  #:horizontal-align horiz-align 
  #:vertical-align vert-align 
  #:overflow overflow]) 
 β†’ sgr-lines?
  orig-lines : sgr-lines?
  width : (or/c #f exact-nonnegative-integer?) = #f
  height : (or/c #f exact-nonnegative-integer?) = #f
  horiz-align : halign/c = #f
  vert-align : valign/c = #f
  overflow : overflow/c = 'wrap
Ensures that all sgr-list? orig-lines have exactly given width and the number of returned sgr-list? lines is exactly height.