diff options
author | Rupert Rutledge <eosis2 [at] gmail.com> | 2020-04-26 14:12:06 +0100 |
---|---|---|
committer | Rupert Rutledge <eosis2 [at] gmail.com> | 2020-04-26 14:12:06 +0100 |
commit | 1953c8bb8b7ec3a84e1b238421c0320f9e0b3391 (patch) | |
tree | 0fe8ea191ca7175a3346e5cbeb72a35980d91211 | |
parent | 648eb0a66906035858794977cfaa2a03a75f0210 (diff) |
Working implementation of the elapsed time of the capture
This elapsed time pauses during pause sections and restarts on
resumption.
Ongoing:
- Consider a RwLock instead of a Mutex for the elapsed time and
cumulative time values, as these are only written by the UI thread
and read elsewhere.
- Combine the cumulative time and start times into a struct for
clarity.
- Separate out the elapsed time from the Bandwidth, as they are
separate considerations, leading to altering the rendering of the
header to write bandwidth and elapsed time separately.
-rw-r--r-- | src/display/components/total_bandwidth.rs | 15 | ||||
-rw-r--r-- | src/display/ui.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 30 |
3 files changed, 44 insertions, 4 deletions
diff --git a/src/display/components/total_bandwidth.rs b/src/display/components/total_bandwidth.rs index 80d892f..311ce67 100644 --- a/src/display/components/total_bandwidth.rs +++ b/src/display/components/total_bandwidth.rs @@ -8,6 +8,7 @@ use crate::display::{DisplayBandwidth, UIState}; pub struct TotalBandwidth<'a> { pub state: &'a UIState, + pub elapsed_time: std::time::Duration, pub paused: bool, } @@ -38,8 +39,22 @@ impl<'a> TotalBandwidth<'a> { Style::default().fg(color).modifier(Modifier::BOLD), )] }; + Paragraph::new(title_text.iter()) .alignment(Alignment::Left) .render(frame, rect); + + let elapsed_time_text = [Text::styled( + format!( + "Total Elapsed Time: {:02}:{:02}:{:02}", + self.elapsed_time.as_secs() / 3600, + (self.elapsed_time.as_secs() % 3600) / 60, + self.elapsed_time.as_secs() % 60 + ), + Style::default().fg(Color::LightBlue).modifier(Modifier::BOLD), + )]; + Paragraph::new(elapsed_time_text.iter()) + .alignment(Alignment::Right) + .render(frame, rect); } } diff --git a/src/display/ui.rs b/src/display/ui.rs index 68f133a..cd87786 100644 --- a/src/display/ui.rs +++ b/src/display/ui.rs @@ -79,7 +79,7 @@ where )); } } - pub fn draw(&mut self, paused: bool, show_dns: bool) { + pub fn draw(&mut self, paused: bool, show_dns: bool, elapsed_time: std::time::Duration) { let state = &self.state; let children = self.get_tables_to_display(); self.terminal @@ -87,6 +87,7 @@ where let size = frame.size(); let total_bandwidth = TotalBandwidth { state: &state, + elapsed_time, paused, }; let help_text = HelpText { paused, show_dns }; diff --git a/src/main.rs b/src/main.rs index d91430a..50cdf16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,6 +121,8 @@ where { let running = Arc::new(AtomicBool::new(true)); let paused = Arc::new(AtomicBool::new(false)); + let last_start_time = Arc::new(Mutex::new(Instant::now())); + let cumulative_time = Arc::new(Mutex::new(std::time::Duration::new(0, 0))); let dns_shown = opts.show_dns; let mut active_threads = vec![]; @@ -148,7 +150,7 @@ where on_winch({ Box::new(move || { let mut ui = ui.lock().unwrap(); - ui.draw(paused.load(Ordering::SeqCst), dns_shown); + ui.draw(paused.load(Ordering::SeqCst), dns_shown, std::time::Duration::new(131, 0)); }) }); } @@ -163,6 +165,9 @@ where let running = running.clone(); let paused = paused.clone(); let network_utilization = network_utilization.clone(); + let last_start_time = last_start_time.clone(); + let cumulative_time = cumulative_time.clone(); + move || { while running.load(Ordering::Acquire) { let render_start_time = Instant::now(); @@ -188,10 +193,16 @@ where if !paused { ui.update_state(sockets_to_procs, utilization, ip_to_host); } + let elapsed_time = if paused { + cumulative_time.lock().unwrap().clone() + } else { + *cumulative_time.lock().unwrap() + last_start_time.lock().unwrap().elapsed() + }; + if raw_mode { ui.output_text(&mut write_to_stdout); } else { - ui.draw(paused, dns_shown); + ui.draw(paused, dns_shown, elapsed_time); } } let render_duration = render_start_time.elapsed(); @@ -223,7 +234,20 @@ where break; } Event::Key(Key::Char(' ')) => { - paused.fetch_xor(true, Ordering::SeqCst); + let restarting = paused.fetch_xor(true, Ordering::SeqCst); + if restarting { + *last_start_time.lock().unwrap() = Instant::now(); + } else { + let last_start_time_clone = { + last_start_time.lock().unwrap().clone() + }; + let current_cumulative_time_clone = { + cumulative_time.lock().unwrap().clone() + }; + let new_cumulative_time = current_cumulative_time_clone + last_start_time_clone.elapsed(); + *cumulative_time.lock().unwrap() = new_cumulative_time; + } + display_handler.unpark(); } _ => (), |