diff --git a/ffplayout-api/examples/system_i.rs b/ffplayout-api/examples/system_i.rs index 9ecb42c9..7a466237 100644 --- a/ffplayout-api/examples/system_i.rs +++ b/ffplayout-api/examples/system_i.rs @@ -10,7 +10,7 @@ pub fn byte_convert(num: f64) -> String { let num = num.abs(); let units = ["B", "kiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]; if num < 1_f64 { - return format!("{}{} {}", negative, num, "B"); + return format!("{negative}{num} B"); } let delimiter = 1024_f64; let exponent = cmp::min( @@ -22,7 +22,68 @@ pub fn byte_convert(num: f64) -> String { .unwrap() * 1_f64; let unit = units[exponent as usize]; - format!("{}{} {}", negative, pretty_bytes, unit) + format!("{negative}{pretty_bytes} {unit}") +} + +#[derive(Debug)] +struct Cpu { + cores: f32, + usage: f32, +} + +#[derive(Debug, Default)] +struct Disk { + name: String, + total: String, + used: String, +} + +#[derive(Debug)] +struct Load { + one: f64, + five: f64, + fifteen: f64, +} + +#[derive(Debug)] +struct Memory { + total: String, + used: String, + free: String, +} + +#[derive(Debug, Default)] +struct Network { + name: String, + current_in: String, + total_in: String, + current_out: String, + total_out: String, +} + +#[derive(Debug)] +struct MySystem { + name: Option, + kernel: Option, + version: Option, +} + +#[derive(Debug)] +struct Swap { + total: String, + used: String, + free: String, +} + +#[derive(Debug)] +struct SystemStat { + cpu: Cpu, + disk: Disk, + load: Load, + memory: Memory, + network: Network, + swap: Swap, + system: MySystem, } fn main() { @@ -32,65 +93,78 @@ fn main() { sys.refresh_all(); let mut usage = 0.0; - let count = sys.cpus().len() as f32; + let cores = sys.cpus().len() as f32; for cpu in sys.cpus() { usage += cpu.cpu_usage(); } - print!("{esc}[2J{esc}[1;1H", esc = 27 as char); + let my_cpu = Cpu { + cores, + usage: usage * cores / 100.0, + }; - println!("System name: {:?}", sys.name()); - println!("System kernel version: {:?}", sys.kernel_version()); - println!("System OS version: {:?}\n", sys.os_version()); - - println!("total memory: {}", byte_convert(sys.total_memory() as f64)); - println!("used memory : {}", byte_convert(sys.used_memory() as f64)); - println!( - "free memory : {}\n", - byte_convert((sys.total_memory() - sys.used_memory()) as f64) - ); - - println!("total swap : {}", byte_convert(sys.total_swap() as f64)); - println!("used swap : {}", byte_convert(sys.used_swap() as f64)); - println!( - "free swap : {}\n", - byte_convert((sys.total_swap() - sys.used_swap()) as f64) - ); + let mut my_disk = Disk::default(); for disk in sys.disks() { - if disk.mount_point() == Path::new("/") { - println!( - "disk: {:?} | available space {}", - disk.name(), - byte_convert(disk.available_space() as f64), - ); + if disk.mount_point().to_string_lossy().len() > 1 + && Path::new("/home/jb/Videos").starts_with(disk.mount_point()) + { + my_disk.name = disk.name().to_string_lossy().to_string(); + my_disk.total = byte_convert(disk.total_space() as f64); + my_disk.used = byte_convert(disk.available_space() as f64); } } - println!(); let load_avg = sys.load_average(); + let my_load = Load { + one: load_avg.one, + five: load_avg.five, + fifteen: load_avg.fifteen, + }; + + let my_memory = Memory { + total: byte_convert(sys.total_memory() as f64), + used: byte_convert(sys.used_memory() as f64), + free: byte_convert((sys.total_memory() - sys.used_memory()) as f64), + }; + + let mut my_network = Network::default(); for (interface_name, data) in sys.networks() { if interface_name == "wlp5s0" { - println!( - "{}: (in / total) {} / {} | (out / total) {} / {}", - interface_name, - byte_convert(data.received() as f64), - byte_convert(data.total_received() as f64), - byte_convert(data.transmitted() as f64), - byte_convert(data.total_transmitted() as f64), - ); + my_network.name = interface_name.clone(); + my_network.current_in = byte_convert(data.received() as f64); + my_network.total_in = byte_convert(data.total_received() as f64); + my_network.current_out = byte_convert(data.transmitted() as f64); + my_network.total_out = byte_convert(data.total_transmitted() as f64); } } - println!(); + let my_swap = Swap { + total: byte_convert(sys.total_swap() as f64), + used: byte_convert(sys.used_swap() as f64), + free: byte_convert((sys.total_swap() - sys.used_swap()) as f64), + }; - println!("CPU Usage: {:.2}%", usage * count / 100.0); - println!( - "Load: {}% {}% {}%\n", - load_avg.one, load_avg.five, load_avg.fifteen - ); + let my_system = MySystem { + name: sys.name(), + kernel: sys.kernel_version(), + version: sys.os_version(), + }; + + let system_stat = SystemStat { + system: my_system, + memory: my_memory, + swap: my_swap, + disk: my_disk, + cpu: my_cpu, + load: my_load, + network: my_network, + }; + + print!("{esc}[2J{esc}[1;1H", esc = 27 as char); + println!("{system_stat:#?}"); sleep(Duration::from_secs(1)) }