Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/columns/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ pub struct Command {
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, String>,
width: usize,
abbr_nix: bool,
}

impl Command {
pub fn new(header: Option<String>) -> Self {
pub fn new(header: Option<String>, abbr_nix: bool) -> Self {
let header = header.unwrap_or_else(|| String::from("Command"));
let unit = String::new();
Self {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/columns/os_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
) -> Box<dyn Column> {
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)),
Expand Down
3 changes: 2 additions & 1 deletion src/columns/os_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
) -> Box<dyn Column> {
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)),
Expand Down
3 changes: 2 additions & 1 deletion src/columns/os_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
) -> Box<dyn Column> {
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")]
Expand Down
3 changes: 2 additions & 1 deletion src/columns/os_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
) -> Box<dyn Column> {
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()),
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -587,6 +589,7 @@ impl Default for ConfigDisplay {
String::from("└"),
],
abbr_sid: true,
abbr_nix: false,
theme: ConfigTheme::Auto,
show_kthreads: true,
}
Expand Down
1 change: 1 addition & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down