Add shortcut help popup

This commit is contained in:
2026-09-18 14:32:58 +00:00
parent 6e420b2db5
commit a5e8e32f1e
5 changed files with 268 additions and 8 deletions
+55
View File
@@ -0,0 +1,55 @@
from __future__ import annotations
import argparse
import curses
import unittest
from unittest.mock import Mock, patch
from herdr_hub.cli import build_parser, command_keys
from herdr_hub.key_help import KEY_SECTIONS, KeyHelpPopup
class KeyHelpTests(unittest.TestCase):
def test_reference_contains_primary_hub_mappings(self) -> None:
mappings = {
mapping.keys: mapping.action
for _, section in KEY_SECTIONS
for mapping in section
}
for keys in (
"Ctrl+A ?",
"Ctrl+A g",
"Ctrl+A h",
"Ctrl+A r",
"Ctrl+A R",
"Ctrl+A q",
"Ctrl+A d",
):
self.assertIn(keys, mappings)
def test_narrow_layout_stacks_keys_and_actions(self) -> None:
wide = [value for value, _ in KeyHelpPopup.lines(70)]
narrow = [value for value, _ in KeyHelpPopup.lines(36)]
self.assertGreater(len(narrow), len(wide))
self.assertIn(" Ctrl+A ?", narrow)
self.assertIn(" 打开快捷键帮助", narrow)
def test_escape_closes_popup(self) -> None:
screen = Mock()
screen.getmaxyx.return_value = (24, 80)
screen.get_wch.return_value = "\x1b"
with patch("curses.set_escdelay"), patch("curses.curs_set"), patch(
"curses.use_default_colors"
), patch("curses.mousemask"):
self.assertEqual(KeyHelpPopup().run(screen), 0)
def test_cli_entry_uses_curses_wrapper(self) -> None:
arguments = build_parser().parse_args(["keys"])
self.assertEqual(arguments.command, "keys")
with patch("herdr_hub.cli.curses.wrapper", return_value=0) as wrapper:
self.assertEqual(command_keys(argparse.Namespace()), 0)
wrapper.assert_called_once()
if __name__ == "__main__":
unittest.main()
+15 -2
View File
@@ -74,6 +74,19 @@ class TmuxIntegrationTests(unittest.TestCase):
self.assertEqual(self.hub.run(["show-options", "-gv", "prefix2"]).stdout.strip(), "None")
self.assertIn("choose", self.hub.run(["list-keys", "-T", "prefix", "g"]).stdout)
def test_help_popup_replaces_status_shortcut_list(self) -> None:
self.hub.ensure_server()
binding = self.hub.run(["list-keys", "-T", "prefix", "?"]).stdout
self.assertIn("display-popup", binding)
self.assertIn(" keys", binding)
status_right = self.hub.run(
["show-options", "-gv", "status-right"]
).stdout
self.assertIn("C-a ?", status_right)
self.assertNotIn("choose", status_right)
self.assertNotIn("refresh", status_right)
self.assertNotIn("C-a R", status_right)
def test_configures_isolated_server_and_adds_without_duplicates(self) -> None:
self.assertTrue(self.hub.ensure_server())
endpoints = [Endpoint.local(), Endpoint.remote("orangepi")]
@@ -136,9 +149,9 @@ class TmuxIntegrationTests(unittest.TestCase):
self.assertEqual(position, "top")
self.assertEqual(mouse, "on")
self.assertEqual(clipboard, "on")
self.assertIn("连接已断开", status_right)
self.assertIn("C-a ?", status_right)
self.assertTrue(expanded_status_left.startswith("#[bold,fg=colour81]"))
self.assertIn("C-a R", status_right)
self.assertNotIn("C-a R", status_right)
self.assertIn("连接已断开", dead_format)
self.assertIn("Ctrl+A R", dead_format)
self.assertIn("client_width", inactive_window_format)