format in TCL in not working correctly

311 views Asked by At

format in TCL in not working correctly,I am trying to format some text and writing them in a file and then sending that file as a mail to user. I am seeing that format is correct in Linux but when mail comes to user then format is not proper.

code-

puts [format {%-50s%-170s%-50s%-50s} "Test_Id" "Test_Description" "Test_Ran_Count" "Test_Result"]
puts [format {%-50s%-170s%-50s%-50s} $test_id1 "$mail_desc1" $loop_count $test_result]
puts [format {%-50s%-170s%-50s%-50s} $test_id "$mail_desc" $loop_count $test_result]

output-

Test_Id           Test_Description            Test_Ran_Count                Test_Result                                       
test_id_1         To execute - test_id_1            10                         PASS
test_id_2         To execute - test_id_2            10                         PASS  

here if test_id_2 is big then entire format is shifting,as per format behavior if test_id is less then 50 char then it should not shift other column as I am giving %-50s for test_id.

1

There are 1 answers

0
Donal Fellows On

Format fields (which Tcl borrows from C's sprintf() with very few changes) are a bit tricky. When you use %50s (or %-50s — the - just affects alignment) you are setting a minimum field width. To set a maximum field width, you might use %.50s. Or, more likely in your case, you'll set minimum, maximum and alignment: %-50.50s.

Demonstrating with some narrower fields and simple strings of different lengths:

foreach str {abc defgh ijklmnop} {
    puts [format ">%-5.5s< |%-5s| /%-.5s/" $str $str $str]
}

Which produces this output:

>abc  < |abc  | /abc/
>defgh< |defgh| /defgh/
>ijklm< |ijklmnop| /ijklm/

As you can see, left aligned, completely fixed width requires giving %-N.Ns (for some N).