• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14# pylint: skip-file
15"""Console key bindings."""
16import logging
17
18from prompt_toolkit.filters import (
19    Condition,
20    has_focus,
21)
22from prompt_toolkit.key_binding import KeyBindings
23from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
24from prompt_toolkit.key_binding.key_bindings import Binding
25
26
27__all__ = ('create_key_bindings',)
28
29_LOG = logging.getLogger(__package__)
30
31DEFAULT_KEY_BINDINGS: dict[str, list[str]] = {
32    'global.open-user-guide': ['f1'],
33    'global.open-menu-search': ['c-p'],
34    'global.focus-previous-widget': ['c-left'],
35    'global.focus-next-widget': ['c-right', 's-tab'],
36    'global.exit-no-confirmation': ['c-x c-c'],
37    'global.exit-with-confirmation': ['c-d'],
38    'log-pane.shift-line-to-top': ['z t'],
39    'log-pane.shift-line-to-center': ['z z'],
40    'log-pane.toggle-follow': ['f'],
41    'log-pane.toggle-wrap-lines': ['w'],
42    'log-pane.toggle-table-view': ['t'],
43    'log-pane.duplicate-log-pane': ['insert'],
44    'log-pane.remove-duplicated-log-pane': ['delete'],
45    'log-pane.clear-history': ['C'],
46    'log-pane.toggle-follow': ['f'],
47    'log-pane.toggle-web-browser': ['O'],
48    'log-pane.move-cursor-up': ['up', 'k'],
49    'log-pane.move-cursor-down': ['down', 'j'],
50    'log-pane.visual-select-up': ['s-up'],
51    'log-pane.visual-select-down': ['s-down'],
52    'log-pane.visual-select-all': ['c-r'],
53    'log-pane.deselect-cancel-search': ['c-c'],
54    'log-pane.scroll-page-up': ['pageup'],
55    'log-pane.scroll-page-down': ['pagedown'],
56    'log-pane.scroll-to-top': ['g'],
57    'log-pane.scroll-to-bottom': ['G'],
58    'log-pane.save-copy': ['c-o'],
59    'log-pane.search': ['/', 'c-f'],
60    'log-pane.search-next-match': ['n', 'c-s', 'c-g'],
61    'log-pane.search-previous-match': ['N', 'c-r'],
62    'log-pane.search-apply-filter': ['escape c-f'],
63    'log-pane.clear-filters': ['escape c-r'],
64    'search-toolbar.toggle-column': ['c-t'],
65    'search-toolbar.toggle-invert': ['c-v'],
66    'search-toolbar.toggle-matcher': ['c-n'],
67    'search-toolbar.cancel': ['escape', 'c-c', 'c-d'],
68    'search-toolbar.create-filter': ['escape c-f'],
69    'window-manager.move-pane-left': ['escape c-left'],  # Alt-Ctrl-
70    'window-manager.move-pane-right': ['escape c-right'],  # Alt-Ctrl-
71    # NOTE: c-up and c-down seem swapped in prompt-toolkit
72    'window-manager.move-pane-down': ['escape c-up'],  # Alt-Ctrl-
73    'window-manager.move-pane-up': ['escape c-down'],  # Alt-Ctrl-
74    'window-manager.enlarge-pane': ['escape ='],  # Alt-= (mnemonic: Alt Plus)
75    'window-manager.shrink-pane': [
76        'escape -'
77    ],  # Alt-minus (mnemonic: Alt Minus)
78    'window-manager.shrink-split': ['escape ,'],  # Alt-, (mnemonic: Alt <)
79    'window-manager.enlarge-split': ['escape .'],  # Alt-. (mnemonic: Alt >)
80    'window-manager.focus-prev-pane': ['escape c-p'],  # Ctrl-Alt-p
81    'window-manager.focus-next-pane': ['escape c-n'],  # Ctrl-Alt-n
82    'window-manager.balance-window-panes': ['c-u'],
83    'python-repl.copy-output-selection': ['c-c'],
84    'python-repl.copy-all-output': ['escape c-c'],
85    'python-repl.copy-clear-or-cancel': ['c-c'],
86    'python-repl.paste-to-input': ['c-v'],
87    'python-repl.history-search': ['c-r'],
88    'python-repl.snippet-search': ['c-t'],
89    'save-as-dialog.cancel': ['escape', 'c-c', 'c-d'],
90    'quit-dialog.no': ['escape', 'n', 'c-c'],
91    'quit-dialog.yes': ['y', 'c-d'],
92    'command-runner.cancel': ['escape', 'c-c'],
93    'command-runner.select-previous-item': ['up', 's-tab'],
94    'command-runner.select-next-item': ['down', 'tab'],
95    'help-window.close': ['q', 'f1', 'escape'],
96    'help-window.copy-all': ['c-c'],
97}
98
99
100def create_key_bindings(console_app) -> KeyBindings:
101    """Create custom key bindings.
102
103    This starts with the key bindings, defined by `prompt-toolkit`, but adds the
104    ones which are specific for the console_app. A console_app instance
105    reference is passed in so key bind functions can access it.
106    """
107
108    key_bindings = KeyBindings()
109    register = console_app.prefs.register_keybinding
110
111    @register(
112        'global.open-user-guide',
113        key_bindings,
114        filter=Condition(lambda: not console_app.modal_window_is_open()),
115    )
116    def show_help(event):
117        """Toggle user guide window."""
118        console_app.user_guide_window.toggle_display()
119
120    # F2 is ptpython settings
121    # F3 is ptpython history
122
123    @register(
124        'global.open-menu-search',
125        key_bindings,
126        filter=Condition(lambda: not console_app.modal_window_is_open()),
127    )
128    def show_command_runner(event):
129        """Open command runner window."""
130        console_app.open_command_runner_main_menu()
131
132    @register('global.focus-previous-widget', key_bindings)
133    def app_focus_previous(event):
134        """Move focus to the previous widget."""
135        focus_previous(event)
136
137    @register('global.focus-next-widget', key_bindings)
138    def app_focus_next(event):
139        """Move focus to the next widget."""
140        focus_next(event)
141
142    # Bindings for when the ReplPane input field is in focus.
143    # These are hidden from help window global keyboard shortcuts since the
144    # method names end with `_hidden`.
145    @register(
146        'python-repl.copy-clear-or-cancel',
147        key_bindings,
148        filter=has_focus(console_app.pw_ptpython_repl),
149    )
150    def handle_ctrl_c_hidden(event):
151        """Reset the python repl on Ctrl-c"""
152        console_app.repl_pane.ctrl_c()
153
154    @register('global.exit-no-confirmation', key_bindings)
155    def quit_no_confirm(event):
156        """Quit without confirmation."""
157        event.app.exit()
158
159    @register(
160        'global.exit-with-confirmation',
161        key_bindings,
162        filter=console_app.pw_ptpython_repl.input_empty_if_in_focus_condition()
163        | has_focus(console_app.quit_dialog),
164    )
165    def quit(event):
166        """Quit with confirmation dialog."""
167        # If the python repl is in focus and has text input then Ctrl-d will
168        # delete forward characters instead.
169        console_app.quit_dialog.open_dialog()
170
171    @register(
172        'python-repl.paste-to-input',
173        key_bindings,
174        filter=has_focus(console_app.pw_ptpython_repl),
175    )
176    def paste_into_repl(event):
177        """Reset the python repl on Ctrl-c"""
178        console_app.repl_pane.paste_system_clipboard_to_input_buffer()
179
180    @register(
181        'python-repl.history-search',
182        key_bindings,
183        filter=has_focus(console_app.pw_ptpython_repl),
184    )
185    def history_search(event):
186        """Open the repl history search dialog."""
187        console_app.open_command_runner_history()
188
189    @register(
190        'python-repl.snippet-search',
191        key_bindings,
192        filter=has_focus(console_app.pw_ptpython_repl),
193    )
194    def insert_snippet(event):
195        """Open the repl snippet search dialog."""
196        console_app.open_command_runner_snippets()
197
198    @register(
199        'python-repl.copy-all-output',
200        key_bindings,
201        filter=console_app.repl_pane.input_or_output_has_focus(),
202    )
203    def copy_repl_output_text(event):
204        """Copy all Python output to the system clipboard."""
205        console_app.repl_pane.copy_all_output_text()
206
207    return key_bindings
208