pub struct Width(/* private fields */);
Expand description
A type-safe wrapper for display width values.
This type represents the visual width of text as it would appear on a terminal or console, correctly accounting for:
- ANSI escape sequences (which have zero display width)
- Unicode characters with varying display widths
- Multi-byte characters that occupy single or double terminal columns
The Width
type prevents common bugs by distinguishing between:
- Byte length: The number of bytes in a string (
str.len()
) - Character count: The number of Unicode scalar values (
str.chars().count()
) - Display width: The number of terminal columns occupied when rendered
§Why Display Width Matters
Consider these examples:
"hello"
has 5 bytes, 5 characters, and 5 display width"古古古"
has 9 bytes, 3 characters, and 6 display width (CJK characters are wide)"\x1b[31mred\x1b[0m"
has 11 bytes, 7 characters, and 3 display width (ANSI codes are invisible)
§Examples
§Basic Usage
use ansi_align::Width;
let width = Width::new(42);
assert_eq!(width.get(), 42);
// Convert from usize
let width: Width = 24.into();
assert_eq!(width.get(), 24);
§Comparison and Ordering
use ansi_align::Width;
let small = Width::new(10);
let large = Width::new(20);
assert!(small < large);
assert_eq!(Width::new(15), Width::new(15));
// Can be used in collections
let mut widths = vec![Width::new(30), Width::new(10), Width::new(20)];
widths.sort();
assert_eq!(widths, vec![Width::new(10), Width::new(20), Width::new(30)]);
§Integration with String Width Calculation
use ansi_align::Width;
// Note: This example shows the concept, actual width calculation
// is done internally by the library
let display_width = Width::new(5); // Represents 5 terminal columns
let padding_needed = 10 - display_width.get(); // 5 columns of padding
Implementations§
Source§impl Width
impl Width
Sourcepub const fn get(self) -> usize
pub const fn get(self) -> usize
Returns the underlying usize
value.
§Examples
use ansi_align::Width;
let width = Width::new(42);
assert_eq!(width.get(), 42);
Sourcepub const fn saturating_sub(self, other: Self) -> Self
pub const fn saturating_sub(self, other: Self) -> Self
Performs saturating subtraction, returning 0 if the result would be negative.
§Arguments
other
- TheWidth
value to subtract
§Examples
use ansi_align::Width;
let w1 = Width::new(10);
let w2 = Width::new(15);
assert_eq!(w1.saturating_sub(w2).get(), 0); // 10 - 15 = 0 (saturated)
let w3 = Width::new(20);
assert_eq!(w3.saturating_sub(w1).get(), 10); // 20 - 10 = 10
Trait Implementations§
Source§impl Ord for Width
impl Ord for Width
Source§impl PartialOrd for Width
impl PartialOrd for Width
impl Copy for Width
impl Eq for Width
impl StructuralPartialEq for Width
Auto Trait Implementations§
impl Freeze for Width
impl RefUnwindSafe for Width
impl Send for Width
impl Sync for Width
impl Unpin for Width
impl UnwindSafe for Width
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