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.ui.utils import term_width
19
21 """
22 Horizontal separator widget
23 """
24
25 ignore_focus = True
26
27 - def __init__(self, title=None, div_char=None, title_attr=None,
28 div_attr=None, top=0, bottom=0):
29 """
30 @param title: Title
31 @param div_char: Character to repeat across line
32 @param title_attr: Attribute of title text
33 @param div_attr: Attribute of divider line
34 @param top: number of blank lines above
35 @param bottom: number of blank lines below
36 """
37
38 super(Separator, self).__init__()
39
40 self.set_text(title, title_attr)
41
42 self.div_char = lowui.escape.utf8decode("─")
43 self.div_attr = div_attr
44 self.top = top
45 self.bottom = bottom
46
47
48
49 - def set_text(self, title, title_attr=None):
50 """
51 Set some text in the middle of seprator
52 """
53
54 if title is not None:
55 _title = " %s " % title
56 self.title_len = term_width(_title)
57
58 if title_attr is not None:
59 self.text = lowui.Text((title_attr, _title))
60 else:
61 self.text = lowui.Text(_title)
62 else:
63 self.text = None
64 self.title_len = 0
65
66 self._invalidate()
67
68
69
70 - def clear_text(self):
71 """
72 Remove text if any
73 """
74
75 self.text = None
76 self.title_len = 0
77
78 self._invalidate()
79
80
81
82 - def rows(self, (maxcol,), focus=False):
83 """
84 Return the number of lines that will be rendered.
85 """
86
87 return self.top + 1 + self.bottom
88
89
90
91 - def render(self, (maxcol,), focus=False):
92 """
93 Render the separator as canvas and return it.
94 """
95
96 sep_len = (maxcol - self.title_len) / 2
97 _len = sep_len * 2 + self.title_len
98
99 if _len != maxcol:
100 _offset = abs(maxcol - _len)
101 else:
102 _offset = 0
103
104 _list = []
105
106 canv_begin = lowui.Text((self.div_attr, self.div_char * sep_len))
107 canv_begin = canv_begin.render((maxcol,))
108 _list.append((canv_begin, None, False, sep_len))
109
110 if self.text is not None:
111 canv_text = self.text.render((maxcol,))
112 _list.append((canv_text, None, False, self.title_len))
113
114 canv_end = lowui.Text((self.div_attr, self.div_char *(sep_len+_offset)))
115 canv_end = canv_end.render((maxcol,))
116 _list.append((canv_end, None, False, sep_len + _offset))
117
118 canv = lowui.CanvasJoin(_list)
119
120 if self.top or self.bottom:
121 canv.pad_trim_top_bottom(self.top, self.bottom)
122
123 return canv
124