diff options
author | Aram Drevekenin <aram@poor.dev> | 2019-10-06 17:02:00 +0200 |
---|---|---|
committer | Aram Drevekenin <aram@poor.dev> | 2019-10-06 17:02:00 +0200 |
commit | 6421ad4005dc7bfd782c1765cbcea17886e81970 (patch) | |
tree | 12eec1a402b67837a5acb8ae61ae95295d7211e0 /src/display | |
parent | 0422839ddb9baaca906524b3b535e00bdaecc343 (diff) |
feat(ui): react to SIGWINCH
Diffstat (limited to 'src/display')
-rw-r--r-- | src/display/ui.rs | 65 | ||||
-rw-r--r-- | src/display/ui_state.rs | 3 |
2 files changed, 50 insertions, 18 deletions
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<impl Backend>, rect table.render(frame, rect); } -pub fn display_loop( - network_utilization: &Utilization, - terminal: &mut Terminal<impl Backend>, - connections_to_procs: HashMap<Connection, String>, - ip_to_host: &HashMap<Ipv4Addr, String>, -) { - 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<B> +where B: Backend +{ + terminal: Terminal<B>, + state: UIState, + ip_to_host: HashMap<Ipv4Addr, String>, +} + +impl <B>Ui<B> +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<Connection, String>, + utilization: Utilization, + ip_to_host: HashMap<Ipv4Addr, String>, + ) { + 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<String, NetworkData>, pub remote_ips: BTreeMap<Ipv4Addr, NetworkData>, @@ -49,7 +50,7 @@ pub struct UIState { impl UIState { pub fn new( connections_to_procs: HashMap<Connection, String>, - network_utilization: &Utilization, + network_utilization: Utilization, ) -> Self { let mut processes: BTreeMap<String, NetworkData> = BTreeMap::new(); let mut remote_ips: BTreeMap<Ipv4Addr, NetworkData> = BTreeMap::new(); |