Skip to content

Commit

Permalink
feat(tui): add quit-preserve-screen key binding
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Nov 5, 2024
1 parent 31ea947 commit 6cfc48d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
15 changes: 15 additions & 0 deletions crates/trippy-tui/src/config/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct TuiBindings {
pub toggle_as_info: TuiKeyBinding,
pub toggle_hop_details: TuiKeyBinding,
pub quit: TuiKeyBinding,
pub quit_preserve_screen: TuiKeyBinding,
}

impl Default for TuiBindings {
Expand Down Expand Up @@ -98,6 +99,10 @@ impl Default for TuiBindings {
toggle_as_info: TuiKeyBinding::new(KeyCode::Char('z')),
toggle_hop_details: TuiKeyBinding::new(KeyCode::Char('d')),
quit: TuiKeyBinding::new(KeyCode::Char('q')),
quit_preserve_screen: TuiKeyBinding::new_with_modifier(
KeyCode::Char('q'),
KeyModifiers::SHIFT,
),
}
}
}
Expand Down Expand Up @@ -151,6 +156,10 @@ impl TuiBindings {
(self.toggle_as_info, TuiCommandItem::ToggleASInfo),
(self.toggle_hop_details, TuiCommandItem::ToggleHopDetails),
(self.quit, TuiCommandItem::Quit),
(
self.quit_preserve_screen,
TuiCommandItem::QuitPreserveScreen,
),
]
.iter()
.fold(
Expand Down Expand Up @@ -326,6 +335,10 @@ impl From<(HashMap<TuiCommandItem, TuiKeyBinding>, ConfigBindings)> for TuiBindi
.get(&TuiCommandItem::Quit)
.or(cfg.quit.as_ref())
.unwrap_or(&Self::default().quit),
quit_preserve_screen: *cmd_items
.get(&TuiCommandItem::QuitPreserveScreen)
.or(cfg.quit_preserve_screen.as_ref())
.unwrap_or(&Self::default().quit_preserve_screen),
}
}
}
Expand Down Expand Up @@ -628,4 +641,6 @@ pub enum TuiCommandItem {
ToggleHopDetails,
/// Quit the application.
Quit,
/// Quit the application and preserve the screen.
QuitPreserveScreen,
}
2 changes: 2 additions & 0 deletions crates/trippy-tui/src/config/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ pub struct ConfigBindings {
pub toggle_as_info: Option<TuiKeyBinding>,
pub toggle_hop_details: Option<TuiKeyBinding>,
pub quit: Option<TuiKeyBinding>,
pub quit_preserve_screen: Option<TuiKeyBinding>,
}

impl Default for ConfigBindings {
Expand Down Expand Up @@ -442,6 +443,7 @@ impl Default for ConfigBindings {
toggle_as_info: Some(bindings.toggle_as_info),
toggle_hop_details: Some(bindings.toggle_hop_details),
quit: Some(bindings.quit),
quit_preserve_screen: Some(bindings.quit_preserve_screen),
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions crates/trippy-tui/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn run_frontend(
let mut app = TuiApp::new(tui_config, resolver, geoip_lookup, traces);
let res = run_app(&mut terminal, &mut app);
disable_raw_mode()?;
if preserve_screen {
if preserve_screen || matches!(res, Ok(ExitAction::PreserveScreen)) {
terminal.set_cursor_position(Position::new(0, terminal.size()?.height))?;
terminal.backend_mut().append_lines(1)?;
} else {
Expand All @@ -60,8 +60,16 @@ pub fn run_frontend(
Ok(())
}

/// The exit action to take when the frontend exits.
enum ExitAction {
/// Exit the frontend normally.
Normal,
/// Preserve the screen on exit.
PreserveScreen,
}

#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut TuiApp) -> io::Result<()> {
fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut TuiApp) -> io::Result<ExitAction> {
loop {
if app.frozen_start.is_none() {
app.snapshot_trace_data();
Expand Down Expand Up @@ -221,7 +229,9 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut TuiApp) -> io::Resu
} else if bindings.toggle_hop_details.check(key) {
app.toggle_hop_details();
} else if bindings.quit.check(key) || CTRL_C.check(key) {
return Ok(());
return Ok(ExitAction::Normal);
} else if bindings.quit_preserve_screen.check(key) {
return Ok(ExitAction::PreserveScreen);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/trippy-tui/src/frontend/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct Bindings {
pub toggle_as_info: KeyBinding,
pub toggle_hop_details: KeyBinding,
pub quit: KeyBinding,
pub quit_preserve_screen: KeyBinding,
}

impl From<TuiBindings> for Bindings {
Expand Down Expand Up @@ -85,6 +86,7 @@ impl From<TuiBindings> for Bindings {
toggle_as_info: KeyBinding::from(value.toggle_as_info),
toggle_hop_details: KeyBinding::from(value.toggle_hop_details),
quit: KeyBinding::from(value.quit),
quit_preserve_screen: KeyBinding::from(value.quit_preserve_screen),
}
}
}
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,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-privacy,contract-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-privacy,contract-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,quit-preserve-screen
1 change: 1 addition & 0 deletions trippy-config-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,4 @@ clear-selection = "esc"
toggle-as-info = "z"
toggle-hop-details = "d"
quit = "q"
quit-preserve-screen = "shift+q"

0 comments on commit 6cfc48d

Please sign in to comment.