Skip to main content

width

Function width 

Source
pub fn width(src: &str) -> usize
Available on crate feature fitted only.
Expand description

§Width.

Find the (total) “display width” of a string slice.

Like anything having to do with width vs length, this should be considered at best an approximation. The unicode_width crate is used under-the-hood; refer to their documentation for more details on how terrible Unicode is. Haha.

This method is ANSI-aware and will automatically skip past any CSI or OSC sequences it encounters.

This requires the fitted crate feature.

§Examples

use fyi_msg::width;

// The problem with length is inclusivity:
assert_eq!("\x1b[1mBjörk\x1b[0m".len(), 14);

// Even without the ANSI, the length is still too high:
assert_eq!("Björk".len(), 6);

// Width is just what it looks like:
assert_eq!(width("\x1b[1mBjörk\x1b[0m"), 5);
assert_eq!(
    width("Björk"), // Unicode.
    width("Bjork"), // ASCII.
);

// Be careful with multi-line content; widths are _total_.
assert_eq!(width("Hello World"),  11);
assert_eq!(width("Hello\nWorld"), 10); // Line breaks have no width.