Skip to content

Commit

Permalink
优化路径匹配逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
xml committed Apr 15, 2024
1 parent e8991c8 commit 8215730
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ impl Battery {

pub fn enumerate() -> Vec<Battery> {
fn is_battery(entry: &walkdir::DirEntry) -> bool {
if entry.path().to_str().unwrap() == ROOTPATH {
return false;
}
let realpath = if entry.file_type().is_symlink() {
let paths = std::fs::read_link(entry.path()).unwrap();
std::path::Path::new(ROOTPATH).join(paths).canonicalize().unwrap()
Expand Down
21 changes: 21 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ fn show_dim_info() {

fn show_cpu_info() {
fn is_cpuname(entry: &walkdir::DirEntry) -> bool {
if entry.path().to_str().unwrap() == "/sys/devices/system/cpu" {
return false;
}
entry.file_name()
.to_str()
.map(|s| {
Expand Down Expand Up @@ -146,6 +149,9 @@ fn show_audio_info() {

fn show_graphics_info() {
fn is_cardname(entry: &walkdir::DirEntry) -> bool {
if entry.path().to_str().unwrap() == "/sys/class/drm" {
return false;
}
let realpath = if entry.file_type().is_symlink() {
let paths = std::fs::read_link(entry.path()).unwrap();
std::path::Path::new("/sys/class/drm").join(paths).canonicalize().unwrap()
Expand Down Expand Up @@ -189,6 +195,9 @@ fn show_suspend_info() {

fn disk_info() {
fn is_scsihost_name(entry: &walkdir::DirEntry) -> bool {
if entry.path().to_str().unwrap() == "/sys/class/scsi_host" {
return false;
}
entry.file_name()
.to_str()
.map(|s| {
Expand All @@ -212,6 +221,9 @@ fn disk_info() {
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_dir() || e.file_type().is_symlink()) {
if entry.path().to_str().unwrap() == "/sys/bus/pci/devices" {
return;
}
let full_path = entry.path().to_str().expect("is not path").to_string();
read_line_with_echo(&(full_path.clone() + "/power/control"));
}
Expand All @@ -221,6 +233,9 @@ fn disk_info() {
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_dir() || e.file_type().is_symlink()) {
if entry.path().to_str().unwrap() == "/sys/block" {
return;
}
if !entry.file_name().to_str().unwrap().starts_with("loop") {
let full_path = entry.path().to_str().expect("is not path").to_string();
read_line_with_echo(&(full_path.clone() + "/device/power/control"));
Expand Down Expand Up @@ -248,6 +263,9 @@ fn wakeup_info() {
.into_iter()
.filter_map(Result::ok)
.filter(|e| e.file_type().is_dir() || e.file_type().is_symlink()) {
if entry.path().to_str().unwrap() == "/sys/class/net" {
return;
}
let name = entry.file_name().to_str().unwrap().to_string();
if name != "lo" && !name.starts_with("docker") {
let full_path = entry.path().to_str().expect("is not path").to_string();
Expand All @@ -260,6 +278,9 @@ fn wakeup_info() {
.into_iter()
.filter_map(Result::ok)
.filter(|e| (e.file_type().is_dir() || e.file_type().is_symlink()) && is_usb_name(e)) {
if entry.path().to_str().unwrap() == "/sys/bus/usb/devices" {
return;
}
let full_path = entry.path().to_str().expect("is not path").to_string();
read_line_with_echo(&(full_path.clone() + "/power/wakeup"));
read_line_with_echo(&(full_path.clone() + "/link_power_management_policy"));
Expand Down
3 changes: 3 additions & 0 deletions src/thermal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ impl Thermal {

pub fn enumerate() -> Vec<Thermal> {
fn is_thermal(entry: &walkdir::DirEntry) -> bool {
if entry.path().to_str().unwrap() == ROOTPATH {
return false;
}
let realpath = if entry.file_type().is_symlink() {
let paths = std::fs::read_link(entry.path()).unwrap();
std::path::Path::new(ROOTPATH).join(paths).canonicalize().unwrap()
Expand Down

0 comments on commit 8215730

Please sign in to comment.