proc string'limit {str length} { expr {[string length $str]>$length? "[string range $str 0 [incr length -4]]..." : $str} } % string'limit hello 10 hello string'limit "hello long string" 10 hello l...
escargo 4 Oct 2005 - There are typographic rules for the use of ellipsis that make its use less than straightforward [1].
JH - The string replace command is designed to do the ellipsis without otherwise modifying the string. Just use:
set str [string replace $str $length end ...]If the string is already shorter than $length, it will not be touched.There is the end case where [string length $str] > $length, but [string length $str] < $length + 3. Adding the ellipsis might make the string longer.
JMN - the little overtype package also supports the ellipsis.
%package require overtype %set field1 [string repeat " " 5] %puts [overtype::left -ellipsis 1 $field1 1234567]nextfieldstring 12...nextfieldstringIt too has an edge case where if the field is shorter than an ellipsis the result will contain just the ellipsis or part thereof.
%set field1 [string repeat " " 2] %puts [overtype::left -ellipsis 1 $field1 1234567]nextfieldstring ..nextfieldstring