From 3903381ad3f427e0bcb5a9f1eb10f368c5d15399 Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Mon, 6 Jun 2022 08:34:03 -0700 Subject: [PATCH 1/4] Move print_turbo_status out of daemon --- src/daemon.rs | 27 +++------------------------ src/display.rs | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index de0cd72b..046407ca 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -14,12 +14,12 @@ use super::power::{has_battery, read_battery_charge, read_lid_state, read_power_ use super::settings::{GraphType, Settings}; use super::system::{ check_available_governors, check_cpu_freq, check_cpu_temperature, check_cpu_usage, - check_turbo_enabled, get_highest_temp, list_cpus, parse_proc_file, read_proc_stat_file, + get_highest_temp, list_cpus, parse_proc_file, read_proc_stat_file, ProcStat, }; use super::terminal::terminal_width; use super::Error; -use crate::display::print_turbo_animation; +use crate::display::print_turbo_status; use crate::warn_user; #[derive(Debug, PartialEq)] @@ -32,7 +32,7 @@ pub enum State { Unknown, } -// Return governor string based on current state +/// Return governor string based on current state fn get_governor(current_state: &State) -> &'static str { match current_state { State::Normal => "powersave", @@ -132,27 +132,6 @@ fn get_battery_status(charging: bool) -> String { } } -fn print_turbo_status(cores: usize, no_animation: bool, term_width: usize, delay: u64) { - let mut turbo_y_pos: usize = 7; - let title_width = 94; - - if term_width > title_width { - turbo_y_pos = 6 - } - - match check_turbo_enabled() { - Ok(turbo) => { - let enabled_message = if turbo { "yes" } else { "no" }; - - println!("{} {}", " Turbo:", enabled_message.bold(),); - - if !no_animation { - print_turbo_animation(cores, turbo_y_pos, delay); - } - } - Err(e) => eprintln!("Could not check turbo\n{:?}", e), - } -} fn calculate_average_usage(cpus: &Vec) -> Result { let mut sum = 0.0; diff --git a/src/display.rs b/src/display.rs index 11ebec79..5e06ff44 100644 --- a/src/display.rs +++ b/src/display.rs @@ -4,6 +4,7 @@ use std::thread; use super::config::get_config; use super::cpu::CPU; use super::power::LidState; +use super::system::check_turbo_enabled; use colored::*; #[macro_export] @@ -79,6 +80,28 @@ pub fn print_turbo_animation(cpu: usize, y_pos: usize, delay: u64) { }); } +pub fn print_turbo_status(cores: usize, no_animation: bool, term_width: usize, delay: u64) { + let mut turbo_y_pos: usize = 7; + let title_width = 94; + + if term_width > title_width { + turbo_y_pos = 6 + } + + match check_turbo_enabled() { + Ok(turbo) => { + let enabled_message = if turbo { "yes" } else { "no" }; + + println!("{} {}", " Turbo:", enabled_message.bold(),); + + if !no_animation { + print_turbo_animation(cores, turbo_y_pos, delay); + } + } + Err(e) => eprintln!("Could not check turbo\n{:?}", e), + } +} + fn print_vec(t: Vec, raw: bool) { if raw { for x in t { From feef0b0bd32ce13746636b58e97a5b2de795f2d5 Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Mon, 6 Jun 2022 09:45:57 -0700 Subject: [PATCH 2/4] Move get_battery_status away from daemon --- src/daemon.rs | 29 +++++------------------------ src/power.rs | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 046407ca..51764e43 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -10,12 +10,14 @@ use super::cpu::{Speed, CPU}; use super::graph::{Graph, Grapher}; use super::logger; use super::logger::Interface; -use super::power::{has_battery, read_battery_charge, read_lid_state, read_power_source, LidState}; +use super::power::{ + get_battery_status, has_battery, read_battery_charge, read_lid_state, read_power_source, + LidState, +}; use super::settings::{GraphType, Settings}; use super::system::{ check_available_governors, check_cpu_freq, check_cpu_temperature, check_cpu_usage, - get_highest_temp, list_cpus, parse_proc_file, read_proc_stat_file, - ProcStat, + get_highest_temp, list_cpus, parse_proc_file, read_proc_stat_file, ProcStat, }; use super::terminal::terminal_width; use super::Error; @@ -112,27 +114,6 @@ fn make_gov_schedutil(cpu: &mut CPU) -> Result<(), Error> { // Ok(()) //} -fn get_battery_status(charging: bool) -> String { - if has_battery() { - match read_battery_charge() { - Ok(bat) => { - format!( - "Battery: {}", - if charging { - format!("{}%", bat).green() - } else { - format!("{}%", bat).red() - }, - ) - } - Err(e) => format!("Battery charge could not be read\n{:?}", e), - } - } else { - format!("Battery: {}", "N/A".bold()) - } -} - - fn calculate_average_usage(cpus: &Vec) -> Result { let mut sum = 0.0; for cpu in cpus { diff --git a/src/power.rs b/src/power.rs index 2ebac2b8..f179380e 100644 --- a/src/power.rs +++ b/src/power.rs @@ -4,6 +4,7 @@ use std::fmt; use std::fs::{read_dir, File}; use std::io::Read; use std::path::Path; +use colored::Colorize; use super::create_issue; use super::Error; @@ -92,6 +93,26 @@ pub fn read_lid_state() -> Result { Ok(state) } +pub fn get_battery_status(charging: bool) -> String { + if has_battery() { + match read_battery_charge() { + Ok(bat) => { + format!( + "Battery: {}", + if charging { + format!("{}%", bat).green() + } else { + format!("{}%", bat).red() + }, + ) + } + Err(e) => format!("Battery charge could not be read\n{:?}", e), + } + } else { + format!("Battery: {}", "N/A".bold()) + } +} + pub fn read_battery_charge() -> Result { let path: &str = match get_best_path(BATTERY_CHARGE_PATH) { Ok(path) => path, From 63a2c14acce95243cff6660aa797c9de51da8e46 Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Mon, 6 Jun 2022 09:46:47 -0700 Subject: [PATCH 3/4] Format --- src/power.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/power.rs b/src/power.rs index f179380e..e4582969 100644 --- a/src/power.rs +++ b/src/power.rs @@ -1,10 +1,10 @@ +use colored::Colorize; use std::any::Any; use std::cmp::PartialEq; use std::fmt; use std::fs::{read_dir, File}; use std::io::Read; use std::path::Path; -use colored::Colorize; use super::create_issue; use super::Error; From faa850e037fb90bd7364126fcb65039398e2bd61 Mon Sep 17 00:00:00 2001 From: jakeroggenbuck Date: Mon, 6 Jun 2022 09:50:54 -0700 Subject: [PATCH 4/4] Remove error for calc --- src/daemon.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 51764e43..421fdfa9 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -108,18 +108,12 @@ fn make_gov_schedutil(cpu: &mut CPU) -> Result<(), Error> { Ok(()) } -// TODO Figure out how to make generic governor work -//fn make_gov_generic(cpu: &mut CPU) -> Result<(), Error> { -// cpu.set_gov(generic_gov.to_string())?; -// Ok(()) -//} - -fn calculate_average_usage(cpus: &Vec) -> Result { +fn calculate_average_usage(cpus: &Vec) -> f32 { let mut sum = 0.0; for cpu in cpus { sum += cpu.cur_usage; } - Ok((sum / (cpus.len() as f32)) as f32) + (sum / (cpus.len() as f32)) as f32 } impl Checker for Daemon { @@ -226,7 +220,7 @@ impl Checker for Daemon { self.charging = read_power_source()?; self.charge = read_battery_charge()?; self.lid_state = read_lid_state()?; - self.usage = calculate_average_usage(&self.cpus)? * 100.0; + self.usage = calculate_average_usage(&self.cpus) * 100.0; Ok(()) }