console: refactor console.flowview.mode

We now have:

console.flowview.mode           gets the current mode
console.flowview.mode.options   gets the possible options for the mode
console.flowview.mode.set       sets the flowview mode

This is quite a common pattern in our commands, to the point where we should
formalise it.

Fixes #2655
This commit is contained in:
Aldo Cortesi 2017-12-12 09:06:58 +13:00
parent 322af02eb2
commit 91a2979694
2 changed files with 27 additions and 13 deletions

View File

@ -431,26 +431,32 @@ class ConsoleAddon:
self._grideditor().cmd_spawn_editor() self._grideditor().cmd_spawn_editor()
@command.command("console.flowview.mode.set") @command.command("console.flowview.mode.set")
def flowview_mode_set(self) -> None: def flowview_mode_set(self, mode: str) -> None:
""" """
Set the display mode for the current flow view. Set the display mode for the current flow view.
""" """
fv = self.master.window.current("flowview") fv = self.master.window.current_window("flowview")
if not fv: if not fv:
raise exceptions.CommandError("Not viewing a flow.") raise exceptions.CommandError("Not viewing a flow.")
idx = fv.body.tab_offset idx = fv.body.tab_offset
def callback(opt): if mode not in [i.name.lower() for i in contentviews.views]:
try: raise exceptions.CommandError("Invalid flowview mode.")
self.master.commands.call_args(
"view.setval",
["@focus", "flowview_mode_%s" % idx, opt]
)
except exceptions.CommandError as e:
signals.status_message.send(message=str(e))
opts = [i.name.lower() for i in contentviews.views] try:
self.master.overlay(overlay.Chooser(self.master, "Mode", opts, "", callback)) self.master.commands.call_args(
"view.setval",
["@focus", "flowview_mode_%s" % idx, mode]
)
except exceptions.CommandError as e:
signals.status_message.send(message=str(e))
@command.command("console.flowview.mode.options")
def flowview_mode_options(self) -> typing.Sequence[str]:
"""
Returns the valid options for the flowview mode.
"""
return [i.name.lower() for i in contentviews.views]
@command.command("console.flowview.mode") @command.command("console.flowview.mode")
def flowview_mode(self) -> str: def flowview_mode(self) -> str:

View File

@ -116,7 +116,15 @@ def map(km):
"View flow body in an external viewer" "View flow body in an external viewer"
) )
km.add("p", "view.focus.prev", ["flowview"], "Go to previous flow") km.add("p", "view.focus.prev", ["flowview"], "Go to previous flow")
km.add("m", "console.flowview.mode.set", ["flowview"], "Set flow view mode") km.add(
"m",
"""
console.choose.cmd Mode console.flowview.mode.options
console.flowview.mode.set {choice}
""",
["flowview"],
"Set flow view mode"
)
km.add( km.add(
"z", "z",
""" """