Trait RuneDisplayWidth

Source
pub trait RuneDisplayWidth {
    // Required methods
    fn rune_width(&self) -> usize;
    fn display_width(&self) -> usize;
    fn display_widths(&self) -> Vec<usize>;

    // Provided method
    fn width(&self) -> usize { ... }
}
Expand description

Extension trait for measuring the display width of runes, graphemes, and strings.

This trait provides unified access to terminal display width calculations for both char and &str. It supports:

  • Single runes (char)
  • Full Unicode strings with grapheme segmentation (&str)

All widths are measured in terminal columns, respecting East Asian widths, emoji sequences, and control characters.

§Examples

use runefix_core::RuneDisplayWidth;

assert_eq!('語'.rune_width(), 2);
assert_eq!("你👋a".display_widths(), vec![2, 2, 1]);
assert_eq!("Hi👋".display_width(), 4);
assert_eq!("👋".width(), 2);

Required Methods§

Source

fn rune_width(&self) -> usize

Returns the display width of a single rune or grapheme.

For char, this is the width of the character. For &str, this assumes the string is a single grapheme cluster.

Source

fn display_width(&self) -> usize

Returns the total display width in terminal columns.

Equivalent to summing the result of display_widths().

Source

fn display_widths(&self) -> Vec<usize>

Returns the display width of each grapheme cluster in the value.

For &str, this segments the string using Unicode grapheme rules. For char, returns a single-item vector.

Provided Methods§

Source

fn width(&self) -> usize

Returns the total display width in terminal columns (alias of display_width()).

Examples found in repository?
examples/text_align.rs (line 29)
23pub fn print_fixed_centered_box(content: &str) {
24    use runefix_core::RuneDisplayWidth;
25
26    let total_width = 120;
27    let content_width = total_width - 2;
28
29    let width = content.width();
30    let left_pad = (content_width - width) / 2;
31    let right_pad = content_width - width - left_pad;
32
33    println!();
34    println!("┏{}┓", "━".repeat(content_width));
35    println!("┃{:width$}┃", "", width = content_width);
36    println!("┃{:left$}{}{:right$}┃", "", content, "", left = left_pad, right = right_pad);
37    println!("┃{:width$}┃", "", width = content_width);
38    println!("┗{}┛", "━".repeat(content_width));
39}

Implementations on Foreign Types§

Source§

impl RuneDisplayWidth for char

Source§

impl RuneDisplayWidth for str

Implementors§