From a676e522d27b81de1770194a6cca340b4c62f1f6 Mon Sep 17 00:00:00 2001 From: champignoom Date: Thu, 24 Dec 2020 19:37:25 +0100 Subject: [PATCH] Add 'abort_backspace' config, False by default. If true, hitting backspace against an empty prompt aborts the prompt. --- vit/application.py | 3 ++- vit/command_bar.py | 50 ++++++++++++++++-------------------- vit/config/config.sample.ini | 3 +++ vit/config_parser.py | 1 + 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/vit/application.py b/vit/application.py index b4da14a..2521661 100644 --- a/vit/application.py +++ b/vit/application.py @@ -562,7 +562,8 @@ def init_autocomplete(self): self.autocomplete = AutoComplete(self.config, extra_filters={'report': self.reports.keys(), 'help': self.help.autocomplete_entries(), 'context': context_list}) def init_command_bar(self): - self.command_bar = CommandBar(autocomplete=self.autocomplete, event=self.event) + abort_backspace = self.config.get('vit', 'abort_backspace') + self.command_bar = CommandBar(autocomplete=self.autocomplete, abort_backspace=abort_backspace, event=self.event) def build_frame(self): self.status_report = urwid.AttrMap(urwid.Text('Welcome to VIT'), 'status') diff --git a/vit/command_bar.py b/vit/command_bar.py index a891e81..d71c05b 100644 --- a/vit/command_bar.py +++ b/vit/command_bar.py @@ -5,13 +5,12 @@ class CommandBar(urwid.Edit): """Custom urwid.Edit class for the command bar. """ def __init__(self, **kwargs): - self.event = kwargs['event'] - self.autocomplete = kwargs['autocomplete'] + self.event = kwargs.pop('event') + self.autocomplete = kwargs.pop('autocomplete') + self.abort_backspace = kwargs.pop('abort_backspace') self.metadata = None self.history = CommandBarHistory() self.readline = Readline(self) - kwargs.pop('event') - kwargs.pop('autocomplete') return super().__init__(**kwargs) def keypress(self, size, key): @@ -20,15 +19,7 @@ def keypress(self, size, key): if key not in ('tab', 'shift tab'): self.autocomplete.deactivate() if 'choices' in self.metadata: - op = self.metadata['op'] - data = { - 'choice': None, - 'metadata': self.get_metadata(), - } - if key in self.metadata['choices']: - data['choice'] = self.metadata['choices'][key] - self.cleanup(op) - self.event.emit('command-bar:keypress', data) + self.quit({'choice': self.metadata['choices'].get(key)}) return None elif key in ('up',): self.readline.keypress('ctrl p') @@ -38,16 +29,9 @@ def keypress(self, size, key): return None elif key in ('enter', 'esc'): text = self.get_edit_text().strip() - metadata = self.get_metadata() - data = { - 'key': key, - 'text': text, - 'metadata': metadata, - } - self.cleanup(metadata['op']) - if text and key in ('enter'): - self.history.add(metadata['history'], text) - self.event.emit('command-bar:keypress', data) + if text and key == 'enter': + self.history.add(self.metadata['history'], text) + self.quit({'key': key, 'text': text}) return None elif key in ('tab', 'shift tab'): if self.is_autocomplete_op(): @@ -59,8 +43,14 @@ def keypress(self, size, key): return None elif key in self.readline.keys(): return self.readline.keypress(key) + elif self.is_aborting_backspace(key): + self.quit({'key': key}) + return None return super().keypress(size, key) + def is_aborting_backspace(self, key): + return key == 'backspace' and self.abort_backspace and not self.get_edit_text() + def is_autocomplete_op(self): return self.metadata['op'] not in ['search-forward', 'search-reverse'] @@ -73,19 +63,23 @@ def set_edit_text(self, text, edit_pos=None): def set_command_prompt(self, caption, edit_text=None): self.set_caption(caption) - if edit_text: + if edit_text is not None: self.set_edit_text(edit_text) def activate(self, caption, metadata, edit_text=None): self.set_metadata(metadata) self.set_command_prompt(caption, edit_text) - def cleanup(self, command): - self.set_caption('') - self.set_edit_text('') - self.history.cleanup(command) + def deactivate(self): + self.set_command_prompt('', '') + self.history.cleanup(self.metadata['op']) self.set_metadata(None) + def quit(self, metadata_args={}): + data = {'metadata': self.get_metadata(), **metadata_args} + self.deactivate() + self.event.emit('command-bar:keypress', data) # remove focus from command bar + def get_metadata(self): return self.metadata.copy() if self.metadata else None diff --git a/vit/config/config.sample.ini b/vit/config/config.sample.ini index 11f5ff5..f3951a2 100644 --- a/vit/config/config.sample.ini +++ b/vit/config/config.sample.ini @@ -50,6 +50,9 @@ # list items. #mouse = False +# Boolean. If true, hitting backspace against an empty prompt aborts the prompt. +#abort_backspace = False + [report] # The default Taskwarrior report to load when VIT first starts, if no report diff --git a/vit/config_parser.py b/vit/config_parser.py index 349c583..da3ef99 100644 --- a/vit/config_parser.py +++ b/vit/config_parser.py @@ -44,6 +44,7 @@ 'confirmation': True, 'wait': True, 'mouse': False, + 'abort_backspace': False, }, 'report': { 'default_report': 'next',