From a07807f1c6ce9b6a2aa0ad5dfb86acf776c32c6f Mon Sep 17 00:00:00 2001 From: River Date: Tue, 28 Feb 2023 20:46:44 -0500 Subject: [PATCH] Added support for memory, and refactored some older code to use Option instead of placeholder Strings --- .fleet/run.json | 9 +++++++ Cargo.lock | 23 ++++++++++++++++ Cargo.toml | 1 + src/main.rs | 71 ++++++++++++++++++++++++++++++++++--------------- 4 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 .fleet/run.json diff --git a/.fleet/run.json b/.fleet/run.json new file mode 100644 index 0000000..05ef418 --- /dev/null +++ b/.fleet/run.json @@ -0,0 +1,9 @@ +{ + "configurations": [ + { + "type": "cargo", + "name": "", + }, + + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 85f0a26..950f36c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,16 @@ version = "3.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" +[[package]] +name = "byte-unit" +version = "4.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3348673602e04848647fffaa8e9a861e7b5d5cae6570727b41bde0f722514484" +dependencies = [ + "serde", + "utf8-width", +] + [[package]] name = "cc" version = "1.0.78" @@ -325,6 +335,7 @@ checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" name = "oxidefetch" version = "0.1.0" dependencies = [ + "byte-unit", "chrono", "colored", "compound_duration", @@ -391,6 +402,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" + [[package]] name = "syn" version = "1.0.107" @@ -449,6 +466,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +[[package]] +name = "utf8-width" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index a6abe02..fae919c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +byte-unit = "4.0.18" chrono = "0.4.23" colored = "2.0.0" compound_duration = "1.2.0" diff --git a/src/main.rs b/src/main.rs index 247aacf..5fc745a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,17 @@ - // Code by River. Copy if you want, but don't say it's yours. +use byte_unit::*; use chrono::*; use colored::*; -use sysinfo::*; -use std::env; -use whoami; use compound_duration; +use std::env; +use sysinfo::*; +use whoami; fn main() { // Generate system info struct let sys_info = InformationStruct::new(); - + // Format the date and time let datetime_formatted = format!( "{}, {}", @@ -23,22 +23,30 @@ fn main() { println!("{}", String::from(">>> OxideFetch  <<<").red()); color_print("Date:\t", '', &Some(datetime_formatted), "bright yellow"); - color_print("Host:\t", '', &Some(format!("{}@{}", sys_info.username, sys_info.hostname)), "purple"); + color_print( + "Host:\t", + '', + &Some(format!("{}@{}", sys_info.username, sys_info.hostname)), + "purple", + ); color_print("OS:\t", sys_info.icon, &sys_info.os_name, &sys_info.color); color_print("Ver:\t", '', &sys_info.os_ver, "bright red"); color_print("Kernel:\t", '', &sys_info.kernel_ver, "bright blue"); color_print("Uptime:\t", '', &Some(sys_info.uptime), "bright black"); color_print("Shell:\t", '', &sys_info.shell, "bright magenta"); color_print("CPU:\t", '', &Some(sys_info.cpu), "green"); - color_print("GPU:\t", '', &sys_info.gpu, "bright green") - + color_print("GPU:\t", '', &sys_info.gpu, "bright green"); + color_print("Memory:\t", '', &Some(sys_info.memory), "bright blue"); } fn color_print(field_title: &str, icon: char, field: &Option, color: &str) { // If the field is missing, it won't print. if field.is_some() { //print!("{} ", field_title.bright_white()); - println!("{}", format!("{} {}", icon, field.as_ref().unwrap()).color(color)); + println!( + "{}", + format!("{} {}", icon, field.as_ref().unwrap()).color(color) + ); } } @@ -51,10 +59,10 @@ struct InformationStruct { kernel_ver: Option, uptime: String, shell: Option, - _terminal: String, + _terminal: Option, cpu: String, gpu: Option, - _memory: String, + memory: String, icon: char, color: String, } @@ -86,29 +94,47 @@ impl InformationStruct { } }, - _terminal: String::from("Unknown Terminal"), // TODO: Add terminal detection. + _terminal: None, // TODO: Add terminal detection. cpu: String::from(sys.cpus()[0].brand()), gpu: { - match sys.name().unwrap_or(String::from("Unknown System")).as_ref() { + match sys + .name() + .unwrap_or(String::from("Unknown System")) + .as_ref() + { "Windows" => { - // On windows, we run "wmic path win32_VideoController get name" and + // On windows, we run "wmic path win32_VideoController get name" and // the second line is our GPU name. - let command_output = std::process::Command::new("wmic").args(["path", "win32_VideoController", "get", "name"]).output(); + let command_output = std::process::Command::new("wmic") + .args(["path", "win32_VideoController", "get", "name"]) + .output(); match command_output { Ok(gpu_info) => { let gpu_info_as_string = String::from_utf8(gpu_info.stdout); - Some(String::from(gpu_info_as_string.unwrap().split("\n").collect::>()[1])) - }, + Some(String::from( + gpu_info_as_string + .unwrap() + .split("\n") + .collect::>()[1], + )) + } Err(_) => None, } } _ => { // On Linux or Mac, hopefully, "lspci | grep VGA | cut -d ":" -f3" gives us our GPU name. - let command_output = std::process::Command::new("bash").args(["-c", "lspci | grep VGA | cut -d \":\" -f3"]).output(); + let command_output = std::process::Command::new("bash") + .args(["-c", "lspci | grep VGA | cut -d \":\" -f3"]) + .output(); let gpu = match command_output { - Ok(gpu_info) => Some(String::from_utf8(gpu_info.stdout).unwrap().trim().to_owned()), + Ok(gpu_info) => Some( + String::from_utf8(gpu_info.stdout) + .unwrap() + .trim() + .to_owned(), + ), Err(_) => None, }; if gpu == Some(String::from("")) { @@ -120,7 +146,11 @@ impl InformationStruct { } }, - _memory: String::from("Unknown memory"), // TODO: Add memory detection. + memory: format!( + "{}/{}", + Byte::from(sys.used_memory()).get_appropriate_unit(true), + Byte::from(sys.total_memory()).get_appropriate_unit(true) + ), icon: match sys .name() @@ -210,5 +240,4 @@ mod test { assert!(result.is_ok()); } - }