diff --git a/changelog.md b/changelog.md index 52868439..04e6b391 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,7 @@ Features * Add prompt format strings for socket connections. * Optionally defer auto-completions until a minimum number of characters is typed. * Make the completion interface more responsive using a background thread. +* Option to suppress control-d exit behavior. Bug Fixes diff --git a/mycli/key_bindings.py b/mycli/key_bindings.py index 7f44856b..edb7b622 100644 --- a/mycli/key_bindings.py +++ b/mycli/key_bindings.py @@ -1,7 +1,13 @@ import logging +from prompt_toolkit.application.current import get_app from prompt_toolkit.enums import EditingMode -from prompt_toolkit.filters import completion_is_selected, control_is_searchable, emacs_mode +from prompt_toolkit.filters import ( + Condition, + completion_is_selected, + control_is_searchable, + emacs_mode, +) from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.key_binding.key_processor import KeyPressEvent @@ -11,6 +17,13 @@ _logger = logging.getLogger(__name__) +@Condition +def ctrl_d_condition() -> bool: + """Ctrl-D exit binding is only active when the buffer is empty.""" + app = get_app() + return not app.current_buffer.text + + def mycli_bindings(mycli) -> KeyBindings: """Custom key bindings for mycli.""" kb = KeyBindings() @@ -156,6 +169,16 @@ def _(event: KeyPressEvent) -> None: _logger.debug("Detected key.") search_history(event) + @kb.add('c-d', filter=ctrl_d_condition) + def _(event: KeyPressEvent) -> None: + """Exit mycli or ignore keypress.""" + _logger.debug('Detected key on empty line.') + mode = mycli.config.get('keys', {}).get('control_d', 'exit') + if mode == 'exit': + event.app.exit(exception=EOFError, style='class:exiting') + else: + event.app.output.bell() + @kb.add("enter", filter=completion_is_selected) def _(event: KeyPressEvent) -> None: """Makes the enter key work as the tab key only when showing the menu. diff --git a/mycli/myclirc b/mycli/myclirc index ab021eca..45557953 100644 --- a/mycli/myclirc +++ b/mycli/myclirc @@ -197,6 +197,10 @@ prompt_field_truncate = None prompt_section_truncate = None [keys] + +# possible values: exit, none +control_d = exit + # possible values: auto, fzf, reverse_isearch control_r = auto diff --git a/test/myclirc b/test/myclirc index f3e3bbd2..2b9a4454 100644 --- a/test/myclirc +++ b/test/myclirc @@ -195,6 +195,10 @@ prompt_field_truncate = None prompt_section_truncate = None [keys] + +# possible values: exit, none +control_d = exit + # possible values: auto, fzf, reverse_isearch control_r = auto