| Crates.io | ansi-align |
| lib.rs | ansi-align |
| version | 0.2.2 |
| created_at | 2025-09-08 00:12:46.547417+00 |
| updated_at | 2025-09-08 01:11:56.630606+00 |
| description | Text alignment library with ANSI escape sequence and Unicode support |
| homepage | |
| repository | https://github.com/sabry-awad97/ansi-align |
| max_upload_size | |
| id | 1828640 |
| size | 85,788 |
A Rust library for aligning text with proper support for ANSI escape sequences and Unicode characters.
Width type for display width valuesAdd this to your Cargo.toml:
[dependencies]
ansi-align = "0.2.2"
For CLI tool development with enhanced error handling:
[dev-dependencies]
ansi-align = "0.2.2"
thiserror = "2.0"
clap = { version = "4.5", features = ["derive", "color"] }
colored = "3.0"
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");
use ansi_align::{ansi_align_with_options, Alignment, AlignOptions};
let text = "line1|line2|line3";
let options = AlignOptions::new(Alignment::Right)
.with_split("|") // Custom line separator
.with_pad('.'); // Custom padding character
let result = ansi_align_with_options(text, &options);
// Result: "..line1|..line2|..line3"
The library correctly handles ANSI escape sequences by ignoring them during width calculation:
use ansi_align::center;
let colored_text = "\x1b[31mred\x1b[0m\n\x1b[32mgreen text\x1b[0m";
let aligned = center(colored_text);
// ANSI codes are preserved but don't affect alignment
Wide Unicode characters (like CJK) are handled correctly:
use ansi_align::center;
let unicode_text = "古\n古古古";
let aligned = center(unicode_text);
// Properly accounts for double-width characters
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 textAlignmentpub enum Alignment {
Left,
Center,
Right,
}
WidthA type-safe wrapper for display width values:
let width = Width::new(42);
let value = width.get(); // Returns usize
AlignOptionsConfiguration for alignment behavior:
pub struct AlignOptions {
pub align: Alignment, // Alignment type
pub split: String, // Line separator (default: "\n")
pub pad: char, // Padding character (default: ' ')
}
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 characterWidth prevent common errorsThe example CLI tool showcases production-ready Rust patterns:
// Custom error types with context
#[derive(Error, Debug)]
enum CliError {
#[error("Failed to read file '{path}': {source}")]
FileRead { path: String, source: std::io::Error },
#[error("Failed to read from stdin: {0}")]
StdinRead(std::io::Error),
#[error("File not found: {0}")]
FileNotFound(String),
}
// Configurable border rendering
struct BorderConfig {
top_left: char,
top_right: char,
// ... other border characters
padding: usize,
}
// Modular demo system
struct DemoSection {
title: String,
content: Box<dyn Fn()>,
}
The CLI tool features a configurable border system:
// Default Unicode box drawing characters
struct BorderConfig {
top_left: '┌', // ┌
top_right: '┐', // ┐
bottom_left: '└', // └
bottom_right: '┘', // ┘
horizontal: '─', // ─
vertical: '│', // │
padding: 1, // Space padding
}
Example output with borders:
┌───────┐
│ Hello │
│ World │
│ Rust │
└───────┘
The crate includes a powerful, production-ready CLI tool with advanced features and robust error handling:
# Run the interactive demo to see all features
cargo run --example cli_tool -- --demo
# Align text from command line
cargo run --example cli_tool -- "Hello\nWorld\nRust" --align center --border
# Read from stdin with escape sequence processing
echo "Line 1\nLonger Line 2\nShort" | cargo run --example cli_tool -- - --align right --pad '.'
# Read from file with validation
cargo run --example cli_tool -- --file examples/sample.txt --align center --border
# Custom separator and padding
cargo run --example cli_tool -- "Name|Age|City" --split "|" --align center --pad '_'
# Quiet mode for scripting
cargo run --example cli_tool -- "data" --border --quiet
The CLI tool demonstrates modern Rust best practices:
thiserror for type-safe, descriptive error handlingThe CLI includes an interactive demo showcasing all features:
$ cargo run --example cli_tool -- --demo
🎨 ansi-align Demo - Beautiful Text Alignment
📝 Basic Alignment:
Left:
Hello
World
Rust
Center:
Hello
World
Rust
Right:
Hello
World
Rust
🌈 ANSI Color Support:
Center aligned with colors:
Red Text
Green Text
Blue Text
🌏 Unicode Support:
Right aligned Unicode:
古
古古古
Hello 世界
⚙️ Custom Options:
Custom separator '|' and padding '.':
..Name|..Age|Location
📋 Menu Example:
Center aligned menu:
🏠 Home
📋 About Us
📞 Contact
⚙️ Settings
The CLI provides clear, actionable error messages:
# File not found
$ cargo run --example cli_tool -- --file nonexistent.txt
Error: File not found: nonexistent.txt
# Invalid file permissions
$ cargo run --example cli_tool -- --file /protected/file.txt
Error: Failed to read file '/protected/file.txt': Permission denied
use ansi_align::center;
let menu = "Home\nAbout Us\nContact\nServices";
let aligned_menu = center(menu);
println!("{}", aligned_menu);
use ansi_align::right;
let code = "if x:\n return y\nelse:\n return z";
let aligned_code = right(code);
println!("{}", aligned_code);
use ansi_align::{ansi_align_with_options, Alignment, AlignOptions};
let data = "Name|Age|City";
let options = AlignOptions::new(Alignment::Center)
.with_split("|")
.with_pad('_');
let result = ansi_align_with_options(data, options);
cargo test
# Interactive demo
cargo run --example cli_tool -- --demo
# Test with sample data
cargo run --example cli_tool -- "Hello\nWorld" --border --align center
# Test error handling
cargo run --example cli_tool -- --file nonexistent.txt
The project follows Rust best practices:
thiserror for descriptive error typesContributions 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.
Licensed under either of
at your option.