1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from libxyz.ui import lowui
18 from libxyz.core.utils import ustring
19
21 """
22 Invalidate canvas after calling function
23 """
24
25 def _touch(instance, *args, **kwargs):
26 _res = func(instance, *args, **kwargs)
27 instance._invalidate()
28
29 return _res
30
31 return _touch
32
33
34
35 -def truncate(text, cols, enc=None, backward=False):
36 """
37 Truncate text if its length exceeds cols
38 If backward is True, text will be truncated from the beginning
39 """
40
41 if enc is None:
42 enc = xyzenc
43
44 text = ustring(text, enc)
45
46 _len = lowui.util.calc_width(text, 0, len(text))
47
48 if _len < cols:
49 return text
50 else:
51 if backward:
52 return u"~%s" % text[-(cols - 1):]
53 else:
54 return u"%s~" % text[:cols - 1]
55
56
57
59 """
60 Return length of the string in terms of terminal columns
61 """
62
63 return lowui.util.calc_width(text, 0, len(text))
64