Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion mycli/key_bindings.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -156,6 +169,16 @@ def _(event: KeyPressEvent) -> None:
_logger.debug("Detected <alt-r> key.")
search_history(event)

@kb.add('c-d', filter=ctrl_d_condition)
def _(event: KeyPressEvent) -> None:
"""Exit mycli or ignore keypress."""
_logger.debug('Detected <C-d> 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.
Expand Down
4 changes: 4 additions & 0 deletions mycli/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions test/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down