diff --git a/src/columns/command.rs b/src/columns/command.rs index 1add31432..895cacabc 100644 --- a/src/columns/command.rs +++ b/src/columns/command.rs @@ -9,10 +9,11 @@ pub struct Command { fmt_contents: HashMap, raw_contents: HashMap, width: usize, + abbr_nix: bool, } impl Command { - pub fn new(header: Option) -> Self { + pub fn new(header: Option, abbr_nix: bool) -> Self { let header = header.unwrap_or_else(|| String::from("Command")); let unit = String::new(); Self { @@ -21,10 +22,38 @@ impl Command { width: 0, header, unit, + abbr_nix, } } } +// Abbreviate a Nix store path so only the first 12 chars of the hash are kept, +// followed by an ellipsis. Example: /nix/store/abc12345…-hello-2.12.1/bin/hello +fn abbr_nix_store_path(s: &str) -> String { + let Some(rest) = s.strip_prefix("/nix/store/") else { + return s.to_string(); + }; + let Some(hash_end) = rest.find('-') else { + return s.to_string(); + }; + if hash_end >= 32 && rest.as_bytes()[..hash_end].iter().all(|b| b.is_ascii_hexdigit()) { + let (hash, name_and_more) = rest.split_at(hash_end); + // safe truncation: 12 chars of hash + ellipsis + let head = &hash[..12]; + format!("/nix/store/{head}…{name_and_more}") + } else { + s.to_string() + } +} + +fn abbr_nix_store(s: &str, abbr: bool) -> String { + if abbr { + abbr_nix_store_path(s) + } else { + s.to_string() + } +} + #[cfg(any(target_os = "linux", target_os = "android"))] impl Column for Command { fn add(&mut self, proc: &ProcessInfo) { @@ -47,7 +76,7 @@ impl Column for Command { } else { proc.curr_proc.stat().comm.clone() }; - let raw_content = fmt_content.clone(); + let raw_content = abbr_nix_store(&fmt_content, self.abbr_nix); self.fmt_contents.insert(proc.pid, fmt_content); self.raw_contents.insert(proc.pid, raw_content); @@ -79,7 +108,7 @@ impl Column for Command { } else { String::from("") }; - let raw_content = fmt_content.clone(); + let raw_content = abbr_nix_store(&fmt_content, self.abbr_nix); self.fmt_contents.insert(proc.pid, fmt_content); self.raw_contents.insert(proc.pid, raw_content); @@ -92,7 +121,7 @@ impl Column for Command { impl Column for Command { fn add(&mut self, proc: &ProcessInfo) { let fmt_content = proc.command.clone(); - let raw_content = fmt_content.clone(); + let raw_content = abbr_nix_store(&fmt_content, self.abbr_nix); self.fmt_contents.insert(proc.pid, fmt_content); self.raw_contents.insert(proc.pid, raw_content); @@ -120,7 +149,7 @@ impl Column for Command { x }; let fmt_content = command; - let raw_content = fmt_content.clone(); + let raw_content = abbr_nix_store(&fmt_content, self.abbr_nix); self.fmt_contents.insert(proc.pid, fmt_content); self.raw_contents.insert(proc.pid, raw_content); diff --git a/src/columns/os_freebsd.rs b/src/columns/os_freebsd.rs index 56e03723a..cf879fdca 100644 --- a/src/columns/os_freebsd.rs +++ b/src/columns/os_freebsd.rs @@ -180,11 +180,12 @@ pub fn gen_column( _docker_path: &str, separator: &str, abbr_sid: bool, + abbr_nix: bool, tree_symbols: &[String; 5], procfs: Option, ) -> Box { match kind { - ConfigColumnKind::Command => Box::new(Command::new(header)), + ConfigColumnKind::Command => Box::new(Command::new(header, abbr_nix)), ConfigColumnKind::ContextSw => Box::new(ContextSw::new(header)), ConfigColumnKind::CpuTime => Box::new(CpuTime::new(header)), ConfigColumnKind::ElapsedTime => Box::new(ElapsedTime::new(header)), diff --git a/src/columns/os_linux.rs b/src/columns/os_linux.rs index f15c2f58f..209b6665d 100644 --- a/src/columns/os_linux.rs +++ b/src/columns/os_linux.rs @@ -264,13 +264,14 @@ pub fn gen_column( _docker_path: &str, separator: &str, abbr_sid: bool, + abbr_nix: bool, tree_symbols: &[String; 5], procfs: Option, ) -> Box { match kind { ConfigColumnKind::Ccgroup => Box::new(Ccgroup::new(header)), ConfigColumnKind::Cgroup => Box::new(Cgroup::new(header)), - ConfigColumnKind::Command => Box::new(Command::new(header)), + ConfigColumnKind::Command => Box::new(Command::new(header, abbr_nix)), ConfigColumnKind::ContextSw => Box::new(ContextSw::new(header)), ConfigColumnKind::VoluntaryContextSw => Box::new(VoluntaryContextSw::new(header)), ConfigColumnKind::InvoluntaryContextSw => Box::new(InvoluntaryContextSw::new(header)), diff --git a/src/columns/os_macos.rs b/src/columns/os_macos.rs index af45435f1..e4231ad6b 100644 --- a/src/columns/os_macos.rs +++ b/src/columns/os_macos.rs @@ -165,12 +165,13 @@ pub fn gen_column( _docker_path: &str, separator: &str, abbr_sid: bool, + abbr_nix: bool, tree_symbols: &[String; 5], _procfs: Option, ) -> Box { match kind { ConfigColumnKind::Arch => Box::new(Arch::new(header)), - ConfigColumnKind::Command => Box::new(Command::new(header)), + ConfigColumnKind::Command => Box::new(Command::new(header, abbr_nix)), ConfigColumnKind::ContextSw => Box::new(ContextSw::new(header)), ConfigColumnKind::CpuTime => Box::new(CpuTime::new(header)), #[cfg(feature = "docker")] diff --git a/src/columns/os_windows.rs b/src/columns/os_windows.rs index 6a5e1fa3f..4f6191314 100644 --- a/src/columns/os_windows.rs +++ b/src/columns/os_windows.rs @@ -114,11 +114,12 @@ pub fn gen_column( _docker_path: &str, separator: &str, abbr_sid: bool, + abbr_nix: bool, tree_symbols: &[String; 5], _procfs: Option, ) -> Box { match kind { - ConfigColumnKind::Command => Box::new(Command::new(header)), + ConfigColumnKind::Command => Box::new(Command::new(header, abbr_nix)), ConfigColumnKind::CpuTime => Box::new(CpuTime::new(header)), ConfigColumnKind::ElapsedTime => Box::new(ElapsedTime::new(header)), ConfigColumnKind::Empty => Box::new(Empty::new()), diff --git a/src/config.rs b/src/config.rs index 9a99e7c3b..9f8c82e56 100644 --- a/src/config.rs +++ b/src/config.rs @@ -555,6 +555,8 @@ pub struct ConfigDisplay { pub tree_symbols: [String; 5], #[serde(default = "default_true")] pub abbr_sid: bool, + #[serde(default = "default_false")] + pub abbr_nix: bool, #[serde(default = "default_theme_auto")] pub theme: ConfigTheme, #[serde(default = "default_true")] @@ -587,6 +589,7 @@ impl Default for ConfigDisplay { String::from("└"), ], abbr_sid: true, + abbr_nix: false, theme: ConfigTheme::Auto, show_kthreads: true, } diff --git a/src/view.rs b/src/view.rs index a8ed8b3a9..fd617943c 100644 --- a/src/view.rs +++ b/src/view.rs @@ -136,6 +136,7 @@ impl View { &config.docker.path, &config.display.separator, config.display.abbr_sid, + config.display.abbr_nix, &config.display.tree_symbols, opt.procfs.clone(), );