pw_console: Add styles and key bindings
This adds the necessary prompt_toolkit boiler plate for some barebones
colors styles and key bindings.
No-Docs-Update-Reason: prompt_toolkit UI boilerplate
Change-Id: I29b5a7f82bbc412f3ada3a52bebefa1576136704
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/48766
Pigweed-Auto-Submit: Anthony DiGirolamo <tonymd@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Keir Mierle <keir@google.com>
Reviewed-by: Joe Ethier <jethier@google.com>
diff --git a/pw_console/py/BUILD.gn b/pw_console/py/BUILD.gn
index f8952f1..e6b2848 100644
--- a/pw_console/py/BUILD.gn
+++ b/pw_console/py/BUILD.gn
@@ -22,6 +22,8 @@
"pw_console/__init__.py",
"pw_console/__main__.py",
"pw_console/console_app.py",
+ "pw_console/key_bindings.py",
+ "pw_console/style.py",
]
tests = [ "console_app_test.py" ]
python_deps = [
diff --git a/pw_console/py/pw_console/key_bindings.py b/pw_console/py/pw_console/key_bindings.py
new file mode 100644
index 0000000..3579d17
--- /dev/null
+++ b/pw_console/py/pw_console/key_bindings.py
@@ -0,0 +1,74 @@
+# Copyright 2021 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+# pylint: skip-file
+"""Console key bindings."""
+import logging
+
+from prompt_toolkit.filters import (
+ Condition,
+ has_focus,
+)
+from prompt_toolkit.key_binding import KeyBindings
+from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
+
+__all__ = ('create_key_bindings', )
+
+_LOG = logging.getLogger(__package__)
+
+
+def create_key_bindings(console_app):
+ """Create custom key bindings.
+
+ This starts with the key bindings, defined by `prompt-toolkit`, but adds the
+ ones which are specific for the console_app. A console_app instance
+ reference is passed in so key bind functions can access it.
+ """
+
+ bindings = KeyBindings()
+
+ @bindings.add('f1')
+ def show_help(event):
+ """Toggle help window."""
+ console_app.toggle_help()
+
+ @bindings.add('q', filter=Condition(lambda: console_app.show_help_window))
+ def close_help_window(event):
+ """Hide help window."""
+ console_app.toggle_help()
+
+ @bindings.add('f2')
+ def toggle_vertical_split(event):
+ """Toggle horizontal and vertical window splitting."""
+ console_app.toggle_vertical_split()
+
+ @bindings.add('c-w')
+ @bindings.add('c-q')
+ def exit_(event):
+ """Quit the console application."""
+ event.app.exit()
+
+ @bindings.add('s-tab')
+ @bindings.add('c-right')
+ @bindings.add('c-down')
+ def app_focus_next(event):
+ """Move focus to the next widget."""
+ focus_next(event)
+
+ @bindings.add('c-left')
+ @bindings.add('c-up')
+ def app_focus_previous(event):
+ """Move focus to the previous widget."""
+ focus_previous(event)
+
+ return bindings
diff --git a/pw_console/py/pw_console/style.py b/pw_console/py/pw_console/style.py
new file mode 100644
index 0000000..471911d
--- /dev/null
+++ b/pw_console/py/pw_console/style.py
@@ -0,0 +1,55 @@
+# Copyright 2021 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+"""UI Color Styles for ConsoleApp."""
+
+from prompt_toolkit.styles import Style
+
+TOOLBAR_STYLE = 'bg:#fdd1ff #432445'
+
+pw_console_styles = Style.from_dict({
+ 'bottom_toolbar_colored_background': TOOLBAR_STYLE,
+ 'bottom_toolbar': TOOLBAR_STYLE,
+ 'bottom_toolbar_colored_text': TOOLBAR_STYLE,
+
+ # FloatingMessageBar style
+ 'message': 'bg:#282c34 #c678dd',
+
+ # prompt_toolkit scrollbar styles:
+ 'scrollbar.background': 'bg:#3e4452 #abb2bf',
+ 'scrollbar.button': 'bg:#7f3285 #282c34',
+ # Unstyled scrollbar classes:
+ # 'scrollbar.arrow'
+ # 'scrollbar.start'
+ # 'scrollbar.end'
+
+ # Top menu bar styles
+ 'menu': 'bg:#3e4452 #bbc2cf',
+ 'menu-bar.selected-item': 'bg:#61afef #282c34',
+ 'menu-border': 'bg:#282c34 #61afef',
+ 'menu-bar': TOOLBAR_STYLE,
+
+ # Top bar logo + keyboard shortcuts
+ 'logo': TOOLBAR_STYLE + ' bold',
+ 'keybind': TOOLBAR_STYLE,
+ 'keyhelp': TOOLBAR_STYLE,
+
+ # Help window styles
+ 'help_window_content': 'bg:default default',
+ 'frame.border': '',
+ 'shadow': 'bg:#282c34',
+
+ # Highlighted line style
+ 'cursor-line': 'bg:#3e4452 nounderline',
+ 'selected-log-line': 'bg:#3e4452',
+}) # yapf: disable