Skip to content

Commit

Permalink
Implement a grace period (3000ms) for process selection in which the …
Browse files Browse the repository at this point in the history
…list is not reordered
  • Loading branch information
alexmaco committed Jun 1, 2020
1 parent 39da351 commit 714619c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/metrics.rs
Expand Up @@ -676,9 +676,8 @@ impl CPUTimeApp {
}
}

fn update_process_list(&mut self) {
fn update_process_list(&mut self, keep_order: bool) {
debug!("Updating Process List");
self.processes.clear();
let process_list = self.system.get_process_list();
let mut current_pids: HashSet<i32> = HashSet::with_capacity(process_list.len());
let mut top_pid: Option<i32> = None;
Expand Down Expand Up @@ -770,10 +769,15 @@ impl CPUTimeApp {
}
self.process_map.insert(zprocess.pid, zprocess);
}
self.processes.push(*pid);
current_pids.insert(*pid);
}

if keep_order {
self.processes.retain(|pid| current_pids.contains(pid));
} else {
self.processes = current_pids.iter().cloned().collect();
}

// remove pids that are gone
self.process_map.retain(|&k, _| current_pids.contains(&k));

Expand Down Expand Up @@ -818,7 +822,9 @@ impl CPUTimeApp {
}
}

self.sort_process_table();
if !keep_order {
self.sort_process_table();
}
}

pub fn sort_process_table(&mut self) {
Expand Down Expand Up @@ -968,7 +974,7 @@ impl CPUTimeApp {
.add_value_to("cpu_usage_histogram", self.cpu_utilization);
}

pub async fn update(&mut self, width: u16) {
pub async fn update(&mut self, width: u16, keep_order: bool) {
debug!("Updating Metrics");
self.system.refresh_all();
self.update_cpu().await;
Expand All @@ -994,7 +1000,7 @@ impl CPUTimeApp {
self.net_out = net.get_outcome();
self.histogram_map.add_value_to("net_in", self.net_in);
self.histogram_map.add_value_to("net_out", self.net_out);
self.update_process_list();
self.update_process_list(keep_order);
self.update_frequency().await;
self.update_disk(width);
self.get_platform().await;
Expand Down
19 changes: 17 additions & 2 deletions src/render.rs
Expand Up @@ -16,7 +16,7 @@ use std::borrow::Cow;
use std::io;
use std::io::Stdout;
use std::path::PathBuf;
use std::time::{Duration, UNIX_EPOCH};
use std::time::{Duration, Instant, UNIX_EPOCH};
use sysinfo::DiskExt;
use termion::event::Key;
use termion::input::MouseTerminal;
Expand All @@ -33,6 +33,8 @@ use tui::widgets::{
use tui::Frame;
use tui::Terminal;

const PROCESS_SELECTION_GRACE: Duration = Duration::from_millis(2000);

type ZBackend = TermionBackend<AlternateScreen<MouseTerminal<RawTerminal<Stdout>>>>;

macro_rules! float_to_byte_string {
Expand Down Expand Up @@ -1259,6 +1261,7 @@ pub struct TerminalRenderer {
show_find: bool,
filter: String,
highlighted_row: usize,
selection_grace_start: Option<Instant>,
}

impl<'a> TerminalRenderer {
Expand Down Expand Up @@ -1322,6 +1325,7 @@ impl<'a> TerminalRenderer {
show_find: false,
filter: String::from(""),
highlighted_row: 0,
selection_grace_start: None,
}
}

Expand Down Expand Up @@ -1471,7 +1475,16 @@ impl<'a> TerminalRenderer {
Event::Input(input) => input,
Event::Tick => {
debug!("Event Tick");
self.app.update(width).await;

if let Some(start) = self.selection_grace_start {
if start.elapsed() > PROCESS_SELECTION_GRACE {
self.selection_grace_start = None;
}
}

self.app
.update(width, self.selection_grace_start.is_some())
.await;
self.update_number += 1;
if self.update_number == self.zoom_factor {
self.update_number = 0;
Expand Down Expand Up @@ -1514,6 +1527,7 @@ impl<'a> TerminalRenderer {
(_, Key::Up) => {
if self.app.selected_process.is_some() {
} else if !process_table.is_empty() {
self.selection_grace_start = Some(Instant::now());
if self.highlighted_row != 0 {
self.highlighted_row -= 1;
}
Expand All @@ -1527,6 +1541,7 @@ impl<'a> TerminalRenderer {
(_, Key::Down) => {
if self.app.selected_process.is_some() {
} else if !process_table.is_empty() {
self.selection_grace_start = Some(Instant::now());
if self.highlighted_row < process_table.len() - 1 {
self.highlighted_row += 1;
}
Expand Down

0 comments on commit 714619c

Please sign in to comment.