refactor: extract key display mapping and helper methods
This commit is contained in:
@@ -6,28 +6,53 @@ from textual.screen import ModalScreen
|
|||||||
from textual.widgets import Static
|
from textual.widgets import Static
|
||||||
|
|
||||||
|
|
||||||
|
KEY_DISPLAY_MAP = {
|
||||||
|
"ctrl+": "^",
|
||||||
|
"left": "←",
|
||||||
|
"right": "→",
|
||||||
|
"up": "↑",
|
||||||
|
"down": "↓",
|
||||||
|
"space": "Space",
|
||||||
|
"enter": "Enter",
|
||||||
|
}
|
||||||
|
|
||||||
|
KEY_COLOR = "#f9e2af"
|
||||||
|
|
||||||
|
|
||||||
class HelpScreen(ModalScreen):
|
class HelpScreen(ModalScreen):
|
||||||
"""Help screen displaying all available keybindings."""
|
"""Help screen displaying all available keybindings."""
|
||||||
|
|
||||||
BINDINGS = [("escape", "dismiss", "Close"), ("?", "dismiss", "Close")]
|
BINDINGS = [("escape", "dismiss", "Close"), ("?", "dismiss", "Close")]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _format_key_display(key: str) -> str:
|
||||||
|
"""Format a key string for display with symbols."""
|
||||||
|
result = key
|
||||||
|
for old, new in KEY_DISPLAY_MAP.items():
|
||||||
|
result = result.replace(old, new)
|
||||||
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_binding(binding: tuple | object) -> tuple[str, str]:
|
||||||
|
"""Extract key and description from a binding."""
|
||||||
|
if isinstance(binding, tuple):
|
||||||
|
key, _, description = binding
|
||||||
|
else:
|
||||||
|
key = binding.key
|
||||||
|
description = binding.description
|
||||||
|
return key, description
|
||||||
|
|
||||||
def compose(self) -> ComposeResult:
|
def compose(self) -> ComposeResult:
|
||||||
with Container(id="help_container"):
|
with Container(id="help_container"):
|
||||||
yield Static("Key Bindings", id="help_title")
|
yield Static("Key Bindings", id="help_title")
|
||||||
with ScrollableContainer(id="help_content"):
|
with ScrollableContainer(id="help_content"):
|
||||||
bindings = self.app.BINDINGS
|
for binding in self.app.BINDINGS:
|
||||||
for binding in bindings:
|
key, description = self._parse_binding(binding)
|
||||||
if isinstance(binding, tuple):
|
key_display = self._format_key_display(key)
|
||||||
key, action, description = binding
|
|
||||||
else:
|
|
||||||
key = binding.key
|
|
||||||
description = binding.description
|
|
||||||
key_display = key.replace(
|
|
||||||
"ctrl+", "^").replace("left", "←").replace("right", "→").replace("up", "↑").replace("down", "↓").replace("space", "Space").replace("enter", "Enter")
|
|
||||||
with Horizontal(classes="help_row"):
|
with Horizontal(classes="help_row"):
|
||||||
yield Static(f"[bold #f9e2af]{key_display}[/]", classes="help_key")
|
yield Static(f"[bold {KEY_COLOR}]{key_display}[/]", classes="help_key")
|
||||||
yield Static(description, classes="help_action")
|
yield Static(description, classes="help_action")
|
||||||
yield Static("Press [bold #f9e2af]?[/] or [bold #f9e2af]Escape[/] to close", id="help_footer")
|
yield Static(f"Press [bold {KEY_COLOR}]?[/] or [bold {KEY_COLOR}]Escape[/] to close", id="help_footer")
|
||||||
|
|
||||||
def action_dismiss(self) -> None:
|
def action_dismiss(self) -> None:
|
||||||
self.dismiss()
|
self.dismiss()
|
||||||
|
|||||||
Reference in New Issue
Block a user