pub fn truncate_cell(text: &str, max_width: usize, marker: &str) -> StringExpand description
Truncate text to maximum visual width with ANSI code preservation
Truncates text to fit within max_width visual characters, appending
marker if truncation occurs. Preserves ANSI color codes in the output.
For multiline text (containing \n), each line is truncated independently.
§Arguments
text- Input text (may contain ANSI codes and newlines)max_width- Maximum visual width per line (ANSI codes don’t count)marker- String to append when truncated (default: “…”)
§Returns
Truncated string with preserved ANSI codes and marker appended.
If text fits within max_width, returns original text unchanged.
For multiline text, each line is truncated independently.
§Examples
use tree_fmt::truncate_cell;
// Basic truncation
let result = truncate_cell( "Very long text here", 10, "..." );
assert_eq!( result, "Very lo..." );
// Multiline truncation (per-line)
let result = truncate_cell( "Long line 1\nLong line 2", 8, "..." );
assert!( result.contains( "..." ) ); // Both lines truncated
// ANSI codes preserved
let colored = "\x1b[31mRed text\x1b[0m";
let result = truncate_cell( colored, 5, "..." );
assert!( result.contains( "\x1b[31m" ) ); // Color preserved§Implementation Details
For single-line text: character-by-character iteration tracking visual
position while skipping ANSI escape sequences (\x1b[...m pattern).
For multiline text: splits on \n, truncates each line independently,
then joins back with \n. This ensures proper per-line truncation.