| 169 | |
| 170 | |
| 171 | class TruncatedText(urwid.Widget): |
| 172 | def __init__(self, text, attr, align="left"): |
| 173 | self.text = text |
| 174 | self.attr = attr |
| 175 | self.align = align |
| 176 | super().__init__() |
| 177 | |
| 178 | def pack(self, size, focus=False): |
| 179 | return (len(self.text), 1) |
| 180 | |
| 181 | def rows(self, size, focus=False): |
| 182 | return 1 |
| 183 | |
| 184 | def render(self, size, focus=False): |
| 185 | text = self.text |
| 186 | attr = self.attr |
| 187 | if self.align == "right": |
| 188 | text = text[::-1] |
| 189 | attr = attr[::-1] |
| 190 | |
| 191 | text_len = urwid.calc_width(text, 0, len(text)) |
| 192 | if size is not None and len(size) > 0: |
| 193 | width = size[0] |
| 194 | else: |
| 195 | width = text_len |
| 196 | |
| 197 | if width >= text_len: |
| 198 | remaining = width - text_len |
| 199 | if remaining > 0: |
| 200 | c_text = text + " " * remaining |
| 201 | c_attr = attr + [("text", remaining)] |
| 202 | else: |
| 203 | c_text = text |
| 204 | c_attr = attr |
| 205 | else: |
| 206 | trim = urwid.util.calc_trim_text(text, 0, width - 1, 0, width - 1) |
| 207 | visible_text = text[0 : trim[1]] |
| 208 | if trim[3] == 1: |
| 209 | visible_text += " " |
| 210 | c_text = visible_text + SYMBOL_ELLIPSIS |
| 211 | c_attr = urwid.util.rle_subseg(attr, 0, len(visible_text.encode())) + [ |
| 212 | ("focus", len(SYMBOL_ELLIPSIS.encode())) |
| 213 | ] |
| 214 | |
| 215 | if self.align == "right": |
| 216 | c_text = c_text[::-1] |
| 217 | c_attr = c_attr[::-1] |
| 218 | |
| 219 | return urwid.TextCanvas([c_text.encode()], [c_attr], maxcol=width) |
| 220 | |
| 221 | |
| 222 | def truncated_plain(text, attr, align="left"): |
no outgoing calls
no test coverage detected
searching dependent graphs…