Width

Struct Width 

Source
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

Source

pub const fn new(value: usize) -> Self

Creates a new Width value from a usize.

§Arguments
  • value - The display width value
§Examples
use ansi_align::Width;

let width = Width::new(10);
assert_eq!(width.get(), 10);
Source

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);
Source

pub const fn saturating_sub(self, other: Self) -> Self

Performs saturating subtraction, returning 0 if the result would be negative.

§Arguments
  • other - The Width 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
Source

pub const fn is_zero(self) -> bool

Returns true if the width is zero.

§Examples
use ansi_align::Width;

assert!(Width::new(0).is_zero());
assert!(!Width::new(5).is_zero());

Trait Implementations§

Source§

impl Add for Width

Source§

fn add(self, rhs: Self) -> Self::Output

Adds two Width values.

§Examples
use ansi_align::Width;

let w1 = Width::new(10);
let w2 = Width::new(5);
assert_eq!((w1 + w2).get(), 15);
Source§

type Output = Width

The resulting type after applying the + operator.
Source§

impl Clone for Width

Source§

fn clone(&self) -> Width

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 Width

Source§

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

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

impl Display for Width

Source§

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

Formats the Width value for display.

§Examples
use ansi_align::Width;

let width = Width::new(42);
assert_eq!(format!("{}", width), "42");
Source§

impl Div for Width

Source§

fn div(self, rhs: Self) -> Self::Output

Divides one Width value by another.

§Examples
use ansi_align::Width;

let w1 = Width::new(10);
let w2 = Width::new(5);
assert_eq!((w1 / w2).get(), 2);
Source§

type Output = Width

The resulting type after applying the / operator.
Source§

impl From<usize> for Width

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl Mul for Width

Source§

fn mul(self, rhs: Self) -> Self::Output

Multiplies two Width values.

§Examples
use ansi_align::Width;

let w1 = Width::new(10);
let w2 = Width::new(5);
assert_eq!((w1 * w2).get(), 50);
Source§

type Output = Width

The resulting type after applying the * operator.
Source§

impl Ord for Width

Source§

fn cmp(&self, other: &Width) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Width

Source§

fn eq(&self, other: &Width) -> 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 PartialOrd for Width

Source§

fn partial_cmp(&self, other: &Width) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for Width

Source§

fn sub(self, rhs: Self) -> Self::Output

Subtracts one Width value from another.

§Examples
use ansi_align::Width;

let w1 = Width::new(10);
let w2 = Width::new(5);
assert_eq!((w1 - w2).get(), 5);
Source§

type Output = Width

The resulting type after applying the - operator.
Source§

impl Copy for Width

Source§

impl Eq for Width

Source§

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.