forked from dbcli/mycli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_binding_actions.py
More file actions
63 lines (52 loc) · 2.02 KB
/
key_binding_actions.py
File metadata and controls
63 lines (52 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import annotations
import logging
from typing import Any
import webbrowser
import prompt_toolkit
from prompt_toolkit.application.current import get_app
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from mycli.constants import DOCS_URL
from mycli.packages.toolkit.utils import safe_invalidate_display
_logger = logging.getLogger(__name__)
class KeyBindingActions:
def __init__(self, mycli: Any) -> None:
self._mycli = mycli
@staticmethod
def _print_docs_help() -> None:
app = get_app()
app.print_text('\n')
app.print_text([
('', 'Inline help — type "'),
('bold', 'help'),
('', '" or "'),
('bold', r'\?'),
('', '"\n'),
])
app.print_text([
('', 'Docs index — '),
('bold', DOCS_URL),
('', '\n'),
])
app.print_text('\n')
def open_docs(self, event: KeyPressEvent, message: str) -> None:
_logger.debug(message)
webbrowser.open_new_tab(DOCS_URL)
prompt_toolkit.application.run_in_terminal(self._print_docs_help)
safe_invalidate_display(event.app)
def toggle_smart_completion(self, message: str) -> None:
_logger.debug(message)
self._mycli.completer.smart_completion = not self._mycli.completer.smart_completion
def toggle_multiline(self, message: str) -> None:
_logger.debug(message)
self._mycli.multi_line = not self._mycli.multi_line
def toggle_editing_mode(self, event: KeyPressEvent, message: str) -> None:
_logger.debug(message)
if self._mycli.key_bindings == "vi":
event.app.editing_mode = EditingMode.EMACS
self._mycli.key_bindings = "emacs"
event.app.ttimeoutlen = self._mycli.emacs_ttimeoutlen
else:
event.app.editing_mode = EditingMode.VI
self._mycli.key_bindings = "vi"
event.app.ttimeoutlen = self._mycli.vi_ttimeoutlen