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 95b05f7 commit a5bd090
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
12 changes: 12 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,7 @@ impl TuiBindings {
(self.toggle_as_info, TuiCommandItem::ToggleASInfo),
(self.toggle_hop_details, TuiCommandItem::ToggleHopDetails),
(self.quit, TuiCommandItem::Quit),
(self.quit_preserve_screen, TuiCommandItem::Quit),
]
.iter()
.fold(
Expand Down Expand Up @@ -326,6 +332,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 +638,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 @@ -46,7 +46,7 @@ pub fn run_frontend(
let preserve_screen = tui_config.preserve_screen;
let res = run_app(&mut terminal, traces, tui_config, resolver, geoip_lookup);
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 @@ -59,14 +59,22 @@ 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>,
trace_info: Vec<TraceInfo>,
tui_config: TuiConfig,
resolver: DnsResolver,
geoip_lookup: GeoIpLookup,
) -> io::Result<()> {
) -> io::Result<ExitAction> {
let mut app = TuiApp::new(tui_config, resolver, geoip_lookup, trace_info);
loop {
if app.frozen_start.is_none() {
Expand Down Expand Up @@ -227,7 +235,9 @@ fn run_app<B: Backend>(
} 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
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 = "shit+q"

0 comments on commit a5bd090

Please sign in to comment.