From 986910e52e4d00723cfbc0226a75bd584ecb7e0b Mon Sep 17 00:00:00 2001 From: John Date: Sat, 25 Nov 2023 21:23:16 +0800 Subject: [PATCH] show days in uptime if available --- Cargo.lock | 167 +++++++++++++++++++++++++++++++------------------- Cargo.toml | 4 +- src/fields.rs | 51 +++++++++------ src/main.rs | 25 ++++---- 4 files changed, 151 insertions(+), 96 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ed24a0..c8349b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,36 +4,24 @@ version = 3 [[package]] name = "aho-corasick" -version = "0.7.18" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" dependencies = [ "memchr", ] -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - [[package]] name = "bitflags" -version = "1.3.2" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" [[package]] name = "bytesize" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70" - -[[package]] -name = "cc" -version = "1.0.72" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" +checksum = "a3e368af43e418a04d52505cf3dbc23dda4e3407ae2fa99fd0e4f308ce546acc" [[package]] name = "cfg-if" @@ -42,15 +30,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "chrono" -version = "0.4.19" +name = "deranged" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +checksum = "0f32d04922c60427da6f9fef14d042d9edddef64cb9d4ce0d64d0685fbeb1fd3" dependencies = [ - "libc", - "num-integer", - "num-traits", - "winapi", + "powerfmt", ] [[package]] @@ -61,24 +46,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.117" +version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" [[package]] name = "memchr" -version = "2.4.1" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" - -[[package]] -name = "memoffset" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" -dependencies = [ - "autocfg", -] +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "minimal-lexical" @@ -88,52 +64,66 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "nix" -version = "0.23.1" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f866317acbd3a240710c63f065ffb1e4fd466259045ccb504130b7f668f35c6" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" dependencies = [ "bitflags", - "cc", "cfg-if", "libc", - "memoffset", ] [[package]] name = "nom" -version = "7.1.0" +version = "7.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" dependencies = [ "memchr", "minimal-lexical", - "version_check", ] [[package]] -name = "num-integer" -version = "0.1.44" +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "proc-macro2" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ - "autocfg", - "num-traits", + "unicode-ident", ] [[package]] -name = "num-traits" -version = "0.2.14" +name = "quote" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ - "autocfg", + "proc-macro2", ] [[package]] name = "regex" -version = "1.5.4" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" dependencies = [ "aho-corasick", "memchr", @@ -142,24 +132,73 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.25" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "serde" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.193" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] [[package]] name = "systemstat" -version = "0.1.10" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8862adb0fd5f4c5707b0eeb6c2ec7610bd7a8bf5e069150bd6dde04a7f40ebf7" +checksum = "a24aec24a9312c83999a28e3ef9db7e2afd5c64bf47725b758cdc1cafd5b0bd2" dependencies = [ "bytesize", - "chrono", "lazy_static", "libc", "nom", + "time", "winapi", ] +[[package]] +name = "time" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5" +dependencies = [ + "deranged", + "powerfmt", + "serde", + "time-core", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + [[package]] name = "treefetch" version = "2.0.0" @@ -170,10 +209,10 @@ dependencies = [ ] [[package]] -name = "version_check" -version = "0.9.4" +name = "unicode-ident" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "winapi" diff --git a/Cargo.toml b/Cargo.toml index 741fbd0..4b62f3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] regex = "1" -systemstat = "0.1" -nix = "0.23" +systemstat = "0.2.3" +nix = {version = "0.27", features = ["hostname", "feature"]} diff --git a/src/fields.rs b/src/fields.rs index cabcb3c..2ea84a9 100644 --- a/src/fields.rs +++ b/src/fields.rs @@ -1,7 +1,10 @@ -use std::io::Read; use std::env; +use std::ffi::OsStr; use std::fs; -use regex::{Regex, Captures}; +use std::io::Read; + +use regex::{Captures, Regex}; + use crate::colors; fn format_data(key: &str, value: &str) -> String { @@ -11,7 +14,7 @@ fn format_data(key: &str, value: &str) -> String { color = colors::green, bold = colors::bold, reset = colors::reset, - ) + ) } // Search with Regex in a string and return all of the matches @@ -33,10 +36,8 @@ pub fn get_user_host_name(is_christmas: bool) -> Result<(String, String), String } // Hostname - let mut buf = [0u8; 128]; - let hostname_cstr = nix::unistd::gethostname(&mut buf) - .map_err(|_| "Failed getting hostname".to_owned())?; - let hostname = hostname_cstr.to_str().map_err(|_| "Failed decoding hostname")?; + let hostname_cstr = nix::unistd ::gethostname().unwrap(); + let hostname = hostname_cstr.to_str().unwrap(); // Combine username and hostname into a formatted string let main_color: &str; @@ -54,7 +55,7 @@ pub fn get_user_host_name(is_christmas: bool) -> Result<(String, String), String color = main_color, bold = colors::bold, reset = colors::reset, - ).replace(" ", "").replace("\n", ""); + ).replace(" ", "").replace("\n", ""); // Separator // format: username length + @ (1) + hostname length @@ -144,16 +145,20 @@ pub fn get_distro_name() -> Result { } pub fn get_kernel(show_kern_name: bool) -> Result { - let uname = nix::sys::utsname::uname(); + let uname = nix::sys::utsname::uname().unwrap(); Ok(format_data( "kernel", &if show_kern_name { - format!("{}/{} {}", uname.sysname(), uname.machine(), uname.release()) + format!("{}/{} {}", gs(uname.sysname()), gs(uname.machine()), gs(uname.release())) } else { - format!("{} {}", uname.release(), uname.machine()) + format!("{} {}", gs(uname.release()), gs(uname.machine())) })) } +fn gs(s: &OsStr) -> &str { + s.to_str().unwrap() +} + pub fn get_shell() -> Result { let shell_env = env::var_os("SHELL"); @@ -181,15 +186,25 @@ pub fn get_shell() -> Result { pub fn format_uptime(time: std::time::Duration) -> String { let uptime_seconds = time.as_secs(); - // Calculate the uptime in hours and minutes respectively - let uptime_hours = uptime_seconds / (60 * 60); + // Calculate the uptime in days, hours and minutes respectively + let uptime_days = uptime_seconds / (60 * 60 * 24); + let uptime_hours = (uptime_seconds % (60 * 60 * 24)) / (60 * 60); let uptime_minutes = (uptime_seconds % (60 * 60)) / 60; - format_data( - "uptime", - &format!("{hours}h {minutes}m", - hours = uptime_hours, - minutes = uptime_minutes)) + if uptime_days > 0 { + format_data( + "uptime", + &format!("{days}d {hours}h {minutes}m", + days = uptime_days, + hours = uptime_hours, + minutes = uptime_minutes)) + } else { + format_data( + "uptime", + &format!("{hours}h {minutes}m", + hours = uptime_hours, + minutes = uptime_minutes)) + } } pub fn format_memory(mem: systemstat::Memory) -> String { diff --git a/src/main.rs b/src/main.rs index 79a7355..bb6a8bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ use std::env; use std::process; + use systemstat::Platform; + mod colors; mod fields; // Simple system fetch tool written in Rust. fn main() { - let args: Vec = env::args().collect(); let mut show_kern_name = false; let mut is_christmas = false; @@ -102,8 +103,8 @@ fn main() { let mut data_list: Vec = Vec::new(); if let Ok(value) = fields::get_user_host_name(is_christmas) { - data_list.push(value.0); - data_list.push(value.1); + data_list.push(value.0); + data_list.push(value.1); }; @@ -149,7 +150,7 @@ fn print_left_to_right(left: Vec, right: Vec, is_christmas: bool) { let left_len = left.len(); let right_len = right.len(); - let max_len = if left_len > right_len {left_len} else {right_len}; + let max_len = if left_len > right_len { left_len } else { right_len }; for i in 0..max_len { if i < left_len { @@ -160,10 +161,10 @@ fn print_left_to_right(left: Vec, right: Vec, // Red square if Christmas mode if is_christmas { print!("{}", right[i] - .replace("▪", - &format!("{}▪{}", - colors::red, - colors::green))); + .replace("▪", + &format!("{}▪{}", + colors::red, + colors::green))); } else { print!("{}", right[i]); } @@ -195,10 +196,10 @@ fn help_message() { let version = env!("CARGO_PKG_VERSION"); println!("Usage:"); println!(" {bold}{green}treefetch{reset} [options]", - green = colors::green, - reset = colors::reset, - bold = colors::bold, - ); + green = colors::green, + reset = colors::reset, + bold = colors::bold, + ); println!(); println!("OPTIONS"); println!(" -b, --bonsai Show a bonsai tree");