Skip to main content

truncate_to_width

Function truncate_to_width 

Source
pub fn truncate_to_width(s: &str, max_width: usize) -> String
Expand description

Truncates a string to fit within a maximum display width, adding ellipsis if needed.

Uses Unicode width calculations for proper handling of CJK and other wide characters. If the string fits within max_width, it is returned unchanged. If truncation is needed, characters are removed from the end and replaced with (ellipsis).

§Arguments

  • s - The string to truncate
  • max_width - Maximum display width (in terminal columns)

§Example

use standout::truncate_to_width;

assert_eq!(truncate_to_width("Hello", 10), "Hello");
assert_eq!(truncate_to_width("Hello World", 6), "Hello…");