commander: command argument underlay

Display context-sensitive argument types as an "underlay" in commander.
This commit is contained in:
Aldo Cortesi 2017-12-20 10:07:35 +13:00
parent d7ee5d8f85
commit 79ca2c8437
3 changed files with 90 additions and 3 deletions

View File

@ -69,7 +69,13 @@ class CommandBuffer():
self._cursor = x
def render(self):
parts, _ = self.master.commands.parse_partial(self.text)
"""
This function is somewhat tricky - in order to make the cursor
position valid, we have to make sure there is a
character-for-character offset match in the rendered output, up
to the cursor. Beyond that, we can add stuff.
"""
parts, remhelp = self.master.commands.parse_partial(self.text)
ret = []
for p in parts:
if p.valid:
@ -77,9 +83,15 @@ class CommandBuffer():
ret.append(("commander_command", p.value))
else:
ret.append(("text", p.value))
else:
elif p.value:
ret.append(("commander_invalid", p.value))
else:
ret.append(("text", ""))
ret.append(("text", " "))
if remhelp:
ret.append(("text", " "))
for v in remhelp:
ret.append(("commander_hint", "%s " % v))
return ret
def flatten(self, txt):

View File

@ -34,7 +34,7 @@ class Palette:
'focusfield', 'focusfield_error', 'field_error', 'editfield',
# Commander
'commander_command', 'commander_invalid'
'commander_command', 'commander_invalid', 'commander_hint'
]
high = None # type: typing.Mapping[str, typing.Sequence[str]]
@ -124,6 +124,7 @@ class LowDark(Palette):
commander_command = ('white,bold', 'default'),
commander_invalid = ('light red', 'default'),
commander_hint = ('dark gray', 'default'),
)
@ -193,6 +194,7 @@ class LowLight(Palette):
commander_command = ('dark magenta', 'default'),
commander_invalid = ('light red', 'default'),
commander_hint = ('light gray', 'default'),
)
@ -280,6 +282,7 @@ class SolarizedLight(LowLight):
commander_command = (sol_cyan, 'default'),
commander_invalid = (sol_orange, 'default'),
commander_hint = (sol_base1, 'default'),
)
@ -333,6 +336,7 @@ class SolarizedDark(LowDark):
commander_command = (sol_blue, 'default'),
commander_invalid = (sol_orange, 'default'),
commander_hint = (sol_base00, 'default'),
)

View File

@ -23,6 +23,10 @@ class TAddon:
def cmd3(self, foo: int) -> int:
return foo
@command.command("cmd4")
def cmd4(self, a: int, b: str, c: mitmproxy.types.Path) -> str:
return "ok"
@command.command("subcommand")
def subcommand(self, cmd: mitmproxy.types.Cmd, *args: mitmproxy.types.Arg) -> str:
return "ok"
@ -46,6 +50,10 @@ class TAddon:
def path(self, arg: mitmproxy.types.Path) -> None:
pass
@command.command("flow")
def flow(self, f: flow.Flow, s: str) -> None:
pass
class TestCommand:
def test_varargs(self):
@ -140,6 +148,69 @@ class TestCommand:
],
[]
],
[
"cmd4",
[
command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
],
["int", "str", "path"]
],
[
"cmd4 ",
[
command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "", type = int, valid = False),
],
["str", "path"]
],
[
"cmd4 1",
[
command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "1", type = int, valid = True),
],
["str", "path"]
],
[
"cmd4 1",
[
command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "1", type = int, valid = True),
],
["str", "path"]
],
[
"flow",
[
command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
],
["flow", "str"]
],
[
"flow ",
[
command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "", type = flow.Flow, valid = False),
],
["str"]
],
[
"flow x",
[
command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "x", type = flow.Flow, valid = False),
],
["str"]
],
[
"flow x ",
[
command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
command.ParseResult(value = "x", type = flow.Flow, valid = False),
command.ParseResult(value = "", type = str, valid = True),
],
[]
],
]
with taddons.context() as tctx:
tctx.master.addons.add(TAddon())