Skip to content

Instantly share code, notes, and snippets.

@nat-418
Last active November 14, 2021 14:22
Show Gist options
  • Save nat-418/11398e0dada97baa69b482c3f67338e0 to your computer and use it in GitHub Desktop.
Save nat-418/11398e0dada97baa69b482c3f67338e0 to your computer and use it in GitHub Desktop.
Pretty-print a list of lists as a table in Tcl
# Format a list of lists into an evently-spaced table for logging etc.
#
# @param llst List of lists to format.
# @param padding Optionally specify how many spaces to separate fields, defaults to four.
# @return String of the pretty table.
proc table {llst {padding 4}} {
set lengths [lmap each [join $llst] {string length $each}]
set longest [expr $padding + max([join $lengths ","])]
foreach lst $llst {
foreach element $lst {
set length [string length $element]
set remaining [expr $longest - $length]
append result $element
while {$remaining > 0} {
append result " "
incr remaining -1
}
}
append result \n
}
return $result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment