Trait RuneDisplayWidth

Source
pub trait RuneDisplayWidth {
    // Required methods
    fn rune_width(&self) -> usize;
    fn display_widths(&self) -> Vec<usize>;
    fn display_width(&self) -> 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_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.

Source

fn display_width(&self) -> usize

Returns the total display width in terminal columns.

Equivalent to summing the result of display_widths().

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 28)
22pub fn print_fixed_centered_box(content: &str) {
23    use runefix_core::RuneDisplayWidth;
24
25    let total_width = 120;
26    let content_width = total_width - 2;
27
28    let width = content.width();
29    let left_pad = (content_width - width) / 2;
30    let right_pad = content_width - width - left_pad;
31
32    println!();
33    println!("┏{}┓", "━".repeat(content_width));
34    println!("┃{:width$}┃", "", width = content_width);
35    println!("┃{:left$}{}{:right$}┃", "", content, "", left = left_pad, right = right_pad);
36    println!("┃{:width$}┃", "", width = content_width);
37    println!("┗{}┛", "━".repeat(content_width));
38}

Implementations on Foreign Types§

Source§

impl RuneDisplayWidth for char

Source§

impl RuneDisplayWidth for str

Implementors§