fix: Profile select by name is correctly choosing

Closes #100 #96
This commit is contained in:
Luke D. Jones
2021-08-01 00:04:16 +12:00
parent fb124dd228
commit 29c26e8c89
5 changed files with 38 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
use crate::error::RogError;
use crate::{config::Config, GetSupported};
use log::info;
use log::{info, warn};
use rog_profiles::error::ProfileError;
use rog_profiles::profiles::Profile;
use rog_types::supported::FanCpuSupportedFunctions;
use std::sync::Arc;
@@ -23,12 +24,12 @@ impl GetSupported for CtrlFanAndCpu {
}
impl crate::Reloadable for CtrlFanAndCpu {
/// Fetcht he active profile and use that to set all related components up
fn reload(&mut self) -> Result<(), RogError> {
if let Ok(mut cfg) = self.config.clone().try_lock() {
let active = cfg.active_profile.clone();
if let Some(existing) = cfg.power_profiles.get_mut(&active) {
existing.set_system_all()?;
cfg.write();
}
}
Ok(())
@@ -38,31 +39,38 @@ impl crate::Reloadable for CtrlFanAndCpu {
impl CtrlFanAndCpu {
pub fn new(config: Arc<Mutex<Config>>) -> Result<Self, RogError> {
Profile::get_fan_path()?;
info!("Device has thermal throttle control");
info!("Device has fan control available");
Ok(CtrlFanAndCpu { config })
}
/// Toggle to next profile in list
pub(super) fn do_next_profile(&mut self) -> Result<(), RogError> {
if let Ok(mut config) = self.config.clone().try_lock() {
// Read first just incase the user has modified the config before calling this
config.read();
let mut i = config
let mut toggle_index = config
.toggle_profiles
.binary_search(&config.active_profile)
.unwrap_or(0)
+ 1;
if i >= config.toggle_profiles.len() {
i = 0;
if toggle_index >= config.toggle_profiles.len() {
toggle_index = 0;
}
let profile = config.toggle_profiles[i].clone();
let profile = config.toggle_profiles[toggle_index].clone();
if let Some(existing) = config.power_profiles.get(&profile) {
existing.set_system_all()?;
config.active_profile = existing.name.clone();
config.write();
info!("Profile was changed to: {}", profile);
info!("Profile was changed to: {}", &profile);
} else {
warn!(
"toggle_profile {} does not exist in power_profiles",
&profile
);
return Err(RogError::MissingProfile(profile.to_string()));
}
}
Ok(())
@@ -70,17 +78,25 @@ impl CtrlFanAndCpu {
pub(super) fn set_active(&mut self, profile: &str) -> Result<(), RogError> {
if let Ok(mut config) = self.config.clone().try_lock() {
// Read first just incase the user has modified the config before calling this
config.read();
if let Some(existing) = config.power_profiles.get(profile) {
existing.set_system_all()?;
config.active_profile = existing.name.clone();
config.write();
info!("Profile was changed to: {}", profile);
} else {
warn!(
"toggle_profile {} does not exist in power_profiles",
profile
);
return Err(RogError::MissingProfile(profile.to_string()));
}
}
Ok(())
}
/// Create a new profile if the requested name doesn't exist, or modify existing
pub(super) fn new_or_modify(&mut self, profile: &Profile) -> Result<(), RogError> {
if let Ok(mut config) = self.config.clone().try_lock() {
config.read();