Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ pub fn truncate(s: &'_ str, width: usize) -> Cow<'_, str> {
}
}

/// Trim trailing whitespace from a string that may contain ANSI escape sequences.
/// Unlike str::trim_end(), this correctly handles ANSI codes at the end of the string
/// that would otherwise prevent trimming of trailing whitespace.
pub fn ansi_trim_end(s: &str) -> String {
let stripped = console::strip_ansi_codes(s);
let trimmed_width = UnicodeWidthStr::width(stripped.trim_end());
if trimmed_width == UnicodeWidthStr::width(stripped.as_ref()) {
return s.to_string();
}
truncate(s, trimmed_width).into_owned()
}

pub fn find_column_kind(pat: &str) -> Option<ConfigColumnKind> {
// strict search at first
for (k, (v, _)) in KIND_LIST.iter() {
Expand Down
10 changes: 6 additions & 4 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::opt::{ArgColorMode, ArgPagerMode};
use crate::process::collect_proc;
use crate::style::{apply_color, apply_style, color_to_column_style};
use crate::term_info::TermInfo;
use crate::util::{KeywordClass, classify, find_column_kind, find_exact, find_partial, truncate};
use crate::util::{
KeywordClass, ansi_trim_end, classify, find_column_kind, find_exact, find_partial, truncate,
};
use anyhow::{Error, bail};
#[cfg(not(target_os = "windows"))]
use pager::Pager;
Expand Down Expand Up @@ -511,7 +513,7 @@ impl View {
);
}
}
row = row.trim_end().to_string();
row = ansi_trim_end(&row);
row = truncate(&row, self.term_info.width).to_string();
self.term_info.write_line(&row)?;
Ok(())
Expand All @@ -533,7 +535,7 @@ impl View {
);
}
}
row = row.trim_end().to_string();
row = ansi_trim_end(&row);
row = truncate(&row, self.term_info.width).to_string();
self.term_info.write_line(&row)?;
Ok(())
Expand Down Expand Up @@ -562,7 +564,7 @@ impl View {
);
}
}
row = row.trim_end().to_string();
row = ansi_trim_end(&row);
row = truncate(&row, self.term_info.width).to_string();
self.term_info.write_line(&row)?;
Ok(())
Expand Down