diff options
author | Rupert Rutledge <eosis2 [at] gmail.com> | 2020-05-19 21:24:03 +0100 |
---|---|---|
committer | Rupert Rutledge <eosis2 [at] gmail.com> | 2020-05-19 21:26:25 +0100 |
commit | d897e14eae9c39c0bd64a775acbc6c5dedfbdbdc (patch) | |
tree | a5f934775f6a4978f89d33498946030754d38bf9 | |
parent | 098af1f7befaabb5f129a4bd70a070daa411ce86 (diff) |
Use correct elapsed time when resizing the terminal
- Split the logic for determining the elapsed time into a function
as it was called in different placed in main.rs.
-rw-r--r-- | src/display/components/header_details.rs | 12 | ||||
-rw-r--r-- | src/main.rs | 24 |
2 files changed, 25 insertions, 11 deletions
diff --git a/src/display/components/header_details.rs b/src/display/components/header_details.rs index a3b0766..042d667 100644 --- a/src/display/components/header_details.rs +++ b/src/display/components/header_details.rs @@ -1,11 +1,11 @@ +use crate::display::{DisplayBandwidth, UIState}; +use ::std::time::{Duration, Instant}; use ::tui::backend::Backend; use ::tui::layout::{Alignment, Rect}; use ::tui::style::{Color, Modifier, Style}; use ::tui::terminal::Frame; use ::tui::widgets::{Paragraph, Text, Widget}; -use crate::display::{DisplayBandwidth, UIState}; - const SECONDS_IN_DAY: u64 = 86400; pub struct HeaderDetails<'a> { @@ -14,6 +14,14 @@ pub struct HeaderDetails<'a> { pub paused: bool, } +pub fn elapsed_time(last_start_time: Instant, cumulative_time: Duration, paused: bool) -> Duration { + if paused { + cumulative_time + } else { + cumulative_time + last_start_time.elapsed() + } +} + impl<'a> HeaderDetails<'a> { #[allow(clippy::int_plus_one)] pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) { diff --git a/src/main.rs b/src/main.rs index 62c9621..a52642c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod os; #[cfg(test)] mod tests; -use display::{RawTerminalBackend, Ui}; +use display::{elapsed_time, RawTerminalBackend, Ui}; use network::{ dns::{self, IpTable}, Connection, LocalSocket, Sniffer, Utilization, @@ -147,14 +147,21 @@ where .spawn({ let ui = ui.clone(); let paused = paused.clone(); + let cumulative_time = cumulative_time.clone(); + let last_start_time = last_start_time.clone(); move || { on_winch({ Box::new(move || { let mut ui = ui.lock().unwrap(); + let paused = paused.load(Ordering::SeqCst); ui.draw( - paused.load(Ordering::SeqCst), + paused, dns_shown, - std::time::Duration::new(131, 0), + elapsed_time( + *last_start_time.read().unwrap(), + *cumulative_time.read().unwrap(), + paused, + ), ); }) }); @@ -198,12 +205,11 @@ where if !paused { ui.update_state(sockets_to_procs, utilization, ip_to_host); } - let elapsed_time = if paused { - *cumulative_time.read().unwrap() - } else { - *cumulative_time.read().unwrap() - + last_start_time.read().unwrap().elapsed() - }; + let elapsed_time = elapsed_time( + *last_start_time.read().unwrap(), + *cumulative_time.read().unwrap(), + paused, + ); if raw_mode { ui.output_text(&mut write_to_stdout); |