pub enum DisplayWidth {
Narrow = 1,
Wide = 2,
}Expand description
Represents the display width of a character in East Asian contexts
Variants§
Implementations§
Source§impl DisplayWidth
impl DisplayWidth
Sourcepub const fn as_usize(self) -> usize
pub const fn as_usize(self) -> usize
Returns the numeric width value as usize (common for string operations)
Examples found in repository?
More 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
impl Clone for DisplayWidth
Source§fn clone(&self) -> DisplayWidth
fn clone(&self) -> DisplayWidth
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for DisplayWidth
impl Debug for DisplayWidth
Source§impl From<DisplayWidth> for u8
impl From<DisplayWidth> for u8
Source§fn from(width: DisplayWidth) -> Self
fn from(width: DisplayWidth) -> Self
Converts to this type from the input type.
Source§impl From<DisplayWidth> for usize
impl From<DisplayWidth> for usize
Source§fn from(width: DisplayWidth) -> Self
fn from(width: DisplayWidth) -> Self
Converts to this type from the input type.
Source§impl Hash for DisplayWidth
impl Hash for DisplayWidth
Source§impl PartialEq for DisplayWidth
impl PartialEq for DisplayWidth
impl Copy for DisplayWidth
impl Eq for DisplayWidth
impl StructuralPartialEq for DisplayWidth
Auto Trait Implementations§
impl Freeze for DisplayWidth
impl RefUnwindSafe for DisplayWidth
impl Send for DisplayWidth
impl Sync for DisplayWidth
impl Unpin for DisplayWidth
impl UnwindSafe for DisplayWidth
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more