DisplayWidth

Enum DisplayWidth 

Source
pub enum DisplayWidth {
    Narrow = 1,
    Wide = 2,
}
Expand description

Represents the display width of a character in East Asian contexts

Variants§

§

Narrow = 1

Narrow character (width 1)

§

Wide = 2

Wide character (width 2)

Implementations§

Source§

impl DisplayWidth

Source

pub const fn as_u8(self) -> u8

Returns the numeric width value

Source

pub const fn as_usize(self) -> usize

Returns the numeric width value as usize (common for string operations)

Examples found in repository?
examples/text_alignment.rs (line 19)
17fn display_width(text: &str) -> usize {
18    text.chars()
19        .map(|c| east_asian_width(c as u32).as_usize())
20        .sum()
21}
More examples
Hide additional examples
examples/terminal_width.rs (line 11)
9fn terminal_width(text: &str) -> usize {
10    text.chars()
11        .map(|c| east_asian_width(c as u32).as_usize())
12        .sum()
13}
14
15/// Calculate width with configurable ambiguous character handling
16fn terminal_width_with_config(text: &str, ambiguous_as_wide: bool) -> usize {
17    text.chars()
18        .map(|c| east_asian_width((c as u32, ambiguous_as_wide)).as_usize())
19        .sum()
20}
21
22/// Pad a string to a specific width for terminal alignment
23fn pad_to_width(text: &str, target_width: usize) -> String {
24    let current_width = terminal_width(text);
25    if current_width >= target_width {
26        text.to_string()
27    } else {
28        let padding = " ".repeat(target_width - current_width);
29        format!("{}{}", text, padding)
30    }
31}
32
33/// Truncate a string to fit within a specific width
34fn truncate_to_width(text: &str, max_width: usize) -> String {
35    let mut result = String::new();
36    let mut current_width = 0;
37
38    for ch in text.chars() {
39        let char_width = east_asian_width(ch as u32).as_usize();
40        if current_width + char_width > max_width {
41            break;
42        }
43        result.push(ch);
44        current_width += char_width;
45    }
46
47    result
48}

Trait Implementations§

Source§

impl Clone for DisplayWidth

Source§

fn clone(&self) -> DisplayWidth

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DisplayWidth

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<DisplayWidth> for u8

Source§

fn from(width: DisplayWidth) -> Self

Converts to this type from the input type.
Source§

impl From<DisplayWidth> for usize

Source§

fn from(width: DisplayWidth) -> Self

Converts to this type from the input type.
Source§

impl Hash for DisplayWidth

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for DisplayWidth

Source§

fn eq(&self, other: &DisplayWidth) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for DisplayWidth

Source§

impl Eq for DisplayWidth

Source§

impl StructuralPartialEq for DisplayWidth

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.