Skip to content

Commit

Permalink
Added heuristics to system icons (#35)
Browse files Browse the repository at this point in the history
system entries are usually shell commands, which don't correspond to a
single file on disk. This commit adds heuristics for identifying the
icons for these system entries, so that the first (space deliminated)
word is checked to see if it has a standalone icon.
  • Loading branch information
apgoetz authored Aug 7, 2023
1 parent 79af12d commit 2b94f48
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ once_cell = "1.18.0"
resvg = "0.34.0"
env_logger = "0.10.0"
log = "0.4.19"
which = "4.4.0"

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
Expand Down
4 changes: 2 additions & 2 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl StoreEntry {
icon::IconType::custom(p)
} else {
match &entry {
EntryType::SystemEntry(loc) => icon::IconType::file(loc),
EntryType::SystemEntry(loc) => icon::IconType::system(loc),
EntryType::FileEntry(loc) => {
let parsed_loc = format_param(loc, "");

Expand Down Expand Up @@ -683,7 +683,7 @@ mod tests {
.map(str::to_string)
.collect(),
icon: None,
icon_type: IconType::file("foo bar"),
icon_type: IconType::system("foo bar"),
};

let entry = parse_entry(&toml);
Expand Down
73 changes: 73 additions & 0 deletions src/icon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ trait IconInterface {
Err("is unsupported icon type".into())
}
}
IconVariant::System(command) => {
// heuristic: dont use full path but only first
// word in system entry
use which::which;

if let Some(exe) = command.split(" ").next().and_then(|e| which(e).ok()) {
self.try_load_icon(IconType::file(exe))
} else if let Some(exe) = command
.split(" ")
.next()
.and_then(|exe| self.try_load_icon(IconType::file(exe)).ok())
{
Ok(exe)
} else {
self.try_load_icon(IconType::file(command))
}
}
}
}
}
Expand Down Expand Up @@ -195,6 +212,10 @@ impl IconType {
pub fn file<P: AsRef<std::path::Path>>(path: P) -> Self {
Self(IconVariant::File(path.as_ref().into()))
}

pub fn system<S: ToString>(cmd: S) -> Self {
Self(IconVariant::System(cmd.to_string()))
}
}

// represents the necessary information in an entry to look up an icon
Expand All @@ -206,6 +227,8 @@ enum IconVariant {
Url(url::Url),
// render using icon for path
File(std::path::PathBuf),
// Like a file, but uses heuristics in case command has arguments
System(String),
// override "normal" icon and use icon from this path
CustomIcon(std::path::PathBuf),
}
Expand All @@ -216,6 +239,7 @@ impl Hash for IconVariant {
IconVariant::Url(u) => u.scheme().hash(state),
IconVariant::File(p) => p.hash(state),
IconVariant::CustomIcon(p) => p.hash(state),
IconVariant::System(p) => p.hash(state),
}
}
}
Expand Down Expand Up @@ -245,6 +269,13 @@ impl PartialEq for IconVariant {
false
}
}
IconVariant::System(s) => {
if let IconVariant::System(o) = other {
s == o
} else {
false
}
}
}
}
}
Expand Down Expand Up @@ -384,6 +415,8 @@ fn icon_from_svg(path: &std::path::Path) -> Result<Icon, IconError> {

#[cfg(test)]
mod tests {
use crate::icon::IconType;

use super::{Icon, IconError, IconInterface, IconSettings};
use iced::advanced::image;

Expand Down Expand Up @@ -612,4 +645,44 @@ mod tests {
if *w == DEFAULT_ICON_SIZE as u32 && *h == DEFAULT_ICON_SIZE as u32
));
}

#[test]
fn system_entry_heuristic() {
use tempfile::tempdir;

#[cfg(windows)]
let exe = "cmd.exe";
#[cfg(not(windows))]
let exe = "echo";

let os = IconSettings::default();

os.try_load_icon(IconType::system(format!("{exe}")))
.unwrap();
os.try_load_icon(IconType::system(format!("{exe} a b c")))
.unwrap();

let dir = tempdir().unwrap();

let exe = dir.path().join("test.txt").display().to_string();
std::fs::File::create(&exe).unwrap();

assert!(
!exe.contains(" "),
"test requires that temp dir path not have spaces: actual path: {exe}"
);

os.try_load_icon(IconType::system(format!("{exe}")))
.unwrap();
os.try_load_icon(IconType::system(format!("{exe} a b c")))
.unwrap();

let exe = dir.path().join("test with spaces.py").display().to_string();
std::fs::File::create(&exe).unwrap();

os.try_load_icon(IconType::system(format!("{exe}")))
.unwrap();
os.try_load_icon(IconType::system(format!("{exe} a b c")))
.unwrap_err();
}
}

0 comments on commit 2b94f48

Please sign in to comment.