Skip to content

Commit

Permalink
Add 'abort_backspace' config, False by default.
Browse files Browse the repository at this point in the history
If true, hitting backspace against an empty prompt aborts the prompt.
  • Loading branch information
champignoom committed Dec 28, 2020
1 parent 2a7f2b6 commit a676e52
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
3 changes: 2 additions & 1 deletion vit/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
50 changes: 22 additions & 28 deletions vit/command_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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')
Expand All @@ -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():
Expand All @@ -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']

Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions vit/config/config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions vit/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'confirmation': True,
'wait': True,
'mouse': False,
'abort_backspace': False,
},
'report': {
'default_report': 'next',
Expand Down

0 comments on commit a676e52

Please sign in to comment.