refactor(src/commands): simplify validation and file handling

- New Features
  - None
- Enhancements
  - Simplified percent range validation for battery mode.
  - Combined existence and read checks for sysfs entries, removing redundant branches.
  - Streamlined systemctl command arguments and removed unnecessary references.
  - Removed superfluous braces and simplified cooling profile detection logic.
  - Improved robustness of sysfs parsing for Intel RAPL and AMD hwmon temperature limits.
- Bug Fixes
  - Fixed battery percent validation to use inclusive range (1..=100) instead of incorrect reference comparison.
This commit is contained in:
sankalp committed 2026-03-12 00:42:26 +05:45
1 parent ca9390110e
commit 56a602e0a6
4 files changed
+37 -53

No files matched your search

+18 -29
View File
@@ -38,15 +38,14 @@ pub fn execute() {
{
for ideapad_entry in ideapad_entries.flatten() {
let conservation_path = ideapad_entry.path().join("conservation_mode");
if conservation_path.exists() {
if let Ok(mode) = fs::read_to_string(&conservation_path) {
if conservation_path.exists()
&& let Ok(mode) = fs::read_to_string(&conservation_path) {
if mode.trim() == "1" {
println!(" Charge Limit: Conservation Mode (~60%)");
} else {
println!(" Charge Limit: 100%");
}
}
}
}
}
}
@@ -66,8 +65,8 @@ pub fn execute() {
if let Ok(ideapad_entries) = fs::read_dir("/sys/bus/platform/drivers/ideapad_acpi") {
for ideapad_entry in ideapad_entries.flatten() {
let fan_path = ideapad_entry.path().join("fan_mode");
if fan_path.exists() {
if let Ok(mode) = fs::read_to_string(&fan_path) {
if fan_path.exists()
&& let Ok(mode) = fs::read_to_string(&fan_path) {
thermal_found = true;
let desc = match mode.trim() {
"1" => "Performance",
@@ -77,13 +76,12 @@ pub fn execute() {
};
println!("Cooling Profile: {} (Ideapad)", desc);
}
}
}
}
let asus_path = Path::new("/sys/devices/platform/asus-nb-wmi/throttle_thermal_policy");
if asus_path.exists() {
if let Ok(mode) = fs::read_to_string(asus_path) {
if asus_path.exists()
&& let Ok(mode) = fs::read_to_string(asus_path) {
thermal_found = true;
let desc = match mode.trim() {
"1" => "Performance",
@@ -93,7 +91,6 @@ pub fn execute() {
};
println!("Cooling Profile: {} (ASUS)", desc);
}
}
if !thermal_found {
println!("Cooling Profile: Unknown / Firmware managed");
@@ -104,43 +101,40 @@ pub fn execute() {
// Check Intel RAPL
let rapl_dir = Path::new("/sys/class/powercap/intel-rapl");
if rapl_dir.exists() {
if let Ok(entries) = fs::read_dir(rapl_dir) {
if rapl_dir.exists()
&& let Ok(entries) = fs::read_dir(rapl_dir) {
for entry in entries.flatten() {
let path = entry.path();
let name = path.file_name().unwrap_or_default().to_string_lossy();
if name.starts_with("intel-rapl:") {
let constraint_0 = path.join("constraint_0_power_limit_uw"); // PL1
if constraint_0.exists() {
if let Ok(limit_uw) = fs::read_to_string(&constraint_0) {
if let Ok(limit) = limit_uw.trim().parse::<u64>() {
if constraint_0.exists()
&& let Ok(limit_uw) = fs::read_to_string(&constraint_0)
&& let Ok(limit) = limit_uw.trim().parse::<u64>() {
println!("CPU TDP Limit: {}W (Intel RAPL PL1)", limit / 1_000_000);
tdp_found = true;
break;
}
}
}
}
}
}
}
// Check AMD HWMon
if !tdp_found {
let hwmon_dir = Path::new("/sys/class/hwmon");
if hwmon_dir.exists() {
if let Ok(entries) = fs::read_dir(hwmon_dir) {
if hwmon_dir.exists()
&& let Ok(entries) = fs::read_dir(hwmon_dir) {
for entry in entries.flatten() {
let path = entry.path();
let power1_cap = path.join("power1_cap");
let name_path = path.join("name");
if power1_cap.exists() {
if let Ok(name) = fs::read_to_string(&name_path) {
if name.trim().contains("amd") {
if let Ok(limit_uw) = fs::read_to_string(&power1_cap) {
if let Ok(limit) = limit_uw.trim().parse::<u64>() {
if power1_cap.exists()
&& let Ok(name) = fs::read_to_string(&name_path)
&& name.trim().contains("amd")
&& let Ok(limit_uw) = fs::read_to_string(&power1_cap)
&& let Ok(limit) = limit_uw.trim().parse::<u64>() {
println!(
"CPU TDP Limit: {}W (AMD hwmon)",
limit / 1_000_000
@@ -148,13 +142,8 @@ pub fn execute() {
tdp_found = true;
break;
}
}
}
}
}
}
}
}
}
if !tdp_found {