From fa51908b77f3209704f5834d59c352a755d137db Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 17 Jan 2017 15:09:40 -0800 Subject: commands/global: Don't double prompt to close last buffer Currently if global.quit_on_last_bclose is set to True (not the default), and the last buffer is an envelope, then when 'd' or ':bclose' is called, the user will be prompted twice to 'close without sending?'. This patch fixes that by skipping the prompt in BufferCloseCommand, if there is 1 buffer, and global.quit_on_last_bclose is True. It does this by adding a private keyword to the ExitCommand() constructor, telling that classes apply method to not prompt. Fixes #979 --- alot/commands/globals.py | 66 +++++++++++++++++++++++--------------- docs/source/usage/modes/global.rst | 7 +++- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/alot/commands/globals.py b/alot/commands/globals.py index 9c060bfb..91808ade 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -38,8 +38,17 @@ MODE = 'global' @registerCommand(MODE, 'exit') class ExitCommand(Command): + """Shut down cleanly. + + The _prompt variable is for internal use only, it's used to control + prompting to close without sending, and is used by the BufferCloseCommand + if settings change after yielding to the UI. + """ + + def __init__(self, _prompt=True, **kwargs): + super(ExitCommand, self).__init__(**kwargs) + self.prompt_to_send = _prompt - """shut down cleanly""" @inlineCallbacks def apply(self, ui): if settings.get('bug_on_exit'): @@ -49,13 +58,14 @@ class ExitCommand(Command): return # check if there are any unsent messages - for buffer in ui.buffers: - if (isinstance(buffer, buffers.EnvelopeBuffer) and - not buffer.envelope.sent_time): - if (yield ui.choice('quit without sending message?', - select='yes', cancel='no', - msg_position='left')) == 'no': - raise CommandCanceled() + if self.prompt_to_send: + for buffer in ui.buffers: + if (isinstance(buffer, buffers.EnvelopeBuffer) and + not buffer.envelope.sent_time): + if (yield ui.choice('quit without sending message?', + select='yes', cancel='no', + msg_position='left')) == 'no': + raise CommandCanceled() for b in ui.buffers: b.cleanup() @@ -414,22 +424,30 @@ class BufferCloseCommand(Command): @inlineCallbacks def apply(self, ui): - def last_buffer_no_close(): - """Helper that prints a message to the log and notifies if this is - the last buffer and the settings don't allow closebuffer to - exit.""" - msg = ('not closing last remaining buffer as ' - 'global.quit_on_last_bclose is set to False') - logging.info(msg) - ui.notify(msg, priority='error') + def one_buffer(prompt=True): + """Helper to handle the case on only one buffer being opened. + + prompt is a boolean that is passed to ExitCommand() as the _prompt + keyword argument. + """ + # If there is only one buffer and the settings don't allow using + # closebuffer to exit, then just stop. + if not settings.get('quit_on_last_bclose'): + msg = ('not closing last remaining buffer as ' + 'global.quit_on_last_bclose is set to False') + logging.info(msg) + ui.notify(msg, priority='error') + # Otherwise pass directly to exit command, which also prommpts for + # 'close without sending' + else: + logging.info('closing the last buffer, exiting') + ui.apply_command(ExitCommand(_prompt=prompt)) if self.buffer is None: self.buffer = ui.current_buffer - # If there is only one buffer and the settings don't allow using - # closebuffer to exit, then just stop. - if len(ui.buffers) == 1 and not settings.get('quit_on_last_bclose'): - last_buffer_no_close() + if len(ui.buffers) == 1: + one_buffer() return if (isinstance(self.buffer, buffers.EnvelopeBuffer) and @@ -441,13 +459,9 @@ class BufferCloseCommand(Command): raise CommandCanceled() # Because we yield above it is possible that the settings or the number - # of buffer chould change, so retest + # of buffers chould change, so retest. if len(ui.buffers) == 1: - if settings.get('quit_on_last_bclose'): - logging.info('closing the last buffer, exiting') - ui.apply_command(ExitCommand()) - else: - last_buffer_no_close() + one_buffer(prompt=False) else: ui.buffer_close(self.buffer, self.redraw) diff --git a/docs/source/usage/modes/global.rst b/docs/source/usage/modes/global.rst index a5526baa..f9f0b880 100644 --- a/docs/source/usage/modes/global.rst +++ b/docs/source/usage/modes/global.rst @@ -134,7 +134,12 @@ The following commands are available globally .. describe:: exit - shut down cleanly + Shut down cleanly. + + The _prompt variable is for internal use only, it's used to control + prompting to close without sending, and is used by the BufferCloseCommand + if settings change after yielding to the UI. + .. _cmd.global.flush: -- cgit v1.2.3