Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause UI with <SPACE> #106

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/display/components/total_bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ use crate::display::{DisplayBandwidth, UIState};

pub struct TotalBandwidth<'a> {
pub state: &'a UIState,
pub paused: bool,
}

impl<'a> TotalBandwidth<'a> {
pub fn render(&self, frame: &mut Frame<impl Backend>, rect: Rect) {
let title_text = [Text::styled(
format!(
" Total Rate Up / Down: {} / {}",
DisplayBandwidth(self.state.total_bytes_uploaded as f64),
DisplayBandwidth(self.state.total_bytes_downloaded as f64)
),
Style::default().fg(Color::Green).modifier(Modifier::BOLD),
)];
let title_text = {
let paused_str = if self.paused { "[PAUSED]" } else { "" };
let color = if self.paused {
Color::Yellow
} else {
Color::Green
};

[Text::styled(
format!(
" Total Rate Up / Down: {} / {} {}",
DisplayBandwidth(self.state.total_bytes_uploaded as f64),
DisplayBandwidth(self.state.total_bytes_downloaded as f64),
paused_str
),
Style::default().fg(color).modifier(Modifier::BOLD),
)]
};
Paragraph::new(title_text.iter())
.block(Block::default().borders(Borders::NONE))
.alignment(Alignment::Left)
Expand Down
7 changes: 5 additions & 2 deletions src/display/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where
));
}
}
pub fn draw(&mut self) {
pub fn draw(&mut self, paused: bool) {
let state = &self.state;
let ip_to_host = &self.ip_to_host;
self.terminal
Expand All @@ -83,7 +83,10 @@ where
let connections = Table::create_connections_table(&state, &ip_to_host);
let processes = Table::create_processes_table(&state);
let remote_addresses = Table::create_remote_addresses_table(&state, &ip_to_host);
let total_bandwidth = TotalBandwidth { state: &state };
let total_bandwidth = TotalBandwidth {
state: &state,
paused,
};
let layout = Layout {
header: total_bandwidth,
children: vec![processes, connections, remote_addresses],
Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ where
B: Backend + Send + 'static,
{
let running = Arc::new(AtomicBool::new(true));
let paused = Arc::new(AtomicBool::new(false));

let mut active_threads = vec![];

Expand All @@ -121,11 +122,12 @@ where
.name("resize_handler".to_string())
.spawn({
let ui = ui.clone();
let paused = paused.clone();
move || {
on_winch({
Box::new(move || {
let mut ui = ui.lock().unwrap();
ui.draw();
ui.draw(paused.load(Ordering::SeqCst));
})
});
}
Expand All @@ -138,6 +140,7 @@ where
.name("display_handler".to_string())
.spawn({
let running = running.clone();
let paused = paused.clone();
let network_utilization = network_utilization.clone();
move || {
while running.load(Ordering::Acquire) {
Expand All @@ -160,11 +163,14 @@ where
}
{
let mut ui = ui.lock().unwrap();
ui.update_state(sockets_to_procs, utilization, ip_to_host);
let paused = paused.load(Ordering::SeqCst);
if !paused {
ui.update_state(sockets_to_procs, utilization, ip_to_host);
}
if raw_mode {
ui.output_text(&mut write_to_stdout);
} else {
ui.draw();
ui.draw(paused);
}
}
let render_duration = render_start_time.elapsed();
Expand Down Expand Up @@ -195,6 +201,10 @@ where
display_handler.unpark();
break;
}
Event::Key(Key::Char(' ')) => {
paused.fetch_xor(true, Ordering::SeqCst);
display_handler.unpark();
}
_ => (),
};
}
Expand Down
55 changes: 55 additions & 0 deletions src/tests/cases/snapshots/ui__pause_by_space-2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
source: src/tests/cases/ui.rs
expression: "&terminal_draw_events_mirror[1]"
---
Total Rate Up / Down: 0Bps / 0Bps [PAUSED]


















































Loading