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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
import abc
import typing
import urwid
from urwid.text_layout import calc_coords
import mitmproxy.flow
import mitmproxy.master
import mitmproxy.command
import mitmproxy.types
class Completer: # pragma: no cover
@abc.abstractmethod
def cycle(self) -> str:
pass
class ListCompleter(Completer):
def __init__(
self,
start: str,
options: typing.Sequence[str],
) -> None:
self.start = start
self.options = [] # type: typing.Sequence[str]
for o in options:
if o.startswith(start):
self.options.append(o)
self.options.sort()
self.offset = 0
def cycle(self) -> str:
if not self.options:
return self.start
ret = self.options[self.offset]
self.offset = (self.offset + 1) % len(self.options)
return ret
CompletionState = typing.NamedTuple(
"CompletionState",
[
("completer", Completer),
("parse", typing.Sequence[mitmproxy.command.ParseResult])
]
)
class CommandBuffer():
def __init__(self, master: mitmproxy.master.Master, start: str = "") -> None:
self.master = master
self.buf = start
# Cursor is always within the range [0:len(buffer)].
self._cursor = len(self.buf)
self.completion = None # type: CompletionState
@property
def cursor(self) -> int:
return self._cursor
@cursor.setter
def cursor(self, x) -> None:
if x < 0:
self._cursor = 0
elif x > len(self.buf):
self._cursor = len(self.buf)
else:
self._cursor = x
def render(self):
return self.buf
def left(self) -> None:
self.cursor = self.cursor - 1
def right(self) -> None:
self.cursor = self.cursor + 1
def cycle_completion(self) -> None:
if not self.completion:
parts, remainhelp = self.master.commands.parse_partial(self.buf[:self.cursor])
last = parts[-1]
ct = mitmproxy.types.CommandTypes.get(last.type, None)
if ct:
self.completion = CompletionState(
completer = ListCompleter(
parts[-1].value,
ct.completion(self.master.commands, last.type, parts[-1].value)
),
parse = parts,
)
if self.completion:
nxt = self.completion.completer.cycle()
buf = " ".join([i.value for i in self.completion.parse[:-1]]) + " " + nxt
buf = buf.strip()
self.buf = buf
self.cursor = len(self.buf)
def backspace(self) -> None:
if self.cursor == 0:
return
self.buf = self.buf[:self.cursor - 1] + self.buf[self.cursor:]
self.cursor = self.cursor - 1
self.completion = None
def insert(self, k: str) -> None:
"""
Inserts text at the cursor.
"""
self.buf = self.buf = self.buf[:self.cursor] + k + self.buf[self.cursor:]
self.cursor += 1
self.completion = None
class CommandEdit(urwid.WidgetWrap):
leader = ": "
def __init__(self, master: mitmproxy.master.Master, text: str) -> None:
super().__init__(urwid.Text(self.leader))
self.master = master
self.cbuf = CommandBuffer(master, text)
self.update()
def keypress(self, size, key):
if key == "backspace":
self.cbuf.backspace()
elif key == "left":
self.cbuf.left()
elif key == "right":
self.cbuf.right()
elif key == "tab":
self.cbuf.cycle_completion()
elif len(key) == 1:
self.cbuf.insert(key)
self.update()
def update(self):
self._w.set_text([self.leader, self.cbuf.render()])
def render(self, size, focus=False):
(maxcol,) = size
canv = self._w.render((maxcol,))
canv = urwid.CompositeCanvas(canv)
canv.cursor = self.get_cursor_coords((maxcol,))
return canv
def get_cursor_coords(self, size):
p = self.cbuf.cursor + len(self.leader)
trans = self._w.get_line_translation(size[0])
x, y = calc_coords(self._w.get_text()[0], trans, p)
return x, y
def get_value(self):
return self.cbuf.buf
|