Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delay for usage #498

Merged
merged 2 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ enum GetType {
Usage {
#[structopt(short, long)]
raw: bool,

#[structopt(short, long)]
delay: Option<u64>,
},

/// The overall frequency of your cpu
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 7 additions & 4 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>);
fn thermal(&self, raw: bool);
fn turbo(&self, raw: bool);
fn available_govs(&self, raw: bool);
Expand Down Expand Up @@ -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<u64>) {
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)
Expand Down
8 changes: 5 additions & 3 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ pub fn parse_proc_file(proc: String) -> Result<Vec<ProcStat>, Error> {
Ok(procs)
}

pub fn get_cpu_percent() -> String {
pub fn get_cpu_percent(delay: Option<u64>) -> 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];
Expand Down Expand Up @@ -449,7 +451,7 @@ mod tests {

#[test]
fn test_parse_proc_stat_file() {
let cpu_percent = get_cpu_percent().parse::<f32>().unwrap();
let cpu_percent = get_cpu_percent(None).parse::<f32>().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh i didnt know u can just use None

assert_eq!(type_of(cpu_percent), type_of(0.0_f32));
assert!(cpu_percent > 0.0 && cpu_percent < 100.0);
}
Expand Down