ansi-align
A Rust library for aligning text with proper support for ANSI escape sequences and Unicode characters.
Features
- ANSI-aware alignment: Correctly handles text containing ANSI escape sequences (colors, formatting)
- Unicode support: Properly calculates display width for Unicode characters including CJK characters
- 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
Widthtype for display width values
Installation
Add this to your Cargo.toml:
[]
= "0.2.1"
For CLI tool development with enhanced error handling:
[]
= "0.2.1"
= "2.0"
= { = "4.5", = ["derive", "color"] }
= "3.0"
Quick Start
use ;
// Basic alignment (defaults to center)
let text = "hello\nworld";
let centered = ansi_align;
// Specific alignment functions
let left_aligned = left;
let centered = center;
let right_aligned = right;
Advanced Usage
Custom Options
use ;
let text = "line1|line2|line3";
let options = new
.with_split // Custom line separator
.with_pad; // Custom padding character
let result = ansi_align_with_options;
// Result: "..line1|..line2|..line3"
ANSI Escape Sequences
The library correctly handles ANSI escape sequences by ignoring them during width calculation:
use center;
let colored_text = "\x1b[31mred\x1b[0m\n\x1b[32mgreen text\x1b[0m";
let aligned = center;
// ANSI codes are preserved but don't affect alignment
Unicode Characters
Wide Unicode characters (like CJK) are handled correctly:
use center;
let unicode_text = "古\n古古古";
let aligned = center;
// Properly accounts for double-width characters
API Reference
Core Functions
ansi_align(text: &str) -> String- Center align text (default)ansi_align_with_options(text: &str, opts: AlignOptions) -> String- Align with custom optionsleft(text: &str) -> String- Left align (no-op, returns original)center(text: &str) -> String- Center align textright(text: &str) -> String- Right align text
Types
Alignment
Width
A type-safe wrapper for display width values:
let width = new;
let value = width.get; // Returns usize
AlignOptions
Configuration for alignment behavior:
Builder methods:
AlignOptions::new(align: Alignment)- Create with alignment.with_split(split: impl Into<String>)- Set custom line separator.with_pad(pad: char)- Set custom padding character
Performance
- Left alignment: Optimized as a no-op, returns input unchanged
- Single pass: Text is processed once for optimal performance
- Efficient padding: Uses optimized string creation for different padding sizes
- Memory conscious: Minimal allocations with capacity pre-calculation
- String processing: Optimized escape sequence handling reduces intermediate allocations
- Width calculation: Efficient iterator-based approach without unnecessary collections
Architecture & Design
Library Design Principles
- Type Safety: Custom types like
Widthprevent common errors - Zero-cost Abstractions: Performance-critical paths have minimal overhead
- Composability: Functions can be easily combined and extended
- Unicode First: Proper handling of complex text from the ground up
CLI Tool Architecture
The example CLI tool showcases production-ready Rust patterns:
// Custom error types with context
// Configurable border rendering
// Modular demo system
Border Customization
The CLI tool features a configurable border system:
// Default Unicode box drawing characters
Example output with borders:
┌───────┐
│ Hello │
│ World │
│ Rust │
└───────┘
Key Improvements in v0.2.1
- Enhanced Error Handling: Replaced generic errors with specific, actionable error types
- Performance Optimizations: Reduced string allocations and improved processing efficiency
- Better Separation of Concerns: Modular design with dedicated structs for different responsibilities
- Improved Testability: Broke down large functions into focused, testable units
- Configuration System: Extensible border styles and formatting options
- Input Validation: Proper file existence checking and argument validation
- Modular Demo System: Organized demo sections for better maintainability
- Production Ready: Robust error handling and edge case management
CLI Tool
The crate includes a powerful, production-ready CLI tool with advanced features and robust error handling:
# Run the interactive demo to see all features
# Align text from command line
# Read from stdin with escape sequence processing
|
# Read from file with validation
# Custom separator and padding
# Quiet mode for scripting
CLI Features
- 🎨 Beautiful output with customizable colorful borders
- 📁 Multiple input sources: command line arguments, stdin, or files
- 🌈 ANSI color preservation maintains formatting in aligned output
- 🌏 Full Unicode support including CJK and emoji characters
- ⚙️ Highly customizable padding characters and line separators
- 📋 Interactive demo showcasing all alignment capabilities
- 🛡️ Robust error handling with descriptive error messages
- ⚡ Performance optimized with minimal memory allocations
- 🔧 Production ready with proper validation and edge case handling
- 🤫 Quiet mode for integration with scripts and pipelines
CLI Architecture
The CLI tool demonstrates modern Rust best practices:
- Custom Error Types: Uses
thiserrorfor type-safe, descriptive error handling - Modular Design: Separated concerns with dedicated structs for border rendering and demo sections
- Performance Optimized: Efficient string processing and memory usage
- Configurable: Extensible border styles and formatting options
- Testable: Small, focused functions with single responsibilities
Enhanced Demo System
The CLI includes an interactive demo showcasing all features:
||
Error Handling
The CLI provides clear, actionable error messages:
# File not found
# Invalid file permissions
Examples
Simple Menu Alignment
use center;
let menu = "Home\nAbout Us\nContact\nServices";
let aligned_menu = center;
println!;
Code Block Alignment
use right;
let code = "if x:\n return y\nelse:\n return z";
let aligned_code = right;
println!;
Custom Separator and Padding
use ;
let data = "Name|Age|City";
let options = new
.with_split
.with_pad;
let result = ansi_align_with_options;
Development
Running Tests
Running the CLI Tool
# Interactive demo
# Test with sample data
# Test error handling
Code Quality
The project follows Rust best practices:
- Error Handling: Uses
thiserrorfor descriptive error types - Performance: Optimized string processing and memory usage
- Testing: Comprehensive test coverage for edge cases
- Documentation: Extensive inline documentation and examples
- Modularity: Clean separation of concerns and reusable components
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.