feat(src/commands): implement battery limit command with validation and enhance status output
The changes introduce a robust `BatteryCommands::Limit` implementation that validates user‑provided percentages, writes charge‑control thresholds, and gracefully handles Lenovo Ideapad firmware via the `conservation_mode` fallback. The `lapctl status` command is also updated to display battery‑specific details such as capacity, status, and charge limits, replacing the previous generic placeholder. New Features - Added `BatteryCommands::Limit` handling with range validation (1‑100) and support for multiple `/sys/class/power_supply` entries. - Implemented Ideapad fallback using `conservation_mode` to provide custom charge limits or warnings when unsupported. - Extended `lapctl status` to show charge limit and Conservation Mode for each battery. Enhancements - Added root‑privilege check (`assert_root`) and improved error/debug logging throughout the command. - Restructured directory traversal logic for readability and added `debug!` traces on write failures. - Re‑used status logic to present battery‑specific information instead of a generic placeholder. Bug Fixes - Resolved placeholder status output and proper handling of missing `charge_control_end_threshold` files. - Ensured correct exit codes and clear error messages for invalid percentage inputs.
This commit is contained in:
2 files changed
+122
-3
No files matched your search
+15
-1
@@ -26,9 +26,23 @@ pub fn execute() {
|
||||
println!(" Capacity: {}%", capacity.trim());
|
||||
println!(" Status: {}", status.trim());
|
||||
|
||||
// Print charing limit if exists (e.g., charge_control_end_threshold)
|
||||
if let Ok(limit) = fs::read_to_string(bat_path.join("charge_control_end_threshold")) {
|
||||
println!(" Charge Limit: {}%", limit.trim());
|
||||
} else {
|
||||
if let Ok(ideapad_entries) = fs::read_dir("/sys/bus/platform/drivers/ideapad_acpi") {
|
||||
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 mode.trim() == "1" {
|
||||
println!(" Charge Limit: Conservation Mode (~60%)");
|
||||
} else {
|
||||
println!(" Charge Limit: 100%");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user