Created
November 14, 2021 13:16
-
-
Save nat-418/8e63ea3adddd6a3c1613b31353d908c8 to your computer and use it in GitHub Desktop.
Draw a pretty UTF-8 box around some text in Tcl
This file contains 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
# Draw a UTF-8 box around some text. Useful for pretty-printing, logging, etc. | |
# | |
# @param str String to draw box around | |
# @return String enclosed in a fancy box | |
proc box str { | |
# Draw the top of the box | |
# | |
# @param length Number representing how long the top should be | |
# @return String drawing the top of the box | |
proc top length { | |
append result "┌" | |
# We add two here to keep the formatting evenly spaced | |
incr length +2 | |
while {$length > 0} { | |
append result "─" | |
incr length -1 | |
} | |
append result "┐\n" | |
return $result | |
} | |
# Draw the bottom of the box | |
# | |
# @param length Number representing how long the bottom should be | |
# @return String drawing the bottom of the box | |
proc bottom length { | |
append result "└" | |
# We add two here to keep the formatting evenly spaced | |
incr length +2 | |
while {$length > 0} { | |
append result "─" | |
incr length -1 | |
} | |
append result "┘\n" | |
return $result | |
} | |
set lines [split [string trim $str] \n] | |
set lengths [lmap line $lines {string length $line}] | |
set longest [lindex [lsort -real $lengths] end] | |
# Now we are loop through and format the "inside" of the box. | |
append result [top $longest] | |
foreach line $lines { | |
set length [string length $line] | |
set remaining [expr $longest - $length + 1] | |
append result "│ $line" | |
while {$remaining > 0} { | |
append result " " | |
incr remaining -1 | |
} | |
append result "│\n" | |
} | |
append result [bottom $longest] | |
return $result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment