Search code examples
tclputs

tcl puts if format strings


I want to print a information in a table. some of the information contain a color code as \033\[1;31m${info}\033\[0m

The method I use to print the content is as below:

set name "arb bul"
set qname black
set phone 123-456789
set print_users az
set name "arbi yu"
set info dod
set string  "%-15s%-22s%-15s%-11s%-1s"
puts [format $string $name $qname ${info} ${phone} $print_users]

The problem came out when I try to color one of the strings, the table shift doesn't kept since the next string take into account the spaces from the last characters.

 set qname "\033\[1;31mblack\033\[0m"
 puts [format $string $name $qname ${info} ${phone} $print_users]

Is there any way to set the string spaces from the beginning of the line?


Solution

  • The format command does not have any knowledge of what the different escape codes may mean to the terminal. Different terminals use different escape codes. If you want to be able to use escape codes in your values, it may be best to also use escape codes to position the strings:

    set string  "%s\033\[16G%s\033\[38G%s\033\[53G%s\033\[64G%s"