Skip to content

Commit 8c922b7

Browse files
committed
Unwrapping for typical nix executable names
1 parent 4aa39d9 commit 8c922b7

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/procs.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,18 @@ impl<'a> ProcDesc<'a> {
6464
fn ps_name(p: &Process) -> (Option<String>, ProcNamePre) {
6565
let exe = p.exe().ok();
6666
let cmdline = p.cmdline().ok();
67+
let argv0 = cmdline.as_ref().and_then(|v| v.get(0));
6768
// I considered checking whether to check if the exe file_name is on $PATH
6869
// and print the whole path if not. Nah.
6970
let name = exe
7071
.as_ref()
71-
.and_then(|p| p.file_name().map(|p| p.to_string_lossy().into_owned()))
72-
.or_else(|| cmdline.as_ref().and_then(|v| v.get(0).cloned()));
72+
.and_then(|p| {
73+
p.file_name().map(|p| {
74+
let name = p.to_string_lossy();
75+
un_nix_wrap(name.as_ref(), argv0).unwrap_or_else(|| name.into_owned())
76+
})
77+
})
78+
.or_else(|| argv0.cloned());
7379
let proc_name_pre = ProcNamePre { exe, cmdline, name };
7480
let name = if let py @ Some(_) = py_ps_name(&proc_name_pre) {
7581
py
@@ -85,6 +91,15 @@ fn ps_name(p: &Process) -> (Option<String>, ProcNamePre) {
8591
(name, proc_name_pre)
8692
}
8793

94+
// I generally prefer the exe file name,
95+
// but nixpkgs/nixos has those named .foo-wrapped in many cases.
96+
// I want foo then.
97+
fn un_nix_wrap(name: &str, argv0: Option<&String>) -> Option<String> {
98+
let argv0 = argv0?;
99+
let argv0 = &argv0[argv0.rfind('/').map_or(0, |p| p + 1)..];
100+
(argv0 == name.strip_prefix(".")?.strip_suffix("-wrapped")?).then(|| argv0.into())
101+
}
102+
88103
fn java_ps_name(proc_name_pre: &ProcNamePre) -> Option<String> {
89104
let special = &|arg: &str, next: Option<&str>| {
90105
if arg == "-jar" {

0 commit comments

Comments
 (0)