Last active
November 14, 2021 14:22
-
-
Save nat-418/11398e0dada97baa69b482c3f67338e0 to your computer and use it in GitHub Desktop.
Pretty-print a list of lists as a table in Tcl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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