Added working sorting for processes.

This commit is contained in:
ClementTsang 2019-09-04 23:39:44 -04:00
parent 9c6bfe84a2
commit 2c53ab09a9
1 changed files with 41 additions and 28 deletions

View File

@ -12,9 +12,10 @@ enum ProcessSorting {
fn main() { fn main() {
let mut system = System::new(); let mut system = System::new();
system.refresh_all(); system.refresh_all();
draw_sorted_processes(ProcessSorting::CPU, &system); draw_sorted_processes(ProcessSorting::CPU, true, &system);
} }
// Possible process info struct?
#[derive(Debug)] #[derive(Debug)]
struct ProcessInfo<'a> { struct ProcessInfo<'a> {
pid: u32, pid: u32,
@ -22,7 +23,7 @@ struct ProcessInfo<'a> {
mem_usage: u64, mem_usage: u64,
uptime: u64, uptime: u64,
command: &'a str, command: &'a str,
status: &'a str, //status: &'a str,
// TODO: Env? // TODO: Env?
} }
@ -38,41 +39,53 @@ fn get_status(status: ProcessStatus) -> &'static str {
} }
} }
fn draw_sorted_processes(sorting_method: ProcessSorting, sys: &System) { fn draw_sorted_processes(sorting_method: ProcessSorting, reverse_order: bool, sys: &System) {
let process_hashmap = sys.get_process_list(); let process_hashmap = sys.get_process_list();
// Read into a btreemap, based on sorting type. // Read into a btreemap, based on sorting type.
// TODO: Evaluate whether this is too slow! // TODO: Evaluate whether this is too slow!
let mut process_tree: BTreeMap<String, ProcessInfo> = BTreeMap::new(); let mut process_vector: Vec<sysinfo::Process> = process_hashmap.iter().map(|(_, process)| process.clone()).collect();
for (pid, process) in process_hashmap { match sorting_method {
let new_process_info = ProcessInfo { ProcessSorting::CPU => process_vector.sort_by(|a, b| {
pid: *pid as u32, let a_usage = a.cpu_usage();
cpu_usage: process.cpu_usage(), let b_usage = b.cpu_usage();
mem_usage: process.memory(),
uptime: process.start_time(), // TODO: This is not correct rn if a_usage > b_usage {
command: process.name(), if reverse_order {
status: get_status(process.status()), std::cmp::Ordering::Less
}; } else {
match sorting_method { std::cmp::Ordering::Greater
ProcessSorting::CPU => { }
process_tree.insert(process.cpu_usage().to_string(), new_process_info); } else if a_usage < b_usage {
} if reverse_order {
ProcessSorting::MEM => { std::cmp::Ordering::Greater
process_tree.insert(process.memory().to_string(), new_process_info); } else {
} std::cmp::Ordering::Less
ProcessSorting::PID => { }
process_tree.insert(pid.to_string(), new_process_info); } else {
} std::cmp::Ordering::Equal
ProcessSorting::Status => { }
process_tree.insert(get_status(process.status()).to_string(), new_process_info); }),
} ProcessSorting::MEM => {}
ProcessSorting::PID => {}
ProcessSorting::Status => {}
} }
let mut formatted_vector : Vec<ProcessInfo> = Vec::new();
for process in &mut process_vector {
formatted_vector.push(ProcessInfo {
cpu_usage: process.cpu_usage(),
command: process.name(),
mem_usage: process.memory(),
uptime: process.start_time(),
pid: process.pid() as u32,
});
} }
for (k, v) in process_tree { for process in formatted_vector {
println!("Key: {}, Val: {:?}", k, v); println!("{:?}", process);
} }
} }