console options: help for overlays, improved layout for overlay grid editor

This commit is contained in:
Aldo Cortesi 2017-03-19 12:14:42 +13:00
parent 21794c7bbe
commit cb18c91f13
4 changed files with 40 additions and 16 deletions

View File

@ -309,13 +309,15 @@ class ConsoleMaster(master.Master):
def shutdown(self): def shutdown(self):
raise urwid.ExitMainLoop raise urwid.ExitMainLoop
def overlay(self, widget): def overlay(self, widget, **kwargs):
signals.push_view_state.send( signals.push_view_state.send(
self, self,
window = overlay.SimpleOverlay( window = overlay.SimpleOverlay(
self,
widget, widget,
self.loop.widget, self.loop.widget,
widget.width, widget.width,
**kwargs
) )
) )

View File

@ -1,6 +1,7 @@
import urwid import urwid
import blinker import blinker
import textwrap import textwrap
import pprint
from typing import Optional, Sequence from typing import Optional, Sequence
from mitmproxy import exceptions from mitmproxy import exceptions
@ -8,6 +9,8 @@ from mitmproxy.tools.console import common
from mitmproxy.tools.console import signals from mitmproxy.tools.console import signals
from mitmproxy.tools.console import overlay from mitmproxy.tools.console import overlay
HELP_HEIGHT = 5
def can_edit_inplace(opt): def can_edit_inplace(opt):
if opt.choices: if opt.choices:
@ -27,8 +30,6 @@ def _mkhelp():
keys = [ keys = [
("enter", "edit option"), ("enter", "edit option"),
("D", "reset all to defaults"), ("D", "reset all to defaults"),
("g", "go to start of list"),
("G", "go to end of list"),
("w", "save options"), ("w", "save options"),
] ]
text.extend(common.format_keyvals(keys, key="key", val="text", indent=4)) text.extend(common.format_keyvals(keys, key="key", val="text", indent=4))
@ -62,8 +63,10 @@ class OptionItem(urwid.WidgetWrap):
val = self.opt.current() val = self.opt.current()
if self.opt.typespec == bool: if self.opt.typespec == bool:
displayval = "true" if val else "false" displayval = "true" if val else "false"
elif val is None: elif not val:
displayval = "" displayval = ""
elif self.opt.typespec == Sequence[str]:
displayval = pprint.pformat(val, indent=1)
else: else:
displayval = str(val) displayval = str(val)
@ -218,8 +221,10 @@ class OptionsList(urwid.ListBox):
overlay.OptionsOverlay( overlay.OptionsOverlay(
self.master, self.master,
foc.opt.name, foc.opt.name,
foc.opt.current() foc.opt.current(),
) HELP_HEIGHT + 5
),
valign="top"
) )
else: else:
raise NotImplementedError() raise NotImplementedError()
@ -254,7 +259,7 @@ class Options(urwid.Pile):
super().__init__( super().__init__(
[ [
OptionsList(master), OptionsList(master),
(5, oh), (HELP_HEIGHT, oh),
] ]
) )
self.master = master self.master = master

View File

@ -8,13 +8,15 @@ from mitmproxy.tools.console import grideditor
class SimpleOverlay(urwid.Overlay): class SimpleOverlay(urwid.Overlay):
def __init__(self, widget, parent, width): def __init__(self, master, widget, parent, width, valign="middle"):
self.widget = widget
self.master = master
super().__init__( super().__init__(
widget, widget,
parent, parent,
align="center", align="center",
width=width, width=width,
valign="middle", valign=valign,
height="pack" height="pack"
) )
@ -22,6 +24,8 @@ class SimpleOverlay(urwid.Overlay):
key = super().keypress(size, key) key = super().keypress(size, key)
if key == "esc": if key == "esc":
signals.pop_view_state.send(self) signals.pop_view_state.send(self)
if key == "?":
self.master.view_help(self.widget.make_help())
else: else:
return key return key
@ -105,20 +109,33 @@ class Chooser(urwid.WidgetWrap):
signals.pop_view_state.send(self) signals.pop_view_state.send(self)
return super().keypress(size, key) return super().keypress(size, key)
def make_help(self):
text = []
keys = [
("enter", "choose option"),
("esc", "exit chooser"),
]
text.extend(common.format_keyvals(keys, key="key", val="text", indent=4))
return text
class OptionsOverlay(urwid.WidgetWrap): class OptionsOverlay(urwid.WidgetWrap):
def __init__(self, master, name, vals): def __init__(self, master, name, vals, vspace):
"""
vspace: how much vertical space to keep clear
"""
cols, rows = master.ui.get_cols_rows() cols, rows = master.ui.get_cols_rows()
self.ge = grideditor.OptionsEditor(master, name, vals)
super().__init__( super().__init__(
urwid.AttrWrap( urwid.AttrWrap(
urwid.LineBox( urwid.LineBox(
urwid.BoxAdapter( urwid.BoxAdapter(self.ge, rows - vspace),
grideditor.OptionsEditor(master, name, vals), title=name
math.ceil(rows * 0.5)
),
title="text"
), ),
"background" "background"
) )
) )
self.width = math.ceil(cols * 0.8) self.width = math.ceil(cols * 0.8)
def make_help(self):
return self.ge.make_help()

View File

@ -30,7 +30,7 @@ call_in = blinker.Signal()
# Focus the body, footer or header of the main window # Focus the body, footer or header of the main window
focus = blinker.Signal() focus = blinker.Signal()
# Focus the body, footer or header of the main window # Set the mini help text in the footer of the main window
footer_help = blinker.Signal() footer_help = blinker.Signal()
# Fired when settings change # Fired when settings change