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

@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed
- Fix missing CLI command help for some supported options
- Fix incorrectly selecting profile by name, where the active profile was being copied to the selected profile
# [3.7.1] - 2021-06-11
### Changed

View File

@@ -489,12 +489,21 @@ fn handle_profile(
}
let mut set_profile = false;
let mut profile;
let mut profile = Profile::default();
if cmd.create {
profile = Profile::default();
set_profile = true;
} else {
profile = dbus.proxies().profile().active_data()?;
} else if let Some(ref name) = cmd.profile {
let profiles = dbus.proxies().profile().all_profile_data()?;
for p in profiles {
if p.name == *name {
profile = p;
break;
}
}
if profile.name != *name {
println!("The requested profile doesn't exist, you may need to create it");
std::process::exit(-1);
}
}
if let Some(turbo) = cmd.turbo {

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();

View File

@@ -8,7 +8,6 @@ use crate::ctrl_gfx::error::GfxError;
#[derive(Debug)]
pub enum RogError {
ParseFanLevel,
ParseVendor,
ParseLed,
MissingProfile(String),
@@ -36,7 +35,6 @@ impl fmt::Display for RogError {
// This trait requires `fmt` with this exact signature.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RogError::ParseFanLevel => write!(f, "Parse profile error"),
RogError::ParseVendor => write!(f, "Parse gfx vendor error"),
RogError::ParseLed => write!(f, "Parse LED error"),
RogError::MissingProfile(profile) => write!(f, "Profile does not exist {}", profile),

View File

@@ -6,7 +6,6 @@ use rog_fan_curve::CurveError;
#[derive(Debug)]
pub enum ProfileError {
ParseFanLevel,
MissingProfile(String),
Path(String, std::io::Error),
Read(String, std::io::Error),
Write(String, std::io::Error),
@@ -23,9 +22,6 @@ impl fmt::Display for ProfileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ProfileError::ParseFanLevel => write!(f, "Parse profile error"),
ProfileError::MissingProfile(profile) => {
write!(f, "Profile does not exist {}", profile)
}
ProfileError::Path(path, error) => write!(f, "Path {}: {}", path, error),
ProfileError::Read(path, error) => write!(f, "Read {}: {}", path, error),
ProfileError::Write(path, error) => write!(f, "Write {}: {}", path, error),