make limited length string in form: "the string is very lo...(and 15 more)"
(string, max_width)
| 93 | |
| 94 | |
| 95 | def shorten_string(string, max_width): |
| 96 | ''' make limited length string in form: |
| 97 | "the string is very lo...(and 15 more)" |
| 98 | ''' |
| 99 | string_len = len(string) |
| 100 | if string_len <= max_width: |
| 101 | return string |
| 102 | visible = max_width - 16 - int(log10(string_len)) |
| 103 | # expected suffix len "...(and XXXXX more)" |
| 104 | if not isinstance(string, str): |
| 105 | visstring = str(string[:visible], errors='ignore') |
| 106 | else: |
| 107 | visstring = string[:visible] |
| 108 | return u''.join((visstring, u'...(and ', |
| 109 | str(string_len - visible), u' more)')) |
| 110 | |
| 111 | |
| 112 | def get_console_width(): |