From bb047250ec5cd70a19734ff431e52aeba1109841 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 5 Mar 2026 10:28:44 -0800 Subject: [PATCH] ptree: Align PID column width based on kernel pid_max Read /proc/sys/kernel/pid_max to determine the maximum PID digit count and left-align PIDs to that width, so the tree output stays aligned regardless of PID length. Co-Authored-By: Claude Opus 4.6 --- src/bin/ptree.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/bin/ptree.rs b/src/bin/ptree.rs index 1b424e5..45b188e 100644 --- a/src/bin/ptree.rs +++ b/src/bin/ptree.rs @@ -24,6 +24,14 @@ use nix::unistd::User; nix::ioctl_read_bad!(tiocgwinsz, libc::TIOCGWINSZ, libc::winsize); +fn pid_width() -> usize { + std::fs::read_to_string("/proc/sys/kernel/pid_max") + .ok() + .and_then(|s| s.trim().parse::().ok()) + .map(|max| max.to_string().len()) + .unwrap_or(7) +} + fn terminal_width() -> Option { if let Ok(cols) = std::env::var("COLUMNS") { if let Ok(w) = cols.parse::() { @@ -135,6 +143,7 @@ fn build_proc_maps() -> Result> { struct PrintOpts<'a> { graph: Option<&'a GraphChars>, max_width: Option, + pid_width: usize, } fn print_tree( @@ -247,7 +256,7 @@ fn print_ptree_line(pid: u64, indent_level: u64, opts: &PrintOpts, cont: &[bool] } } } - let _ = write!(line, "{pid} "); + let _ = write!(line, "{pid: