-
Notifications
You must be signed in to change notification settings - Fork 1
/
widgets.py
71 lines (60 loc) · 2.13 KB
/
widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import urwid
class Text(urwid.WidgetWrap):
def parse_match(self, m_iter):
pass
def is_empty(self):
return self.widget is None or len(self.get_text()) <= 0
def get_text(self):
return self.widget.get_text()
def set_text(self, markup):
self.widget.set_text(markup)
def rows(self, size, focus=False):
if self.is_empty():
return 0
return self.widget.rows(size, focus)
class SearchText(Text):
def __init__(self, m_iter):
self.widget = None
self.parse_match(m_iter)
urwid.WidgetWrap.__init__(self, self.widget)
def parse_match(self, m_iter):
parts = []
string = ''
prev_start = prev_end = 0
for m in m_iter:
string = m.string
if m.start() > prev_end:
parts.append(m.string[prev_end:m.start()])
prev_end = m.end()
prev_start = m.start()
parts.append(('match', m.group(0)))
if len(parts) > 0:
if prev_end != len(string):
parts.append(string[prev_end:])
self.widget = urwid.Text(parts, wrap='clip')
class ReplaceText(Text):
def __init__(self, pair, replacement, delim):
self.widget = None
self.parse_match(pair, replacement, delim)
urwid.WidgetWrap.__init__(self, self.widget)
def parse_match(self, pair, replacement, delim):
parts = []
file = pair[1]
s = file.find(delim[0])
e = file.find(delim[1])
ps = pe = 0
if len(replacement) == 0:
parts = [file.replace(delim[0], '').replace(delim[1], '')]
else:
while s >= 0:
if s > 0:
parts.append(file[pe:s].replace(delim[1], ''))
parts.append(('match', replacement))
ps = s
pe = e
s = file.find(delim[0], s + 1)
e = file.find(delim[1], e + 1)
if len(parts) > 0:
if pe != len(file) and len(replacement) > 0:
parts.append(file[pe:].replace(delim[1], ''))
self.widget = urwid.Text(parts, wrap='clip')