aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/tools/console/commands.py
blob: 689aa6372e38d26ebf56d39dccd468f349f2f05d (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import urwid
import blinker
import textwrap
from mitmproxy.tools.console import common
from mitmproxy.tools.console import signals

HELP_HEIGHT = 5


footer = [
    ('heading_key', "enter"), ":edit ",
    ('heading_key', "?"), ":help ",
]


def _mkhelp():
    text = []
    keys = [
        ("enter", "execute command"),
    ]
    text.extend(common.format_keyvals(keys, key="key", val="text", indent=4))
    return text


help_context = _mkhelp()


def fcol(s, width, attr):
    s = str(s)
    return (
        "fixed",
        width,
        urwid.Text((attr, s))
    )


command_focus_change = blinker.Signal()


class CommandItem(urwid.WidgetWrap):
    def __init__(self, walker, cmd, focused):
        self.walker, self.cmd, self.focused = walker, cmd, focused
        super().__init__(None)
        self._w = self.get_widget()

    def get_widget(self):
        parts = [
            ("focus", ">> " if self.focused else "   "),
            ("title", self.cmd.path),
            ("text", " "),
            ("text", " ".join(self.cmd.paramnames())),
        ]
        if self.cmd.returntype:
            parts.append([
                ("title", " -> "),
                ("text", self.cmd.retname()),
            ])

        return urwid.AttrMap(
            urwid.Padding(urwid.Text(parts)),
            "text"
        )

    def get_edit_text(self):
        return self._w[1].get_edit_text()

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key


class CommandListWalker(urwid.ListWalker):
    def __init__(self, master):
        self.master = master

        self.index = 0
        self.focusobj = None
        self.cmds = list(master.commands.commands.values())
        self.cmds.sort(key=lambda x: x.signature_help())
        self.set_focus(0)

    def get_edit_text(self):
        return self.focus_obj.get_edit_text()

    def _get(self, pos):
        cmd = self.cmds[pos]
        return CommandItem(self, cmd, pos == self.index)

    def get_focus(self):
        return self.focus_obj, self.index

    def set_focus(self, index):
        cmd = self.cmds[index]
        self.index = index
        self.focus_obj = self._get(self.index)
        command_focus_change.send(cmd.help or "")

    def get_next(self, pos):
        if pos >= len(self.cmds) - 1:
            return None, None
        pos = pos + 1
        return self._get(pos), pos

    def get_prev(self, pos):
        pos = pos - 1
        if pos < 0:
            return None, None
        return self._get(pos), pos


class CommandsList(urwid.ListBox):
    def __init__(self, master):
        self.master = master
        self.walker = CommandListWalker(master)
        super().__init__(self.walker)

    def keypress(self, size, key):
        if key == "enter":
            foc, idx = self.get_focus()
            signals.status_prompt_command.send(partial=foc.cmd.path + " ")
        return super().keypress(size, key)


class CommandHelp(urwid.Frame):
    def __init__(self, master):
        self.master = master
        super().__init__(self.widget(""))
        self.set_active(False)
        command_focus_change.connect(self.sig_mod)

    def set_active(self, val):
        h = urwid.Text("Command Help")
        style = "heading" if val else "heading_inactive"
        self.header = urwid.AttrWrap(h, style)

    def widget(self, txt):
        cols, _ = self.master.ui.get_cols_rows()
        return urwid.ListBox(
            [urwid.Text(i) for i in textwrap.wrap(txt, cols)]
        )

    def sig_mod(self, txt):
        self.set_body(self.widget(txt))


class Commands(urwid.Pile):
    def __init__(self, master):
        oh = CommandHelp(master)
        super().__init__(
            [
                CommandsList(master),
                (HELP_HEIGHT, oh),
            ]
        )
        self.master = master

    def keypress(self, size, key):
        key = common.shortcuts(key)
        if key == "tab":
            self.focus_position = (
                self.focus_position + 1
            ) % len(self.widget_list)
            self.widget_list[1].set_active(self.focus_position == 1)
            key = None

        # This is essentially a copypasta from urwid.Pile's keypress handler.
        # So much for "closed for modification, but open for extension".
        item_rows = None
        if len(size) == 2:
            item_rows = self.get_item_rows(size, focus = True)
        i = self.widget_list.index(self.focus_item)
        tsize = self.get_item_size(size, i, True, item_rows)
        return self.focus_item.keypress(tsize, key)