chore: apply recommendations from clippy

This commit is contained in:
Cheong Lau 2025-10-04 10:51:18 +10:00 committed by GitHub
parent cec55dafd7
commit 8e0f1c4a09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 720 additions and 824 deletions

View file

@ -79,7 +79,7 @@ async fn is_desktop() -> bool {
}
async fn powered_on(path: impl AsRef<Path>) -> bool {
let Some(component) = path.as_ref().components().last() else {
let Some(component) = path.as_ref().components().next_back() else {
return true;
};
let name_str = component.as_os_str();
@ -87,7 +87,7 @@ async fn powered_on(path: impl AsRef<Path>) -> bool {
return true;
};
let Ok(state) =
tokio::fs::read_to_string(format!("/sys/class/drm/{}/device/power_state", name)).await
tokio::fs::read_to_string(format!("/sys/class/drm/{name}/device/power_state")).await
else {
return true;
};
@ -201,120 +201,117 @@ impl Gpu {
}
async fn app_list(&self, running_apps: &[RunningApp]) -> Option<Vec<Entry>> {
match self.driver.as_ref().and_then(|s| s.to_str()) {
Some("nvidia") => {
// figure out bus path for calling nvidia-smi
let mut sys_path = PathBuf::from("/sys/class/drm");
sys_path.push(self.path.components().last()?.as_os_str());
let buslink = std::fs::read_link(sys_path)
.ok()?
.components()
.rev()
.nth(2)?
.as_os_str()
.to_string_lossy()
.into_owned();
if let Some("nvidia") = self.driver.as_ref().and_then(|s| s.to_str()) {
// figure out bus path for calling nvidia-smi
let mut sys_path = PathBuf::from("/sys/class/drm");
sys_path.push(self.path.components().next_back()?.as_os_str());
let buslink = std::fs::read_link(sys_path)
.ok()?
.components()
.rev()
.nth(2)?
.as_os_str()
.to_string_lossy()
.into_owned();
let smi_output = match tokio::process::Command::new("nvidia-smi")
.args(["pmon", "--id", &buslink, "--count", "1"])
.output()
.await
{
Ok(output) if output.status.success() => {
String::from_utf8_lossy(&output.stdout).into_owned()
}
Ok(output) => {
debug!(
"smi returned error code {}: {}",
output.status,
String::from_utf8_lossy(&output.stdout)
);
return None;
}
Err(err) => {
debug!("smi returned error code: {}", err);
return None;
}
};
let smi_output = match tokio::process::Command::new("nvidia-smi")
.args(["pmon", "--id", &buslink, "--count", "1"])
.output()
.await
{
Ok(output) if output.status.success() => {
String::from_utf8_lossy(&output.stdout).into_owned()
}
Ok(output) => {
debug!(
"smi returned error code {}: {}",
output.status,
String::from_utf8_lossy(&output.stdout)
);
return None;
}
Err(err) => {
debug!("smi returned error code: {}", err);
return None;
}
};
Some(
smi_output
.lines()
.filter(|line| {
// smi shows an empty line filled with - when no app is running
let components = line.split_whitespace().collect::<Vec<_>>();
components[1].trim().ne("-") && !line.starts_with('#')
})
.map(|line| {
let components = line.split_whitespace().collect::<Vec<_>>();
let pid = components[1].trim();
let process_name = components.last().unwrap().trim();
Some(
smi_output
.lines()
.filter(|line| {
// smi shows an empty line filled with - when no app is running
let components = line.split_whitespace().collect::<Vec<_>>();
components[1].trim().ne("-") && !line.starts_with('#')
})
.map(|line| {
let components = line.split_whitespace().collect::<Vec<_>>();
let pid = components[1].trim();
let process_name = components.last().unwrap().trim();
if let Some(application) = running_apps
.iter()
.find(|running_app| running_app.executable_name == process_name)
{
Entry {
name: application.name.clone(),
icon: application.icon.clone(),
secondary: String::new(),
}
} else {
Entry {
name: process_name.to_string(),
icon: None,
secondary: pid.to_string(),
}
if let Some(application) = running_apps
.iter()
.find(|running_app| running_app.executable_name == process_name)
{
Entry {
name: application.name.clone(),
icon: application.icon.clone(),
secondary: String::new(),
}
})
.collect(),
)
}
_ => {
let lsof_output = match tokio::process::Command::new("lsof")
.args([OsStr::new("-t"), self.path.as_os_str()])
.output()
.await
{
Ok(output) => String::from_utf8_lossy(&output.stdout).into_owned(),
Err(err) => {
debug!("lsof returned error code: {}", err);
return None;
}
};
Some(
lsof_output
.lines()
.filter_map(|pid| {
let executable = std::fs::read_link(format!("/proc/{}/exe", pid))
.ok()?
.components()
.last()?
.as_os_str()
.to_string_lossy()
.into_owned();
if let Some(application) = running_apps
.iter()
.find(|running_app| running_app.executable_name == executable)
{
Some(Entry {
name: application.name.clone(),
icon: application.icon.clone(),
secondary: String::new(),
})
} else {
Some(Entry {
name: executable,
icon: None,
secondary: pid.to_string(),
})
} else {
Entry {
name: process_name.to_string(),
icon: None,
secondary: pid.to_string(),
}
})
.collect(),
)
}
}
})
.collect(),
)
} else {
let lsof_output = match tokio::process::Command::new("lsof")
.args([OsStr::new("-t"), self.path.as_os_str()])
.output()
.await
{
Ok(output) => String::from_utf8_lossy(&output.stdout).into_owned(),
Err(err) => {
debug!("lsof returned error code: {err}");
return None;
}
};
Some(
lsof_output
.lines()
.filter_map(|pid| {
let executable = std::fs::read_link(format!("/proc/{pid}/exe"))
.ok()?
.components()
.next_back()?
.as_os_str()
.to_string_lossy()
.into_owned();
if let Some(application) = running_apps
.iter()
.find(|running_app| running_app.executable_name == executable)
{
Some(Entry {
name: application.name.clone(),
icon: application.icon.clone(),
secondary: String::new(),
})
} else {
Some(Entry {
name: executable,
icon: None,
secondary: pid.to_string(),
})
}
})
.collect(),
)
}
}
}
@ -325,14 +322,14 @@ fn all_gpus<S: AsRef<str>>(seat: S) -> io::Result<Vec<Gpu>> {
enumerator.match_sysname("card[0-9]*")?;
let mut gpus = enumerator
.scan_devices()?
.filter(|device| {
device
.filter_map(|device| {
if device
.property_value("ID_SEAT")
.map(|x| x.to_os_string())
.unwrap_or_else(|| OsString::from("seat0"))
== *seat.as_ref()
})
.flat_map(|device| {
.unwrap_or_else(|| OsStr::new("seat0"))
!= seat.as_ref()
{
return None;
}
let path = device.devnode().map(PathBuf::from)?;
let node = DrmNode::from_path(&path).ok()?;
if !node.has_render() {
@ -340,11 +337,8 @@ fn all_gpus<S: AsRef<str>>(seat: S) -> io::Result<Vec<Gpu>> {
}
let boot_vga = if let Ok(Some(pci)) = device.parent_with_subsystem(Path::new("pci")) {
if let Some(value) = pci.attribute_value("boot_vga") {
value == "1"
} else {
false
}
pci.attribute_value("boot_vga")
.is_some_and(|value| value == "1")
} else {
false
};
@ -352,10 +346,10 @@ fn all_gpus<S: AsRef<str>>(seat: S) -> io::Result<Vec<Gpu>> {
let name = if let Some(parent) = device.parent() {
let vendor = parent
.property_value("SWITCHEROO_CONTROL_VENDOR_NAME")
.or_else(|| parent.property_value("ID_VENDOR_FROM_DATABASE"));
.or(parent.property_value("ID_VENDOR_FROM_DATABASE"));
let name = parent
.property_value("SWITCHEROO_CONTROL_PRODUCT_NAME")
.or_else(|| parent.property_value("ID_MODEL_FROM_DATABASE"));
.or(parent.property_value("ID_MODEL_FROM_DATABASE"));
if vendor.is_none() && name.is_none() {
String::from("Unknown GPU")
@ -374,7 +368,7 @@ fn all_gpus<S: AsRef<str>>(seat: S) -> io::Result<Vec<Gpu>> {
let driver = loop {
if let Some(dev) = device {
if dev.driver().is_some() {
break dev.driver().map(std::ffi::OsStr::to_os_string);
break dev.driver().map(OsStr::to_os_string);
} else {
device = dev.parent();
}
@ -412,8 +406,8 @@ fn all_gpus<S: AsRef<str>>(seat: S) -> io::Result<Vec<Gpu>> {
})
})
})
.or_else(|| gpus.iter().position(|gpu| gpu.boot_vga))
.or_else(|| (gpus.len() == 1).then_some(0))
.or(gpus.iter().position(|gpu| gpu.boot_vga))
.or((gpus.len() == 1).then_some(0))
{
gpus[primary_idx].primary = true;
}
@ -487,10 +481,10 @@ async fn start_listening(
let name = if let Some(parent) = device.parent() {
let vendor = parent
.property_value("SWITCHEROO_CONTROL_VENDOR_NAME")
.or_else(|| parent.property_value("ID_VENDOR_FROM_DATABASE"));
.or(parent.property_value("ID_VENDOR_FROM_DATABASE"));
let name = parent
.property_value("SWITCHEROO_CONTROL_PRODUCT_NAME")
.or_else(|| parent.property_value("ID_MODEL_FROM_DATABASE"));
.or(parent.property_value("ID_MODEL_FROM_DATABASE"));
if vendor.is_none() && name.is_none() {
String::from("Unknown GPU")
@ -509,7 +503,7 @@ async fn start_listening(
let driver = loop {
if let Some(dev) = device {
if dev.driver().is_some() {
break dev.driver().map(std::ffi::OsStr::to_os_string);
break dev.driver().map(OsStr::to_os_string);
} else {
device = dev.parent();
}