diff options
author | Aram Drevekenin <aram@poor.dev> | 2020-01-13 17:49:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-13 17:49:04 +0100 |
commit | 463ec9bcb84842955d9ba48af47c9bba6670d670 (patch) | |
tree | 9a2e60e2f2934bc77e1f8d6b626b36ede22cd17f | |
parent | da91443d660106b61d6e712f83d97407f5bf2347 (diff) |
feat(helptext): communicate pause/resume to the user (#111)
* feat(helptext): communicate pause/resume to the user
* style(format): rustfmt
21 files changed, 70 insertions, 25 deletions
diff --git a/src/display/components/help_text.rs b/src/display/components/help_text.rs new file mode 100644 index 0000000..49de3b8 --- /dev/null +++ b/src/display/components/help_text.rs @@ -0,0 +1,32 @@ +use ::tui::backend::Backend; +use ::tui::layout::{Alignment, Rect}; +use ::tui::style::{Modifier, Style}; +use ::tui::terminal::Frame; +use ::tui::widgets::{Paragraph, Text, Widget}; + +pub struct HelpText { + pub paused: bool, +} + +const TEXT_WHEN_PAUSED: &str = " Press <SPACE> to resume."; +const TEXT_WHEN_NOT_PAUSED: &str = " Press <SPACE> to pause."; + +impl HelpText { + pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) { + let text = { + let content = if self.paused { + TEXT_WHEN_PAUSED + } else { + TEXT_WHEN_NOT_PAUSED + }; + + [Text::styled( + content, + Style::default().modifier(Modifier::BOLD), + )] + }; + Paragraph::new(text.iter()) + .alignment(Alignment::Left) + .render(frame, rect); + } +} diff --git a/src/display/components/layout.rs b/src/display/components/layout.rs index e5ca069..9854e3c 100644 --- a/src/display/components/layout.rs +++ b/src/display/components/layout.rs @@ -2,6 +2,7 @@ use ::tui::backend::Backend; use ::tui::layout::{Constraint, Direction, Rect}; use ::tui::terminal::Frame; +use super::HelpText; use super::Table; use super::TotalBandwidth; @@ -9,18 +10,26 @@ const FIRST_HEIGHT_BREAKPOINT: u16 = 30; const FIRST_WIDTH_BREAKPOINT: u16 = 120; const SECOND_WIDTH_BREAKPOINT: u16 = 150; -fn leave_gap_on_top_of_rect(rect: Rect) -> Rect { - let app = ::tui::layout::Layout::default() +fn top_app_and_bottom_split(rect: Rect) -> (Rect, Rect, Rect) { + let parts = ::tui::layout::Layout::default() .direction(Direction::Vertical) .margin(0) - .constraints([Constraint::Length(1), Constraint::Length(rect.height - 1)].as_ref()) + .constraints( + [ + Constraint::Length(1), + Constraint::Length(rect.height - 2), + Constraint::Length(1), + ] + .as_ref(), + ) .split(rect); - app[1] + (parts[0], parts[1], parts[2]) } pub struct Layout<'a> { pub header: TotalBandwidth<'a>, pub children: Vec<Table<'a>>, + pub footer: HelpText, } impl<'a> Layout<'a> { @@ -52,7 +61,7 @@ impl<'a> Layout<'a> { } } pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) { - let app = leave_gap_on_top_of_rect(rect); + let (top, app, bottom) = top_app_and_bottom_split(rect); let layout_slots = self.build_layout(app); for i in 0..layout_slots.len() { if let Some(rect) = layout_slots.get(i) { @@ -61,6 +70,7 @@ impl<'a> Layout<'a> { } } } - self.header.render(frame, rect); + self.header.render(frame, top); + self.footer.render(frame, bottom); } } diff --git a/src/display/components/mod.rs b/src/display/components/mod.rs index a0e226d..c99596c 100644 --- a/src/display/components/mod.rs +++ b/src/display/components/mod.rs @@ -1,9 +1,11 @@ mod display_bandwidth; +mod help_text; mod layout; mod table; mod total_bandwidth; pub use display_bandwidth::*; +pub use help_text::*; pub use layout::*; pub use table::*; pub use total_bandwidth::*; diff --git a/src/display/components/total_bandwidth.rs b/src/display/components/total_bandwidth.rs index ba78d2d..4a7ae9d 100644 --- a/src/display/components/total_bandwidth.rs +++ b/src/display/components/total_bandwidth.rs @@ -2,7 +2,7 @@ use ::tui::backend::Backend; use ::tui::layout::{Alignment, Rect}; use ::tui::style::{Color, Modifier, Style}; use ::tui::terminal::Frame; -use ::tui::widgets::{Block, Borders, Paragraph, Text, Widget}; +use ::tui::widgets::{Paragraph, Text, Widget}; use crate::display::{DisplayBandwidth, UIState}; @@ -32,7 +32,6 @@ impl<'a> TotalBandwidth<'a> { )] }; Paragraph::new(title_text.iter()) - .block(Block::default().borders(Borders::NONE)) .alignment(Alignment::Left) .render(frame, rect); } diff --git a/src/display/ui.rs b/src/display/ui.rs index 49b6721..0c4d36a 100644 --- a/src/display/ui.rs +++ b/src/display/ui.rs @@ -3,7 +3,7 @@ use ::std::collections::HashMap; use ::tui::backend::Backend; use ::tui::Terminal; -use crate::display::components::{Layout, Table, TotalBandwidth}; +use crate::display::components::{HelpText, Layout, Table, TotalBandwidth}; use crate::display::UIState; use crate::network::{display_connection_string, display_ip_or_host, LocalSocket, Utilization}; @@ -87,9 +87,11 @@ where state: &state, paused, }; + let help_text = HelpText { paused }; let layout = Layout { header: total_bandwidth, children: vec![processes, connections, remote_addresses], + footer: help_text, }; layout.render(&mut frame, size); }) diff --git a/src/tests/cases/snapshots/ui__basic_startup.snap b/src/tests/cases/snapshots/ui__basic_startup.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__basic_startup.snap +++ b/src/tests/cases/snapshots/ui__basic_startup.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__bi_directional_traffic.snap b/src/tests/cases/snapshots/ui__bi_directional_traffic.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__bi_directional_traffic.snap +++ b/src/tests/cases/snapshots/ui__bi_directional_traffic.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__layout_full_width_under_30_height.snap b/src/tests/cases/snapshots/ui__layout_full_width_under_30_height.snap index fe45202..2de98e8 100644 --- a/src/tests/cases/snapshots/ui__layout_full_width_under_30_height.snap +++ b/src/tests/cases/snapshots/ui__layout_full_width_under_30_height.snap @@ -29,6 +29,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__layout_under_120_width_full_height.snap b/src/tests/cases/snapshots/ui__layout_under_120_width_full_height.snap index bf9e509..678a313 100644 --- a/src/tests/cases/snapshots/ui__layout_under_120_width_full_height.snap +++ b/src/tests/cases/snapshots/ui__layout_under_120_width_full_height.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ │ │ │ │ │ -│ │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__layout_under_120_width_under_30_height.snap b/src/tests/cases/snapshots/ui__layout_under_120_width_under_30_height.snap index e312002..11de180 100644 --- a/src/tests/cases/snapshots/ui__layout_under_120_width_under_30_height.snap +++ b/src/tests/cases/snapshots/ui__layout_under_120_width_under_30_height.snap @@ -29,6 +29,6 @@ expression: "&terminal_draw_events_mirror[0]" │ │ │ │ │ │ -│ │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__layout_under_150_width_full_height.snap b/src/tests/cases/snapshots/ui__layout_under_150_width_full_height.snap index 43dc610..beaf97d 100644 --- a/src/tests/cases/snapshots/ui__layout_under_150_width_full_height.snap +++ b/src/tests/cases/snapshots/ui__layout_under_150_width_full_height.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__layout_under_150_width_under_30_height.snap b/src/tests/cases/snapshots/ui__layout_under_150_width_under_30_height.snap index 9bd6846..8f8dc64 100644 --- a/src/tests/cases/snapshots/ui__layout_under_150_width_under_30_height.snap +++ b/src/tests/cases/snapshots/ui__layout_under_150_width_under_30_height.snap @@ -29,6 +29,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__multiple_connections_from_remote_address.snap b/src/tests/cases/snapshots/ui__multiple_connections_from_remote_address.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__multiple_connections_from_remote_address.snap +++ b/src/tests/cases/snapshots/ui__multiple_connections_from_remote_address.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_different_connections.snap b/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_different_connections.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_different_connections.snap +++ b/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_different_connections.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_single_connection.snap b/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_single_connection.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_single_connection.snap +++ b/src/tests/cases/snapshots/ui__multiple_packets_of_traffic_from_single_connection.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__multiple_processes_with_multiple_connections.snap b/src/tests/cases/snapshots/ui__multiple_processes_with_multiple_connections.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__multiple_processes_with_multiple_connections.snap +++ b/src/tests/cases/snapshots/ui__multiple_processes_with_multiple_connections.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__one_packet_of_traffic.snap b/src/tests/cases/snapshots/ui__one_packet_of_traffic.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__one_packet_of_traffic.snap +++ b/src/tests/cases/snapshots/ui__one_packet_of_traffic.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__one_process_with_multiple_connections.snap b/src/tests/cases/snapshots/ui__one_process_with_multiple_connections.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__one_process_with_multiple_connections.snap +++ b/src/tests/cases/snapshots/ui__one_process_with_multiple_connections.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__pause_by_space-2.snap b/src/tests/cases/snapshots/ui__pause_by_space-2.snap index 204efd1..727e9cd 100644 --- a/src/tests/cases/snapshots/ui__pause_by_space-2.snap +++ b/src/tests/cases/snapshots/ui__pause_by_space-2.snap @@ -51,5 +51,5 @@ expression: "&terminal_draw_events_mirror[1]" - + resume. diff --git a/src/tests/cases/snapshots/ui__pause_by_space.snap b/src/tests/cases/snapshots/ui__pause_by_space.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__pause_by_space.snap +++ b/src/tests/cases/snapshots/ui__pause_by_space.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. diff --git a/src/tests/cases/snapshots/ui__traffic_with_winch_event.snap b/src/tests/cases/snapshots/ui__traffic_with_winch_event.snap index ec8d97c..baf94e1 100644 --- a/src/tests/cases/snapshots/ui__traffic_with_winch_event.snap +++ b/src/tests/cases/snapshots/ui__traffic_with_winch_event.snap @@ -50,6 +50,6 @@ expression: "&terminal_draw_events_mirror[0]" │ ││ │ │ ││ │ │ ││ │ -│ ││ │ └─────────────────────────────────────────────────────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────┘ + Press <SPACE> to pause. |