From 6421ad4005dc7bfd782c1765cbcea17886e81970 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Sun, 6 Oct 2019 17:02:00 +0200 Subject: feat(ui): react to SIGWINCH --- src/display/ui.rs | 65 ++++++++++++++++++++++++++++++++++++------------- src/display/ui_state.rs | 3 ++- 2 files changed, 50 insertions(+), 18 deletions(-) (limited to 'src/display') diff --git a/src/display/ui.rs b/src/display/ui.rs index 1607e7c..30d36d0 100644 --- a/src/display/ui.rs +++ b/src/display/ui.rs @@ -144,21 +144,52 @@ fn render_remote_ip_table(state: &UIState, frame: &mut Frame, rect table.render(frame, rect); } -pub fn display_loop( - network_utilization: &Utilization, - terminal: &mut Terminal, - connections_to_procs: HashMap, - ip_to_host: &HashMap, -) { - let state = UIState::new(connections_to_procs, &network_utilization); - terminal - .draw(|mut f| { - let screen_horizontal_halves = split(Direction::Horizontal, f.size()); - let right_side_vertical_halves = - split(Direction::Vertical, screen_horizontal_halves[1]); - render_connections_table(&state, &mut f, screen_horizontal_halves[0], ip_to_host); - render_process_table(&state, &mut f, right_side_vertical_halves[0]); - render_remote_ip_table(&state, &mut f, right_side_vertical_halves[1], ip_to_host); - }) - .unwrap(); +pub struct Ui +where B: Backend +{ + terminal: Terminal, + state: UIState, + ip_to_host: HashMap, +} + +impl Ui +where B: Backend +{ + pub fn new (terminal_backend: B) -> Self { + let mut terminal = Terminal::new(terminal_backend).unwrap(); + terminal.clear().unwrap(); + terminal.hide_cursor().unwrap(); + Ui { + terminal: terminal, + state: Default::default(), + ip_to_host: Default::default(), + } + } + pub fn draw (&mut self) { + let state = &self.state; + let ip_to_host = &self.ip_to_host; + self.terminal + .draw(|mut f| { + let screen_horizontal_halves = split(Direction::Horizontal, f.size()); + let right_side_vertical_halves = + split(Direction::Vertical, screen_horizontal_halves[1]); + render_connections_table(state, &mut f, screen_horizontal_halves[0], ip_to_host); + render_process_table(state, &mut f, right_side_vertical_halves[0]); + render_remote_ip_table(state, &mut f, right_side_vertical_halves[1], ip_to_host); + }) + .unwrap(); + } + pub fn update_state( + &mut self, + connections_to_procs: HashMap, + utilization: Utilization, + ip_to_host: HashMap, + ) { + self.state = UIState::new(connections_to_procs, utilization); + self.ip_to_host = ip_to_host; + } + pub fn end(&mut self) { // TODO: destroy? + self.terminal.clear().unwrap(); + self.terminal.show_cursor().unwrap(); + } } diff --git a/src/display/ui_state.rs b/src/display/ui_state.rs index 83c1382..3469518 100644 --- a/src/display/ui_state.rs +++ b/src/display/ui_state.rs @@ -40,6 +40,7 @@ impl Bandwidth for NetworkData { } } +#[derive(Default)] pub struct UIState { pub processes: BTreeMap, pub remote_ips: BTreeMap, @@ -49,7 +50,7 @@ pub struct UIState { impl UIState { pub fn new( connections_to_procs: HashMap, - network_utilization: &Utilization, + network_utilization: Utilization, ) -> Self { let mut processes: BTreeMap = BTreeMap::new(); let mut remote_ips: BTreeMap = BTreeMap::new(); -- cgit v1.2.3