Skip to content

Commit

Permalink
feat(tui): add settings hotkeys (#1217)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Jul 13, 2024
1 parent 30b99ad commit 2b009f6
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 3 deletions.
66 changes: 66 additions & 0 deletions crates/trippy-tui/src/config/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ pub struct TuiBindings {
pub toggle_help: TuiKeyBinding,
pub toggle_help_alt: TuiKeyBinding,
pub toggle_settings: TuiKeyBinding,
pub toggle_settings_tui: TuiKeyBinding,
pub toggle_settings_trace: TuiKeyBinding,
pub toggle_settings_dns: TuiKeyBinding,
pub toggle_settings_geoip: TuiKeyBinding,
pub toggle_settings_bindings: TuiKeyBinding,
pub toggle_settings_theme: TuiKeyBinding,
pub toggle_settings_columns: TuiKeyBinding,
pub previous_hop: TuiKeyBinding,
pub next_hop: TuiKeyBinding,
pub previous_trace: TuiKeyBinding,
Expand Down Expand Up @@ -47,6 +54,13 @@ impl Default for TuiBindings {
toggle_help: TuiKeyBinding::new(KeyCode::Char('h')),
toggle_help_alt: TuiKeyBinding::new(KeyCode::Char('?')),
toggle_settings: TuiKeyBinding::new(KeyCode::Char('s')),
toggle_settings_tui: TuiKeyBinding::new(KeyCode::Char('1')),
toggle_settings_trace: TuiKeyBinding::new(KeyCode::Char('2')),
toggle_settings_dns: TuiKeyBinding::new(KeyCode::Char('3')),
toggle_settings_geoip: TuiKeyBinding::new(KeyCode::Char('4')),
toggle_settings_bindings: TuiKeyBinding::new(KeyCode::Char('5')),
toggle_settings_theme: TuiKeyBinding::new(KeyCode::Char('6')),
toggle_settings_columns: TuiKeyBinding::new(KeyCode::Char('7')),
previous_hop: TuiKeyBinding::new(KeyCode::Up),
next_hop: TuiKeyBinding::new(KeyCode::Down),
previous_trace: TuiKeyBinding::new(KeyCode::Left),
Expand Down Expand Up @@ -95,6 +109,16 @@ impl TuiBindings {
(self.toggle_help, TuiCommandItem::ToggleHelp),
(self.toggle_help_alt, TuiCommandItem::ToggleHelpAlt),
(self.toggle_settings, TuiCommandItem::ToggleSettings),
(self.toggle_settings_tui, TuiCommandItem::ToggleSettings),
(self.toggle_settings_trace, TuiCommandItem::ToggleSettings),
(self.toggle_settings_dns, TuiCommandItem::ToggleSettings),
(self.toggle_settings_geoip, TuiCommandItem::ToggleSettings),
(
self.toggle_settings_bindings,
TuiCommandItem::ToggleSettings,
),
(self.toggle_settings_theme, TuiCommandItem::ToggleSettings),
(self.toggle_settings_columns, TuiCommandItem::ToggleSettings),
(self.previous_hop, TuiCommandItem::PreviousHop),
(self.next_hop, TuiCommandItem::NextHop),
(self.previous_trace, TuiCommandItem::PreviousTrace),
Expand Down Expand Up @@ -163,6 +187,34 @@ impl From<(HashMap<TuiCommandItem, TuiKeyBinding>, ConfigBindings)> for TuiBindi
.get(&TuiCommandItem::ToggleSettings)
.or(cfg.toggle_settings.as_ref())
.unwrap_or(&Self::default().toggle_settings),
toggle_settings_tui: *cmd_items
.get(&TuiCommandItem::ToggleSettingsTui)
.or(cfg.toggle_settings_tui.as_ref())
.unwrap_or(&Self::default().toggle_settings_tui),
toggle_settings_trace: *cmd_items
.get(&TuiCommandItem::ToggleSettingsTrace)
.or(cfg.toggle_settings_trace.as_ref())
.unwrap_or(&Self::default().toggle_settings_trace),
toggle_settings_dns: *cmd_items
.get(&TuiCommandItem::ToggleSettingsDns)
.or(cfg.toggle_settings_dns.as_ref())
.unwrap_or(&Self::default().toggle_settings_dns),
toggle_settings_geoip: *cmd_items
.get(&TuiCommandItem::ToggleSettingsGeoip)
.or(cfg.toggle_settings_geoip.as_ref())
.unwrap_or(&Self::default().toggle_settings_geoip),
toggle_settings_bindings: *cmd_items
.get(&TuiCommandItem::ToggleSettingsBindings)
.or(cfg.toggle_settings_bindings.as_ref())
.unwrap_or(&Self::default().toggle_settings_bindings),
toggle_settings_theme: *cmd_items
.get(&TuiCommandItem::ToggleSettingsTheme)
.or(cfg.toggle_settings_theme.as_ref())
.unwrap_or(&Self::default().toggle_settings_theme),
toggle_settings_columns: *cmd_items
.get(&TuiCommandItem::ToggleSettingsColumns)
.or(cfg.toggle_settings_columns.as_ref())
.unwrap_or(&Self::default().toggle_settings_columns),
previous_hop: *cmd_items
.get(&TuiCommandItem::PreviousHop)
.or(cfg.previous_hop.as_ref())
Expand Down Expand Up @@ -496,6 +548,20 @@ pub enum TuiCommandItem {
ToggleHelpAlt,
/// Toggle the settings dialog.
ToggleSettings,
/// Toggle the TUI settings dialog tab.
ToggleSettingsTui,
/// Toggle the trace settings dialog tab.
ToggleSettingsTrace,
/// Toggle the DNS settings dialog tab.
ToggleSettingsDns,
/// Toggle the Geoip settings dialog tab.
ToggleSettingsGeoip,
/// Toggle the bindings settings dialog tab.
ToggleSettingsBindings,
/// Toggle the theme settings dialog tab.
ToggleSettingsTheme,
/// Toggle the column settings dialog tab.
ToggleSettingsColumns,
/// Move down to the next hop.
NextHop,
/// Move up to the previous hop.
Expand Down
14 changes: 14 additions & 0 deletions crates/trippy-tui/src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ pub struct ConfigBindings {
pub toggle_help: Option<TuiKeyBinding>,
pub toggle_help_alt: Option<TuiKeyBinding>,
pub toggle_settings: Option<TuiKeyBinding>,
pub toggle_settings_tui: Option<TuiKeyBinding>,
pub toggle_settings_trace: Option<TuiKeyBinding>,
pub toggle_settings_dns: Option<TuiKeyBinding>,
pub toggle_settings_geoip: Option<TuiKeyBinding>,
pub toggle_settings_bindings: Option<TuiKeyBinding>,
pub toggle_settings_theme: Option<TuiKeyBinding>,
pub toggle_settings_columns: Option<TuiKeyBinding>,
pub previous_hop: Option<TuiKeyBinding>,
pub next_hop: Option<TuiKeyBinding>,
pub previous_trace: Option<TuiKeyBinding>,
Expand Down Expand Up @@ -385,6 +392,13 @@ impl Default for ConfigBindings {
toggle_help: Some(bindings.toggle_help),
toggle_help_alt: Some(bindings.toggle_help_alt),
toggle_settings: Some(bindings.toggle_settings),
toggle_settings_tui: Some(bindings.toggle_settings_tui),
toggle_settings_trace: Some(bindings.toggle_settings_trace),
toggle_settings_dns: Some(bindings.toggle_settings_dns),
toggle_settings_geoip: Some(bindings.toggle_settings_geoip),
toggle_settings_bindings: Some(bindings.toggle_settings_bindings),
toggle_settings_theme: Some(bindings.toggle_settings_theme),
toggle_settings_columns: Some(bindings.toggle_settings_columns),
previous_hop: Some(bindings.previous_hop),
next_hop: Some(bindings.next_hop),
previous_trace: Some(bindings.previous_trace),
Expand Down
28 changes: 28 additions & 0 deletions crates/trippy-tui/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ fn run_app<B: Backend>(
|| bindings.quit.check(key)
{
app.toggle_settings();
} else if bindings.toggle_settings_tui.check(key) {
app.show_settings_columns(0);
} else if bindings.toggle_settings_trace.check(key) {
app.show_settings_columns(1);
} else if bindings.toggle_settings_dns.check(key) {
app.show_settings_columns(2);
} else if bindings.toggle_settings_geoip.check(key) {
app.show_settings_columns(3);
} else if bindings.toggle_settings_bindings.check(key) {
app.show_settings_columns(4);
} else if bindings.toggle_settings_theme.check(key) {
app.show_settings_columns(5);
} else if bindings.toggle_settings_columns.check(key) {
app.show_settings_columns(6);
} else if bindings.previous_trace.check(key) {
app.previous_settings_tab();
} else if bindings.next_trace.check(key) {
Expand All @@ -109,6 +123,20 @@ fn run_app<B: Backend>(
app.toggle_help();
} else if bindings.toggle_settings.check(key) {
app.toggle_settings();
} else if bindings.toggle_settings_tui.check(key) {
app.show_settings_columns(0);
} else if bindings.toggle_settings_trace.check(key) {
app.show_settings_columns(1);
} else if bindings.toggle_settings_dns.check(key) {
app.show_settings_columns(2);
} else if bindings.toggle_settings_geoip.check(key) {
app.show_settings_columns(3);
} else if bindings.toggle_settings_bindings.check(key) {
app.show_settings_columns(4);
} else if bindings.toggle_settings_theme.check(key) {
app.show_settings_columns(5);
} else if bindings.toggle_settings_columns.check(key) {
app.show_settings_columns(6);
} else if bindings.next_hop.check(key) {
app.next_hop();
} else if bindings.previous_hop.check(key) {
Expand Down
14 changes: 14 additions & 0 deletions crates/trippy-tui/src/frontend/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ pub struct Bindings {
pub toggle_help: KeyBinding,
pub toggle_help_alt: KeyBinding,
pub toggle_settings: KeyBinding,
pub toggle_settings_tui: KeyBinding,
pub toggle_settings_trace: KeyBinding,
pub toggle_settings_dns: KeyBinding,
pub toggle_settings_geoip: KeyBinding,
pub toggle_settings_bindings: KeyBinding,
pub toggle_settings_theme: KeyBinding,
pub toggle_settings_columns: KeyBinding,
pub previous_hop: KeyBinding,
pub next_hop: KeyBinding,
pub previous_trace: KeyBinding,
Expand Down Expand Up @@ -43,6 +50,13 @@ impl From<TuiBindings> for Bindings {
toggle_help: KeyBinding::from(value.toggle_help),
toggle_help_alt: KeyBinding::from(value.toggle_help_alt),
toggle_settings: KeyBinding::from(value.toggle_settings),
toggle_settings_tui: KeyBinding::from(value.toggle_settings_tui),
toggle_settings_trace: KeyBinding::from(value.toggle_settings_trace),
toggle_settings_dns: KeyBinding::from(value.toggle_settings_dns),
toggle_settings_geoip: KeyBinding::from(value.toggle_settings_geoip),
toggle_settings_bindings: KeyBinding::from(value.toggle_settings_bindings),
toggle_settings_theme: KeyBinding::from(value.toggle_settings_theme),
toggle_settings_columns: KeyBinding::from(value.toggle_settings_columns),
previous_hop: KeyBinding::from(value.previous_hop),
next_hop: KeyBinding::from(value.next_hop),
previous_trace: KeyBinding::from(value.previous_trace),
Expand Down
2 changes: 1 addition & 1 deletion crates/trippy-tui/src/frontend/render/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ const HELP_LINES: [&str; 22] = [
"+ & - - zoom chart in and out",
"z - toggle AS information (if available)",
"h or ? - toggle help",
"s - toggle settings",
"s - toggle settings (jump to tab with 1-7 keys)",
"q - quit",
];
6 changes: 6 additions & 0 deletions crates/trippy-tui/src/frontend/tui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ impl TuiApp {
self.show_settings = !self.show_settings;
}

pub fn show_settings_columns(&mut self, column_index: usize) {
self.show_settings = true;
self.settings_tab_selected = column_index;
self.setting_table_state.select(Some(0));
}

pub fn toggle_hop_details(&mut self) {
if self.show_hop_details {
self.tui_config.max_addrs = None;
Expand Down
6 changes: 5 additions & 1 deletion crates/trippy-tui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![allow(clippy::struct_excessive_bools, clippy::cast_sign_loss)]
#![allow(
clippy::struct_excessive_bools,
clippy::cast_sign_loss,
clippy::struct_field_names
)]
#![forbid(unsafe_code)]

use crate::config::TrippyAction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
source: crates/trippy-tui/src/print.rs
---
TUIbindingcommands:toggle-help,toggle-help-alt,toggle-settings,next-hop,previous-hop,next-trace,previous-trace,next-hop-address,previous-hop-address,address-mode-ip,address-mode-host,address-mode-both,toggle-freeze,toggle-chart,toggle-map,toggle-flows,toggle-privacy,expand-hosts,expand-hosts-max,contract-hosts,contract-hosts-min,chart-zoom-in,chart-zoom-out,clear-trace-data,clear-dns-cache,clear-selection,toggle-as-info,toggle-hop-details,quit
TUIbindingcommands:toggle-help,toggle-help-alt,toggle-settings,toggle-settings-tui,toggle-settings-trace,toggle-settings-dns,toggle-settings-geoip,toggle-settings-bindings,toggle-settings-theme,toggle-settings-columns,next-hop,previous-hop,next-trace,previous-trace,next-hop-address,previous-hop-address,address-mode-ip,address-mode-host,address-mode-both,toggle-freeze,toggle-chart,toggle-map,toggle-flows,toggle-privacy,expand-hosts,expand-hosts-max,contract-hosts,contract-hosts-min,chart-zoom-in,chart-zoom-out,clear-trace-data,clear-dns-cache,clear-selection,toggle-as-info,toggle-hop-details,quit
7 changes: 7 additions & 0 deletions trippy-config-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ map-info-panel-text-color = "gray"
toggle-help = "h"
toggle-help-alt = "?"
toggle-settings = "s"
toggle-settings-tui = "1"
toggle-settings-trace = "2"
toggle-settings-dns = "3"
toggle-settings-geoip = "4"
toggle-settings-bindings = "5"
toggle-settings-theme = "6"
toggle-settings-columns = "7"
next-hop = "down"
previous-hop = "up"
next-trace = "right"
Expand Down

0 comments on commit 2b009f6

Please sign in to comment.