Crate ansi_align

Crate ansi_align 

Source
Expand description

§ansi-align

A Rust library for aligning text with proper support for ANSI escape sequences and Unicode characters.

This crate provides functions to align text in various ways (left, center, right) while correctly handling ANSI escape sequences (like color codes) and Unicode characters with varying display widths.

§Features

  • ANSI-aware alignment: Correctly handles text containing ANSI escape sequences
  • Unicode support: Properly calculates display width for Unicode characters including CJK
  • Multiple alignment options: Left, center, and right alignment
  • Customizable: Configure split strings and padding characters
  • Performance optimized: Single-pass processing with efficient memory usage
  • Type-safe: Uses a Width type for display width values

§Quick Start

use ansi_align::{ansi_align, center, left, right};

// Basic alignment (defaults to center)
let text = "hello\nworld";
let centered = ansi_align(text);

// Specific alignment functions
let left_aligned = left("short\nlonger line");
let centered = center("short\nlonger line");
let right_aligned = right("short\nlonger line");

§Advanced Usage

use ansi_align::{ansi_align_with_options, Alignment, AlignOptions};

let text = "line1|line2|line3";
let options = AlignOptions::new(Alignment::Right)
    .with_split("|")
    .with_pad('.');

let result = ansi_align_with_options(text, &options);

Structs§

AlignOptions
Configuration options for text alignment operations.
Width
A type-safe wrapper for display width values.

Enums§

Alignment
Specifies the alignment direction for text.

Functions§

ansi_align
Align text with center alignment (default behavior)
ansi_align_with_options
Align text with support for ANSI escape sequences
center
Align text to the center
left
Align text to the left (no-op, returns original text)
right
Align text to the right