refactor(src/commands): enhance battery command error handling and readability
Restructure loops, add explicit error handling, improve multi‑line messages, and fix formatting issues. - Enhancements - Use explicit braces for error branches to prevent silent failures - Wrap warning and informational messages in multi‑line `println!` with consistent spacing - Refactor directory reads for Ideapad fallback to avoid nested `let` bindings - Improve parsing and formatting of AMD TDP limit output with clear parentheses - Provide clearer debug logging when writing to threshold files - Bug Fixes - Added missing braces around error handling to ensure proper execution flow - Fixed variable binding scope for `ideapad_entries` to prevent shadowing - Ensured consistent handling of missing `charge_control_end_threshold` across all entries no changes.
This commit is contained in:
7 files changed
+242
-80
No files matched your search
+77
-22
@@ -1,5 +1,7 @@
|
||||
use crate::cli::GpuCommands;
|
||||
use crate::hardware::gpu::{get_amd_igpu_name, get_current_mode, get_igpu_vendor, get_nvidia_gpu_pci_bus};
|
||||
use crate::hardware::gpu::{
|
||||
get_amd_igpu_name, get_current_mode, get_igpu_vendor, get_nvidia_gpu_pci_bus,
|
||||
};
|
||||
use crate::utils::system::{assert_root, create_file, get_display_manager, rebuild_initramfs};
|
||||
use log::{error, info};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -116,7 +118,8 @@ done
|
||||
|
||||
// Helpers
|
||||
fn xorg_intel(bus_id: &str) -> String {
|
||||
format!(r#"# Automatically generated by lapctl
|
||||
format!(
|
||||
r#"# Automatically generated by lapctl
|
||||
|
||||
Section "ServerLayout"
|
||||
Identifier "layout"
|
||||
@@ -145,11 +148,14 @@ Section "Screen"
|
||||
Identifier "intel"
|
||||
Device "intel"
|
||||
EndSection
|
||||
"#, bus_id)
|
||||
"#,
|
||||
bus_id
|
||||
)
|
||||
}
|
||||
|
||||
fn xorg_amd(bus_id: &str) -> String {
|
||||
format!(r#"# Automatically generated by lapctl
|
||||
format!(
|
||||
r#"# Automatically generated by lapctl
|
||||
|
||||
Section "ServerLayout"
|
||||
Identifier "layout"
|
||||
@@ -178,7 +184,9 @@ Section "Screen"
|
||||
Identifier "amd"
|
||||
Device "amdgpu"
|
||||
EndSection
|
||||
"#, bus_id)
|
||||
"#,
|
||||
bus_id
|
||||
)
|
||||
}
|
||||
|
||||
fn generate_xrandr_script(igpu_vendor: Option<&String>) -> String {
|
||||
@@ -244,7 +252,9 @@ struct Cache {
|
||||
}
|
||||
|
||||
fn create_cache_obj(bus_id: String) -> Cache {
|
||||
Cache { nvidia_gpu_pci_bus: bus_id }
|
||||
Cache {
|
||||
nvidia_gpu_pci_bus: bus_id,
|
||||
}
|
||||
}
|
||||
|
||||
fn create_cache_file() {
|
||||
@@ -273,7 +283,10 @@ fn read_cache_file() -> Result<Cache, String> {
|
||||
} else if get_current_mode() == "hybrid" {
|
||||
Ok(create_cache_obj(get_nvidia_gpu_pci_bus()))
|
||||
} else {
|
||||
Err("No cache present. Operation requires that the system be in the hybrid Optimus mode".into())
|
||||
Err(
|
||||
"No cache present. Operation requires that the system be in the hybrid Optimus mode"
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,7 +323,10 @@ fn switch_integrated() {
|
||||
fn switch_hybrid(rtd3: Option<u8>, use_nvidia_current: bool) {
|
||||
assert_root();
|
||||
println!("Switching to hybrid mode");
|
||||
println!("Enable PCI-Express Runtime D3 (RTD3) Power Management: {}", rtd3.is_some());
|
||||
println!(
|
||||
"Enable PCI-Express Runtime D3 (RTD3) Power Management: {}",
|
||||
rtd3.is_some()
|
||||
);
|
||||
|
||||
if get_current_mode() == "hybrid" && !Path::new(CACHE_FILE_PATH).exists() {
|
||||
create_cache_file();
|
||||
@@ -333,9 +349,15 @@ fn switch_hybrid(rtd3: Option<u8>, use_nvidia_current: bool) {
|
||||
|
||||
if let Some(val) = rtd3 {
|
||||
let modeset_content = if use_nvidia_current {
|
||||
format!("# Automatically generated by lapctl\n\noptions nvidia-current-drm modeset=1\noptions nvidia-current \"NVreg_DynamicPowerManagement=0x0{}\"\noptions nvidia-current NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n", val)
|
||||
format!(
|
||||
"# Automatically generated by lapctl\n\noptions nvidia-current-drm modeset=1\noptions nvidia-current \"NVreg_DynamicPowerManagement=0x0{}\"\noptions nvidia-current NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n",
|
||||
val
|
||||
)
|
||||
} else {
|
||||
format!("# Automatically generated by lapctl\n\noptions nvidia-drm modeset=1\noptions nvidia \"NVreg_DynamicPowerManagement=0x0{}\"\noptions nvidia NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n", val)
|
||||
format!(
|
||||
"# Automatically generated by lapctl\n\noptions nvidia-drm modeset=1\noptions nvidia \"NVreg_DynamicPowerManagement=0x0{}\"\noptions nvidia NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n",
|
||||
val
|
||||
)
|
||||
};
|
||||
create_file(MODESET_PATH, &modeset_content, false);
|
||||
create_file(UDEV_PM_PATH, UDEV_PM_CONTENT, false);
|
||||
@@ -381,23 +403,35 @@ fn switch_nvidia(
|
||||
if get_current_mode() == "hybrid" && !Path::new(CACHE_FILE_PATH).exists() {
|
||||
create_cache_file();
|
||||
}
|
||||
|
||||
|
||||
let cache = read_cache_file();
|
||||
let pci_bus = match cache {
|
||||
Ok(c) => c.nvidia_gpu_pci_bus,
|
||||
Err(_) => get_nvidia_gpu_pci_bus()
|
||||
Err(_) => get_nvidia_gpu_pci_bus(),
|
||||
};
|
||||
|
||||
cleanup();
|
||||
|
||||
if use_nvidia_current {
|
||||
create_file(MODESET_PATH, "# Automatically generated by lapctl\n\noptions nvidia-current-drm modeset=1\noptions nvidia-current NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n", false);
|
||||
create_file(
|
||||
MODESET_PATH,
|
||||
"# Automatically generated by lapctl\n\noptions nvidia-current-drm modeset=1\noptions nvidia-current NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n",
|
||||
false,
|
||||
);
|
||||
} else {
|
||||
create_file(MODESET_PATH, "# Automatically generated by lapctl\n\noptions nvidia-drm modeset=1\noptions nvidia NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n", false);
|
||||
create_file(
|
||||
MODESET_PATH,
|
||||
"# Automatically generated by lapctl\n\noptions nvidia-drm modeset=1\noptions nvidia NVreg_UsePageAttributeTable=1 NVreg_InitializeSystemMemoryAllocations=0\n",
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
if wayland {
|
||||
create_file("/etc/lapctl-wayland-nvidia", "# Wayland mode marker for lapctl\n", false);
|
||||
create_file(
|
||||
"/etc/lapctl-wayland-nvidia",
|
||||
"# Wayland mode marker for lapctl\n",
|
||||
false,
|
||||
);
|
||||
} else {
|
||||
let igpu_vendor = get_igpu_vendor();
|
||||
if let Some(ref vendor) = igpu_vendor {
|
||||
@@ -430,9 +464,17 @@ fn switch_nvidia(
|
||||
create_file(&format!("{}.bak", SDDM_XSETUP_PATH), &content, false);
|
||||
}
|
||||
}
|
||||
create_file(SDDM_XSETUP_PATH, &generate_xrandr_script(igpu_vendor.as_ref()), true);
|
||||
create_file(
|
||||
SDDM_XSETUP_PATH,
|
||||
&generate_xrandr_script(igpu_vendor.as_ref()),
|
||||
true,
|
||||
);
|
||||
} else if cdm == "lightdm" {
|
||||
create_file(LIGHTDM_SCRIPT_PATH, &generate_xrandr_script(igpu_vendor.as_ref()), true);
|
||||
create_file(
|
||||
LIGHTDM_SCRIPT_PATH,
|
||||
&generate_xrandr_script(igpu_vendor.as_ref()),
|
||||
true,
|
||||
);
|
||||
create_file(LIGHTDM_CONFIG_PATH, LIGHTDM_CONFIG_CONTENT, false);
|
||||
}
|
||||
}
|
||||
@@ -471,7 +513,7 @@ pub fn execute(cmd: &GpuCommands) {
|
||||
}
|
||||
GpuCommands::Reset => {
|
||||
assert_root();
|
||||
|
||||
|
||||
if get_current_mode() == "hybrid" && !Path::new(CACHE_FILE_PATH).exists() {
|
||||
create_cache_file();
|
||||
}
|
||||
@@ -492,9 +534,22 @@ pub fn execute(cmd: &GpuCommands) {
|
||||
println!("Operation completed successfully");
|
||||
}
|
||||
GpuCommands::Integrated => switch_integrated(),
|
||||
GpuCommands::Hybrid { rtd3, use_nvidia_current } => switch_hybrid(rtd3.clone(), *use_nvidia_current),
|
||||
GpuCommands::Nvidia { dm, force_comp, coolbits, use_nvidia_current, wayland } => {
|
||||
switch_nvidia(dm.clone(), *force_comp, coolbits.clone(), *use_nvidia_current, *wayland)
|
||||
}
|
||||
GpuCommands::Hybrid {
|
||||
rtd3,
|
||||
use_nvidia_current,
|
||||
} => switch_hybrid(rtd3.clone(), *use_nvidia_current),
|
||||
GpuCommands::Nvidia {
|
||||
dm,
|
||||
force_comp,
|
||||
coolbits,
|
||||
use_nvidia_current,
|
||||
wayland,
|
||||
} => switch_nvidia(
|
||||
dm.clone(),
|
||||
*force_comp,
|
||||
coolbits.clone(),
|
||||
*use_nvidia_current,
|
||||
*wayland,
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user