From 02ccf2544365144989efe21c8f01579abc90af20 Mon Sep 17 00:00:00 2001 From: Daniel Rainer Date: Mon, 12 May 2025 21:44:51 +0200 Subject: [PATCH] Prettify profiling for multi-line commands The old version just prints the entire command being profiled as-is. If such a command consists of more than one line, these lines do not have any padding, and thus visually interfere with the timings. This commit adds padding, such that all lines but the first one have padding prepended, such that the original line content starts at the column in which the first line starts. This does not work perfectly for subcommands (in the profiling sense, where the command starts with (regex) '-+>' instead of just '>'). In such cases, even if the command string is indented in the source, the command will not start with whitespace. However, subsequent lines are not trimmed, so the might be indented farther than they should be relative to the first line of the command. --- src/parser.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index f8a71d182..4d8462ff0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -30,6 +30,7 @@ use crate::util::get_time; use crate::wait_handle::WaitHandleStore; use crate::wchar::{wstr, WString, L}; +use crate::wchar_ext::WExt; use crate::wutil::{perror, wgettext, wgettext_fmt}; use crate::{function, FLOG}; use libc::c_int; @@ -1067,7 +1068,7 @@ pub fn job_get_with_index_from_pid(&self, pid: Pid) -> Option<(usize, JobRef)> { /// Returns a new profile item if profiling is active. The caller should fill it in. /// The Parser will deallocate it. - /// If profiling is not active, this returns nullptr. + /// If profiling is not active, this returns None. pub fn create_profile_item(&self) -> Option { if PROFILING_ACTIVE.load() { let mut profile_items = self.profile_items.borrow_mut(); @@ -1271,7 +1272,12 @@ fn print_profile(items: &[ProfileItem], out: &mut File) { ) .as_bytes(), ); - let _ = out.write_all(&wcs2string(&item.cmd)); + let indentation_level = col_width + 1 + col_width + 1 + level + 1; + let indented_cmd = item.cmd.replace( + L!("\n"), + &(WString::from("\n") + &wstr::repeat(L!(" "), indentation_level)[..]), + ); + let _ = out.write_all(&wcs2string(&indented_cmd)); let _ = out.write_all(b"\n"); } }