From 274c30fc315ad6e9a2a3d0beeb131e384507de06 Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Fri, 30 Dec 2022 23:49:55 -0800 Subject: [PATCH 1/2] Add delay for usage --- src/args.rs | 5 ++++- src/interactive.rs | 2 +- src/interface.rs | 11 +++++++---- src/system.rs | 6 ++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/args.rs b/src/args.rs index efc3438f..b2440ca0 100644 --- a/src/args.rs +++ b/src/args.rs @@ -44,6 +44,9 @@ enum GetType { Usage { #[structopt(short, long)] raw: bool, + + #[structopt(short, long)] + delay: Option, }, /// The overall frequency of your cpu @@ -272,7 +275,7 @@ pub fn parse_args(config: config::Config) { ACSCommand::Get { get } => match get { GetType::Freq { raw } => int.get.freq(raw), GetType::Power { raw } => int.get.power(raw), - GetType::Usage { raw } => int.get.usage(raw), + GetType::Usage { raw, delay } => int.get.usage(raw, delay), GetType::Thermal { raw } => int.get.thermal(raw), GetType::Turbo { raw } => int.get.turbo(raw), GetType::AvailableGovs { raw } => int.get.available_govs(raw), diff --git a/src/interactive.rs b/src/interactive.rs index 9d06d677..2ccc8efe 100644 --- a/src/interactive.rs +++ b/src/interactive.rs @@ -77,7 +77,7 @@ pub fn interactive() { "help" => help(), "get freq" => int.get.freq(false), "get power" => int.get.power(false), - "get usage" => int.get.usage(false), + "get usage" => int.get.usage(false, None), "get turbo" => int.get.turbo(false), "get available_governors" => int.get.available_govs(false), "get cpus" => int.get.cpus(false), diff --git a/src/interface.rs b/src/interface.rs index b2890b8d..80270bc0 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -121,7 +121,7 @@ pub struct Get {} pub trait Getter { fn freq(&self, raw: bool); fn power(&self, raw: bool); - fn usage(&self, raw: bool); + fn usage(&self, raw: bool, delay: Option); fn thermal(&self, raw: bool); fn turbo(&self, raw: bool); fn available_govs(&self, raw: bool); @@ -175,11 +175,14 @@ impl Getter for Get { print_power(lid, battery.capacity, plugged, raw); } - fn usage(&self, raw: bool) { + fn usage(&self, raw: bool, delay: Option) { if !raw { - println!("Calculating cpu percentage over 1 second."); + println!( + "Calculating cpu percentage over {} second.", + delay.unwrap_or(1) + ); } - let percent = get_cpu_percent(); + let percent = get_cpu_percent(delay); if raw { println!("{}", percent) diff --git a/src/system.rs b/src/system.rs index 6be1b095..896d126a 100644 --- a/src/system.rs +++ b/src/system.rs @@ -138,11 +138,13 @@ pub fn parse_proc_file(proc: String) -> Result, Error> { Ok(procs) } -pub fn get_cpu_percent() -> String { +pub fn get_cpu_percent(delay: Option) -> String { let mut proc = read_proc_stat_file().unwrap(); let avg_timing: &ProcStat = &parse_proc_file(proc).unwrap()[0]; - thread::sleep(time::Duration::from_millis(1000)); + let millis = if let Some(d) = delay { d * 1000 } else { 1000 }; + + thread::sleep(time::Duration::from_millis(millis)); proc = read_proc_stat_file().unwrap(); let avg_timing_2: &ProcStat = &parse_proc_file(proc).unwrap()[0]; From 2d82cd86aa55c018d846ea1364980f89b4b04afa Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Fri, 30 Dec 2022 23:55:01 -0800 Subject: [PATCH 2/2] Add none in test --- src/system.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system.rs b/src/system.rs index 896d126a..23b37630 100644 --- a/src/system.rs +++ b/src/system.rs @@ -451,7 +451,7 @@ mod tests { #[test] fn test_parse_proc_stat_file() { - let cpu_percent = get_cpu_percent().parse::().unwrap(); + let cpu_percent = get_cpu_percent(None).parse::().unwrap(); assert_eq!(type_of(cpu_percent), type_of(0.0_f32)); assert!(cpu_percent > 0.0 && cpu_percent < 100.0); }