commander: palette entries, highlight errors

- Add palette entries specific to commander
- Highlight errors
- Introduce an Unknown type to keep track of extra unknown arguments to
commands
This commit is contained in:
Aldo Cortesi 2017-12-20 09:09:40 +13:00
parent 3341edc9fa
commit d7ee5d8f85
6 changed files with 56 additions and 6 deletions

View File

@ -172,7 +172,7 @@ class CommandManager(mitmproxy.types._CommandBase):
if parts[i] in self.commands:
params[:] = self.commands[parts[i]].paramtypes
else:
typ = str
typ = mitmproxy.types.Unknown
to = mitmproxy.types.CommandTypes.get(typ, None)
valid = False

View File

@ -72,10 +72,13 @@ class CommandBuffer():
parts, _ = self.master.commands.parse_partial(self.text)
ret = []
for p in parts:
if p.type == mitmproxy.types.Cmd and p.valid:
ret.append(("title", p.value))
if p.valid:
if p.type == mitmproxy.types.Cmd:
ret.append(("commander_command", p.value))
else:
ret.append(("text", p.value))
else:
ret.append(("text", p.value))
ret.append(("commander_invalid", p.value))
ret.append(("text", " "))
return ret

View File

@ -32,6 +32,9 @@ class Palette:
# Grid Editor
'focusfield', 'focusfield_error', 'field_error', 'editfield',
# Commander
'commander_command', 'commander_invalid'
]
high = None # type: typing.Mapping[str, typing.Sequence[str]]
@ -117,6 +120,10 @@ class LowDark(Palette):
focusfield_error = ('dark red', 'light gray'),
field_error = ('dark red', 'default'),
editfield = ('white', 'default'),
commander_command = ('white,bold', 'default'),
commander_invalid = ('light red', 'default'),
)
@ -183,6 +190,9 @@ class LowLight(Palette):
focusfield_error = ('dark red', 'light gray'),
field_error = ('dark red', 'black'),
editfield = ('black', 'default'),
commander_command = ('dark magenta', 'default'),
commander_invalid = ('light red', 'default'),
)
@ -267,6 +277,9 @@ class SolarizedLight(LowLight):
focusfield_error = (sol_red, sol_base2),
field_error = (sol_red, 'default'),
editfield = (sol_base01, 'default'),
commander_command = (sol_cyan, 'default'),
commander_invalid = (sol_orange, 'default'),
)
@ -317,6 +330,9 @@ class SolarizedDark(LowDark):
focusfield_error = (sol_red, sol_base02),
field_error = (sol_red, 'default'),
editfield = (sol_base1, 'default'),
commander_command = (sol_blue, 'default'),
commander_invalid = (sol_orange, 'default'),
)

View File

@ -18,6 +18,10 @@ class Arg(str):
pass
class Unknown(str):
pass
class CutSpec(typing.Sequence[str]):
pass
@ -116,6 +120,20 @@ class _StrType(_BaseType):
return isinstance(val, str)
class _UnknownType(_BaseType):
typ = Unknown
display = "unknown"
def completion(self, manager: _CommandBase, t: type, s: str) -> typing.Sequence[str]:
return []
def parse(self, manager: _CommandBase, t: type, s: str) -> str:
return s
def is_valid(self, manager: _CommandBase, typ: typing.Any, val: typing.Any) -> bool:
return False
class _IntType(_BaseType):
typ = int
display = "int"

View File

@ -78,8 +78,12 @@ class TestCommand:
[
"foo bar",
[
command.ParseResult(value = "foo", type = mitmproxy.types.Cmd, valid = False),
command.ParseResult(value = "bar", type = str, valid = True)
command.ParseResult(
value = "foo", type = mitmproxy.types.Cmd, valid = False
),
command.ParseResult(
value = "bar", type = mitmproxy.types.Unknown, valid = False
)
],
[],
],

View File

@ -43,6 +43,15 @@ def test_str():
assert b.parse(tctx.master.commands, str, "foo") == "foo"
def test_unknown():
with taddons.context() as tctx:
b = mitmproxy.types._UnknownType()
assert b.is_valid(tctx.master.commands, mitmproxy.types.Unknown, "foo") is False
assert b.is_valid(tctx.master.commands, mitmproxy.types.Unknown, 1) is False
assert b.completion(tctx.master.commands, mitmproxy.types.Unknown, "") == []
assert b.parse(tctx.master.commands, mitmproxy.types.Unknown, "foo") == "foo"
def test_int():
with taddons.context() as tctx:
b = mitmproxy.types._IntType()