Boxed text

Richard Suchenwirth 2005-10-24 - To make some texts more perspicuous in plain-text output, here's code to wrap them in ASCII graphic boxes (looks good only in fixed-pitch fonts):

 proc boxtext str {
    set lines [split $str \n] 
    set maxl 0
    foreach line $lines {
       set l [string length $line]
       if {$l > $maxl} {set maxl $l}
    }
    set res +-[string repeat - $maxl]-+\n
    foreach line $lines {
        append res [format "| %-${maxl}s |\n" $line]
    }
    append res +-[string repeat - $maxl]-+
 }

Testing:

 % boxtext "Hello world"
 +-------------+
 | Hello world |
 +-------------+
 % boxtext "Hello world\nagain and again and again"
 +---------------------------+
 | Hello world               |
 | again and again and again |
 +---------------------------+

 % boxtext "Hello world\nagain"
 +-------------+
 | Hello world |
 | again       |
 +-------------+