Skip to content

Instantly share code, notes, and snippets.

@rebolek
Created September 18, 2019 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebolek/c898b6f115e5ba963bae50e376aee3ce to your computer and use it in GitHub Desktop.
Save rebolek/c898b6f115e5ba963bae50e376aee3ce to your computer and use it in GitHub Desktop.
Print table in console
Red[]
test-table: [
["Name" "Mass" "Orbit"]
["Mercury" 0.055 0.4]
["Venus" 0.815 0.7]
["Earth" 1.0 1.0]
["Mars" 0.107 1.5]
]
make-divider: func [
stops [block!] "Where to put + chars"
/local
result
][
result: copy "+"
last: 0
forall stops [
append result append/dup clear "" #"-" stops/1
append result #"+"
]
result
]
align-string: func [
"(modifies)"
string [string!]
width [integer!]
alignment [word!] "'center, 'left, 'right"
][
pad: max 0 width - length? string
switch/default alignment [
left [append/dup string #" " pad]
right [insert/dup string #" " pad]
][
append/dup string #" " pad / 2
insert/dup string #" " pad / 2
unless equal? width length? string [append string #" "]
]
string
]
get-widths: func [
table [block!] "Block of blocks"
/local widths index length
][
widths: append/dup [] 0 length? table/1
foreach line table [
forall line [
index: index? line
if (length: length? form line/1) > widths/:index [
widths/:index: length
]
]
]
widths
]
make-line: func [
line [block!]
widths [block!]
/padding
pads [block!] "Left and right paddings (pair!)"
; TODO: alignments
/local result index
][
result: copy "+"
forall line [
index: index? line
if pads [append/dup result #" " pads/:index/x]
append result align-string form line/1 widths/:index 'center
if pads [append/dup result #" " pads/:index/y]
append result #"+"
]
result
]
make-table: func [
table [block!]
/header "Put ---- after first line"
][
result: copy ""
widths: get-widths table
paddings: append/dup clear [] 2x2 length? widths
; first line
append result make-line/padding table/1 widths paddings
append result newline
if header [
header-widths: copy widths
forall header-widths [
index: index? header-widths
header-widths/1: header-widths/1 + paddings/:index/x + paddings/:index/y
]
append result make-divider header-widths
append result newline
]
; rest of data
foreach line next table [
append result make-line/padding line widths paddings
append result newline
]
result
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment