From 51152c719da2afb7294603cbe4c4427914fb4aa5 Mon Sep 17 00:00:00 2001 From: FujiApple Date: Tue, 3 Oct 2023 22:16:09 +0800 Subject: [PATCH] feat: add support for the `ToggleHelpAlt` command which defaults to the `?` key (#694) --- src/config/binding.rs | 8 ++++++++ src/config/file.rs | 1 + src/frontend.rs | 4 +++- src/frontend/binding.rs | 2 ++ src/frontend/render/help.rs | 2 +- src/frontend/render/settings.rs | 3 ++- trippy-config-sample.toml | 1 + 7 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/config/binding.rs b/src/config/binding.rs index 8f0e67f72..b5e630e61 100644 --- a/src/config/binding.rs +++ b/src/config/binding.rs @@ -11,6 +11,7 @@ use strum::{AsRefStr, EnumString, EnumVariantNames}; #[derive(Debug, Clone, Copy)] pub struct TuiBindings { pub toggle_help: TuiKeyBinding, + pub toggle_help_alt: TuiKeyBinding, pub toggle_settings: TuiKeyBinding, pub previous_hop: TuiKeyBinding, pub next_hop: TuiKeyBinding, @@ -45,6 +46,7 @@ impl TuiBindings { pub fn find_duplicates(&self) -> Vec { let (_, duplicates) = [ (self.toggle_help, TuiCommandItem::ToggleHelp), + (self.toggle_help_alt, TuiCommandItem::ToggleHelpAlt), (self.toggle_settings, TuiCommandItem::ToggleSettings), (self.previous_hop, TuiCommandItem::PreviousHop), (self.next_hop, TuiCommandItem::NextHop), @@ -104,6 +106,10 @@ impl From<(HashMap, ConfigBindings)> for TuiBindi .get(&TuiCommandItem::ToggleHelp) .or(cfg.toggle_help.as_ref()) .unwrap_or(&TuiKeyBinding::new(KeyCode::Char('h'))), + toggle_help_alt: *cmd_items + .get(&TuiCommandItem::ToggleHelpAlt) + .or(cfg.toggle_help_alt.as_ref()) + .unwrap_or(&TuiKeyBinding::new(KeyCode::Char('?'))), toggle_settings: *cmd_items .get(&TuiCommandItem::ToggleSettings) .or(cfg.toggle_settings.as_ref()) @@ -435,6 +441,8 @@ impl Display for TuiKeyBinding { pub enum TuiCommandItem { /// Toggle the help dialog. ToggleHelp, + /// Alternative command to toggle the help dialog. + ToggleHelpAlt, /// Toggle the settings dialog. ToggleSettings, /// Move down to the next hop. diff --git a/src/config/file.rs b/src/config/file.rs index 6d2e95ea5..eeebd595d 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -176,6 +176,7 @@ pub struct ConfigThemeColors { #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub struct ConfigBindings { pub toggle_help: Option, + pub toggle_help_alt: Option, pub toggle_settings: Option, pub previous_hop: Option, pub next_hop: Option, diff --git a/src/frontend.rs b/src/frontend.rs index ea3f033d6..e2a49d771 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -68,6 +68,7 @@ fn run_app( let bindings = &app.tui_config.bindings; if app.show_help { if bindings.toggle_help.check(key) + || bindings.toggle_help_alt.check(key) || bindings.clear_selection.check(key) || bindings.quit.check(key) { @@ -88,7 +89,8 @@ fn run_app( } else if bindings.previous_hop.check(key) { app.previous_settings_item(); } - } else if bindings.toggle_help.check(key) { + } else if bindings.toggle_help.check(key) || bindings.toggle_help_alt.check(key) + { app.toggle_help(); } else if bindings.toggle_settings.check(key) { app.toggle_settings(); diff --git a/src/frontend/binding.rs b/src/frontend/binding.rs index d46c3f4dc..0deb67f40 100644 --- a/src/frontend/binding.rs +++ b/src/frontend/binding.rs @@ -7,6 +7,7 @@ use std::fmt::{Display, Formatter}; #[derive(Debug, Clone, Copy)] pub struct Bindings { pub toggle_help: KeyBinding, + pub toggle_help_alt: KeyBinding, pub toggle_settings: KeyBinding, pub previous_hop: KeyBinding, pub next_hop: KeyBinding, @@ -38,6 +39,7 @@ impl From for Bindings { fn from(value: TuiBindings) -> Self { Self { toggle_help: KeyBinding::from(value.toggle_help), + toggle_help_alt: KeyBinding::from(value.toggle_help_alt), toggle_settings: KeyBinding::from(value.toggle_settings), previous_hop: KeyBinding::from(value.previous_hop), next_hop: KeyBinding::from(value.next_hop), diff --git a/src/frontend/render/help.rs b/src/frontend/render/help.rs index 1d7840487..d9abefdcc 100644 --- a/src/frontend/render/help.rs +++ b/src/frontend/render/help.rs @@ -44,7 +44,7 @@ const HELP_LINES: [&str; 20] = [ "{ & } - expand & collapse hosts to max and min", "+ & - - zoom chart in and out", "z - toggle AS information (if available)", - "h - toggle help", + "h or ? - toggle help", "s - toggle settings", "q - quit", ]; diff --git a/src/frontend/render/settings.rs b/src/frontend/render/settings.rs index 829333c7b..610f207a3 100644 --- a/src/frontend/render/settings.rs +++ b/src/frontend/render/settings.rs @@ -259,6 +259,7 @@ fn format_binding_settings(app: &TuiApp) -> Vec { let binds = &app.tui_config.bindings; vec![ SettingsItem::new("toggle-help", format!("{}", binds.toggle_help)), + SettingsItem::new("toggle-help-alt", format!("{}", binds.toggle_help_alt)), SettingsItem::new("toggle-settings", format!("{}", binds.toggle_settings)), SettingsItem::new("next-hop", format!("{}", binds.next_hop)), SettingsItem::new("previous-hop", format!("{}", binds.previous_hop)), @@ -399,7 +400,7 @@ pub const SETTINGS_TABS: [(&str, usize); 6] = [ ("Trace", 14), ("Dns", 3), ("GeoIp", 1), - ("Bindings", 25), + ("Bindings", 26), ("Theme", 27), ]; diff --git a/trippy-config-sample.toml b/trippy-config-sample.toml index fc0499cb5..18745d4ba 100644 --- a/trippy-config-sample.toml +++ b/trippy-config-sample.toml @@ -285,6 +285,7 @@ map-info-panel-text-color = "gray" # See https://github.com/fujiapple852/trippy#key-bindings-reference for details. [bindings] toggle-help = "h" +toggle-help-alt = "?" toggle-settings = "s" next-hop = "down" previous-hop = "up"