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