• 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
17from typing import Dict, List
18
19from prompt_toolkit.filters import (
20    Condition,
21    has_focus,
22)
23from prompt_toolkit.key_binding import KeyBindings
24from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
25from prompt_toolkit.key_binding.key_bindings import Binding
26
27import pw_console.pw_ptpython_repl
28
29__all__ = ('create_key_bindings', )
30
31_LOG = logging.getLogger(__package__)
32
33DEFAULT_KEY_BINDINGS: Dict[str, List[str]] = {
34    'global.open-user-guide': ['f1'],
35    'global.open-menu-search': ['c-p'],
36    'global.focus-previous-widget': ['c-left'],
37    'global.focus-next-widget': ['c-right', 's-tab'],
38    'global.exit-no-confirmation': ['c-x c-c'],
39    'global.exit-with-confirmation': ['c-d'],
40    'log-pane.shift-line-to-top': ['z t'],
41    'log-pane.shift-line-to-center': ['z z'],
42    'log-pane.toggle-follow': ['f'],
43    'log-pane.toggle-wrap-lines': ['w'],
44    'log-pane.toggle-table-view': ['t'],
45    'log-pane.duplicate-log-pane': ['insert'],
46    'log-pane.remove-duplicated-log-pane': ['delete'],
47    'log-pane.clear-history': ['C'],
48    'log-pane.toggle-follow': ['f'],
49    'log-pane.move-cursor-up': ['up', 'k'],
50    'log-pane.move-cursor-down': ['down', 'j'],
51    'log-pane.visual-select-up': ['s-up'],
52    'log-pane.visual-select-down': ['s-down'],
53    'log-pane.visual-select-all': ['N', 'c-r'],
54    'log-pane.deselect-cancel-search': ['c-c'],
55    'log-pane.scroll-page-up': ['pageup'],
56    'log-pane.scroll-page-down': ['pagedown'],
57    'log-pane.scroll-to-top': ['g'],
58    'log-pane.scroll-to-bottom': ['G'],
59    'log-pane.save-copy': ['c-o'],
60    'log-pane.search': ['/', 'c-f'],
61    'log-pane.search-next-match': ['n', 'c-s', 'c-g'],
62    'log-pane.search-previous-match': ['N', 'c-r'],
63    'log-pane.search-apply-filter': ['escape c-f'],
64    'log-pane.clear-filters': ['escape c-r'],
65    'search-toolbar.toggle-column': ['c-t'],
66    'search-toolbar.toggle-invert': ['c-v'],
67    'search-toolbar.toggle-matcher': ['c-n'],
68    'search-toolbar.cancel': ['escape', 'c-c', 'c-d'],
69    'search-toolbar.create-filter': ['escape c-f'],
70    'window-manager.move-pane-left': ['escape c-left'],  # Alt-Ctrl-
71    'window-manager.move-pane-right': ['escape c-right'],  # Alt-Ctrl-
72    # NOTE: c-up and c-down seem swapped in prompt-toolkit
73    'window-manager.move-pane-down': ['escape c-up'],  # Alt-Ctrl-
74    'window-manager.move-pane-up': ['escape c-down'],  # Alt-Ctrl-
75    'window-manager.enlarge-pane': ['escape ='],  # Alt-= (mnemonic: Alt Plus)
76    'window-manager.shrink-pane':
77    ['escape -'],  # 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    'save-as-dialog.cancel': ['escape', 'c-c', 'c-d'],
88    'quit-dialog.no': ['escape', 'n', 'c-c'],
89    'quit-dialog.yes': ['y', 'c-d'],
90    'command-runner.cancel': ['escape', 'c-c'],
91    'command-runner.select-previous-item': ['up', 's-tab'],
92    'command-runner.select-next-item': ['down', 'tab'],
93    'help-window.close': ['q', 'f1', 'escape'],
94    'help-window.copy-all': ['c-c'],
95}
96
97
98def create_key_bindings(console_app) -> KeyBindings:
99    """Create custom key bindings.
100
101    This starts with the key bindings, defined by `prompt-toolkit`, but adds the
102    ones which are specific for the console_app. A console_app instance
103    reference is passed in so key bind functions can access it.
104    """
105
106    key_bindings = KeyBindings()
107    register = console_app.prefs.register_keybinding
108
109    @register('global.open-user-guide',
110              key_bindings,
111              filter=Condition(lambda: not console_app.modal_window_is_open()))
112    def show_help(event):
113        """Toggle user guide window."""
114        console_app.user_guide_window.toggle_display()
115
116    # F2 is ptpython settings
117    # F3 is ptpython history
118
119    @register('global.open-menu-search',
120              key_bindings,
121              filter=Condition(lambda: not console_app.modal_window_is_open()))
122    def show_command_runner(event):
123        """Open command runner window."""
124        console_app.open_command_runner_main_menu()
125
126    @register('global.focus-previous-widget', key_bindings)
127    def app_focus_previous(event):
128        """Move focus to the previous widget."""
129        focus_previous(event)
130
131    @register('global.focus-next-widget', key_bindings)
132    def app_focus_next(event):
133        """Move focus to the next widget."""
134        focus_next(event)
135
136    # Bindings for when the ReplPane input field is in focus.
137    # These are hidden from help window global keyboard shortcuts since the
138    # method names end with `_hidden`.
139    @register('python-repl.copy-clear-or-cancel',
140              key_bindings,
141              filter=has_focus(console_app.pw_ptpython_repl))
142    def handle_ctrl_c_hidden(event):
143        """Reset the python repl on Ctrl-c"""
144        console_app.repl_pane.ctrl_c()
145
146    @register('global.exit-no-confirmation', key_bindings)
147    def quit_no_confirm(event):
148        """Quit without confirmation."""
149        event.app.exit()
150
151    @register(
152        'global.exit-with-confirmation',
153        key_bindings,
154        filter=console_app.pw_ptpython_repl.input_empty_if_in_focus_condition(
155        ) | has_focus(console_app.quit_dialog))
156    def quit(event):
157        """Quit with confirmation dialog."""
158        # If the python repl is in focus and has text input then Ctrl-d will
159        # delete forward characters instead.
160        console_app.quit_dialog.open_dialog()
161
162    @register('python-repl.paste-to-input',
163              key_bindings,
164              filter=has_focus(console_app.pw_ptpython_repl))
165    def paste_into_repl(event):
166        """Reset the python repl on Ctrl-c"""
167        console_app.repl_pane.paste_system_clipboard_to_input_buffer()
168
169    @register('python-repl.copy-all-output',
170              key_bindings,
171              filter=console_app.repl_pane.input_or_output_has_focus())
172    def copy_repl_output_text(event):
173        """Copy all Python output to the system clipboard."""
174        console_app.repl_pane.copy_all_output_text()
175
176    return key_bindings
177