Skip to content

Commit

Permalink
Implement command for confirming command sequences.
Browse files Browse the repository at this point in the history
First attempt at implementing a confirmation command intended for use in
command sequences.

When called (optionally with a free-form string message), prompts the
user for confirmation. If the user declines, a SequenceCanceled
exception is raised, which trickles up the error handling into
apply_commandline where it is explicitly caught outside the common error
handler. This aborts the execution of subsequent commands in the
commandline, but does not show up as an error in the log.

Closes #1368
  • Loading branch information
MacGyverNL authored and pazz committed Jan 21, 2020
1 parent 03c198e commit a431837
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
7 changes: 7 additions & 0 deletions alot/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class CommandCanceled(Exception):
pass


class SequenceCanceled(Exception):
""" Exception triggered when a command sequence has been cancelled by the
confirmsequence command
"""
pass


COMMANDS = {
'search': {},
'envelope': {},
Expand Down
29 changes: 28 additions & 1 deletion alot/commands/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import urwid

from . import Command, registerCommand
from . import CommandCanceled
from . import CommandCanceled, SequenceCanceled
from .utils import update_keys
from .. import commands

Expand Down Expand Up @@ -1178,3 +1178,30 @@ def apply(self, ui):
# flush index
if self.flush:
ui.apply_command(commands.globals.FlushCommand())


@registerCommand(
MODE, 'confirmsequence',
arguments=[
(['msg'], {'help': 'Additional message to prompt',
'nargs': '*'})
],
help="prompt to confirm a sequence of commands")
class ConfirmCommand(Command):
"""Prompt user to confirm a sequence of commands."""

def __init__(self, msg=None, **kwargs):
"""
:param msg: Additional message to prompt the user with
:type msg: List[str]
"""
super(ConfirmCommand, self).__init__(**kwargs)
if not msg:
self.msg = "Confirm sequence?"
else:
self.msg = "Confirm sequence: {}?".format(" ".join(msg))

async def apply(self, ui):
if (await ui.choice(self.msg, select='yes', cancel='no',
msg_position='left')) == 'no':
raise SequenceCanceled()
12 changes: 10 additions & 2 deletions alot/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .buffers import SearchBuffer
from .commands import globals
from .commands import commandfactory
from .commands import CommandCanceled
from .commands import CommandCanceled, SequenceCanceled
from .commands import CommandParseError
from .helper import split_commandline
from .helper import string_decode
Expand Down Expand Up @@ -145,6 +145,10 @@ def _error_handler(self, exception):
self.notify(str(exception), priority='error')
elif isinstance(exception, CommandCanceled):
self.notify("operation cancelled", priority='error')
elif isinstance(exception, SequenceCanceled):
# This exception needs to trickle up to apply_commandline,
# then be handled explicitly.
raise exception
else:
logging.error(traceback.format_exc())
msg = "{}\n(check the log for details)".format(exception)
Expand Down Expand Up @@ -267,7 +271,11 @@ def apply_this_command(cmdstring):
for c in split_commandline(cmdline):
await apply_this_command(c)
except Exception as e:
self._error_handler(e)
if isinstance(e, SequenceCanceled):
self.notify("sequence of operations cancelled",
priority='error')
else:
self._error_handler(e)

@staticmethod
def _unhandled_input(key):
Expand Down
10 changes: 10 additions & 0 deletions docs/source/usage/modes/global.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ The following commands are available globally:
:---omit_signature: do not add signature
:---spawn: spawn editor in new terminal

.. _cmd.global.confirmsequence:

.. describe:: confirmsequence

prompt to confirm a sequence of commands

argument
Additional message to prompt


.. _cmd.global.exit:

.. describe:: exit
Expand Down

0 comments on commit a431837

Please sign in to comment.