diff --git a/README.md b/README.md index 1d377dcd..5da2baea 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A gotop clone, written in Rust. Mostly done in an effort to learn Rust, while a * CPU usage monitor -* Disk usage (both total and I guess active?) +* Total disk usage * Memory usage @@ -18,7 +18,7 @@ A gotop clone, written in Rust. Mostly done in an effort to learn Rust, while a ## Other possible features -* Potentially package managing? +* Potentially process managing? * Theming @@ -28,6 +28,8 @@ A gotop clone, written in Rust. Mostly done in an effort to learn Rust, while a * Filtering in processing +* See if current disk activity is possible to do/graph? + ## Thanks * As mentioned, this project is most definitely inspired by [gotop](https://github.com/cjbassi/gotop) diff --git a/src/main.rs b/src/main.rs index c5c3165b..647d585d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,109 +1,10 @@ -use sysinfo::{ProcessExt, ProcessStatus, RefreshKind, System, SystemExt}; - -// TODO: Fix this - CPU Up, and CPU Down! -enum ProcessSorting { - CPU, - MEM, - PID, - Status, -} +use sysinfo::{System, SystemExt}; +mod widgets; +use widgets::{cpu, disks, mem, network, processes, temperature}; fn main() { let mut system = System::new(); system.refresh_all(); - draw_sorted_processes(ProcessSorting::CPU, true, &system); + //processes::draw_sorted_processes(processes::ProcessSorting::NAME, true, &system); + disks::draw_disk_usage_data(&system); } - -// Possible process info struct? -#[derive(Debug)] -struct ProcessInfo<'a> { - pid: u32, - cpu_usage: f32, - mem_usage: u64, - uptime: u64, - command: &'a str, - //status: &'a str, - // TODO: Env? -} - -fn get_status(status: ProcessStatus) -> &'static str { - match status { - ProcessStatus::Idle => "Idle", - ProcessStatus::Run => "Run", - ProcessStatus::Sleep => "Sleep", - ProcessStatus::Zombie => "Zombie", - ProcessStatus::Tracing => "Tracing", - ProcessStatus::Dead => "Dead", - _ => "Unknown", - } -} - -fn draw_sorted_processes(sorting_method: ProcessSorting, reverse_order: bool, sys: &System) { - let process_hashmap = sys.get_process_list(); - - // Read into a btreemap, based on sorting type. - // TODO: Evaluate whether this is too slow! - - let mut process_vector: Vec = process_hashmap.iter().map(|(_, process)| process.clone()).collect(); - - match sorting_method { - ProcessSorting::CPU => process_vector.sort_by(|a, b| { - let a_usage = a.cpu_usage(); - let b_usage = b.cpu_usage(); - - if a_usage > b_usage { - if reverse_order { - std::cmp::Ordering::Less - } else { - std::cmp::Ordering::Greater - } - } else if a_usage < b_usage { - if reverse_order { - std::cmp::Ordering::Greater - } else { - std::cmp::Ordering::Less - } - } else { - std::cmp::Ordering::Equal - } - }), - ProcessSorting::MEM => {} - ProcessSorting::PID => {} - ProcessSorting::Status => {} - } - - let mut formatted_vector: Vec = 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 process in formatted_vector { - println!("{:?}", process); - } -} - -fn get_timestamped_temperature() {} - -fn draw_temperatures() {} - -fn get_timestamped_cpu_data() {} - -fn draw_cpu_data() {} - -fn get_timestamped_ram_data() {} - -fn draw_ram_data() {} - -fn get_timestamped_network_data() {} - -fn draw_network_data() {} - -fn get_timestamped_drive_data() {} - -fn draw_drive_usage_data() {} diff --git a/src/widgets/cpu.rs b/src/widgets/cpu.rs index e69de29b..aad8bd36 100644 --- a/src/widgets/cpu.rs +++ b/src/widgets/cpu.rs @@ -0,0 +1,3 @@ +fn get_timestamped_cpu_data() {} + +fn draw_cpu_data() {} diff --git a/src/widgets/disks.rs b/src/widgets/disks.rs index e69de29b..50da2f03 100644 --- a/src/widgets/disks.rs +++ b/src/widgets/disks.rs @@ -0,0 +1,11 @@ +use sysinfo::{System, SystemExt, DiskExt}; + +fn get_timestamped_disk_data() {} + +pub fn draw_disk_usage_data(sys: &System) { + let list_of_disks = sys.get_disks(); + + for disk in list_of_disks { + println!("Disk: Total size: {}, used: {}, disk: {}, mount: {}", disk.get_total_space(), disk.get_total_space() - disk.get_available_space(), disk.get_name().to_str().unwrap(), disk.get_mount_point().to_str().unwrap()); + } +} diff --git a/src/widgets/mem.rs b/src/widgets/mem.rs index e69de29b..ebd48893 100644 --- a/src/widgets/mem.rs +++ b/src/widgets/mem.rs @@ -0,0 +1,3 @@ +fn get_timestamped_ram_data() {} + +fn draw_ram_data() {} \ No newline at end of file diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs new file mode 100644 index 00000000..cf9c9d2a --- /dev/null +++ b/src/widgets/mod.rs @@ -0,0 +1,6 @@ +pub mod disks; +pub mod temperature; +pub mod network; +pub mod processes; +pub mod mem; +pub mod cpu; \ No newline at end of file diff --git a/src/widgets/network.rs b/src/widgets/network.rs index e69de29b..7353da03 100644 --- a/src/widgets/network.rs +++ b/src/widgets/network.rs @@ -0,0 +1,3 @@ +fn get_timestamped_network_data() {} + +fn draw_network_data() {} \ No newline at end of file diff --git a/src/widgets/processes.rs b/src/widgets/processes.rs index e69de29b..072b14c8 100644 --- a/src/widgets/processes.rs +++ b/src/widgets/processes.rs @@ -0,0 +1,82 @@ +use sysinfo::{ProcessExt, ProcessStatus, System, SystemExt}; + +// TODO: Fix this - CPU Up, and CPU Down! +pub enum ProcessSorting { + CPU, + MEM, + PID, + NAME, +} + +// Possible process info struct? +#[derive(Debug)] +pub struct ProcessInfo<'a> { + pid: u32, + cpu_usage: f32, + mem_usage: u64, + uptime: u64, + command: &'a str, + //status: &'a str, + // TODO: Env? +} + +fn get_status(status: ProcessStatus) -> &'static str { + match status { + ProcessStatus::Idle => "Idle", + ProcessStatus::Run => "Run", + ProcessStatus::Sleep => "Sleep", + ProcessStatus::Zombie => "Zombie", + ProcessStatus::Tracing => "Tracing", + ProcessStatus::Dead => "Dead", + _ => "Unknown", + } +} + +fn get_ordering(a_val: T, b_val: T, reverse_order: bool) -> std::cmp::Ordering { + if a_val > b_val { + if reverse_order { + std::cmp::Ordering::Less + } else { + std::cmp::Ordering::Greater + } + } else if a_val < b_val { + if reverse_order { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Less + } + } else { + std::cmp::Ordering::Equal + } +} + +pub fn draw_sorted_processes(sorting_method: ProcessSorting, reverse_order: bool, sys: &System) { + let process_hashmap = sys.get_process_list(); + + // TODO: Evaluate whether this is too slow! + // TODO: Should I filter out blank command names? + let mut process_vector: Vec = process_hashmap.iter().map(|(_, process)| process.clone()).collect(); + + match sorting_method { + ProcessSorting::CPU => process_vector.sort_by(|a, b| get_ordering(a.cpu_usage(), b.cpu_usage(), reverse_order)), + ProcessSorting::MEM => process_vector.sort_by(|a, b| get_ordering(a.memory(), b.memory(), reverse_order)), + ProcessSorting::PID => process_vector.sort_by(|a, b| get_ordering(a.pid(), b.pid(), reverse_order)), + ProcessSorting::NAME => process_vector.sort_by(|a, b| get_ordering(a.name(), b.name(), reverse_order)), + } + + let mut formatted_vector: Vec = 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, + }); + } + + // TODO: For debugging, remove. + for process in formatted_vector { + println!("{:?}", process); + } +} diff --git a/src/widgets/temp.rs b/src/widgets/temp.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/src/widgets/temperature.rs b/src/widgets/temperature.rs new file mode 100644 index 00000000..066be228 --- /dev/null +++ b/src/widgets/temperature.rs @@ -0,0 +1,3 @@ +fn get_timestamped_temperature() {} + +fn draw_temperatures() {} \ No newline at end of file