Expand description
Β§π¨ wrap-ansi
A high-performance, Unicode-aware Rust library for intelligently wrapping text while preserving ANSI escape sequences, colors, styles, and hyperlinks.
This library is a faithful Rust port of the popular JavaScript wrap-ansi library, designed for terminal applications, CLI tools, and any software that needs to display formatted text in constrained widths.
Β§β¨ Key Features
| Feature | Description |
|---|---|
| π¨ ANSI-Aware Wrapping | Preserves colors, styles, and formatting across line breaks |
| π Hyperlink Support | Maintains clickable OSC 8 hyperlinks when wrapping |
| π Unicode Ready | Correctly handles CJK characters, emojis, and combining marks |
| β‘ High Performance | Optimized algorithms with pre-compiled regex patterns |
| π οΈ Flexible Options | Hard/soft wrapping, trimming, word boundaries control |
| π Memory Safe | Built-in protection against DoS attacks with input size limits |
| π Rich API | Fluent builder pattern and comprehensive error handling |
Β§π Quick Start
Add this to your Cargo.toml:
[dependencies]
wrap-ansi = "0.1.0"Β§Basic Text Wrapping
use wrap_ansi::wrap_ansi;
let text = "The quick brown fox jumps over the lazy dog";
let wrapped = wrap_ansi(text, 20, None);
println!("{}", wrapped);
// Output:
// The quick brown fox
// jumps over the lazy
// dogΒ§ANSI Colors & Styles
use wrap_ansi::wrap_ansi;
// Colors are preserved across line breaks!
let colored = "\u{001B}[31mThis is red text that will be wrapped properly\u{001B}[39m";
let wrapped = wrap_ansi(colored, 15, None);
println!("{}", wrapped);
// Each line will have proper color codes: \u{001B}[31m...\u{001B}[39mΒ§Advanced Configuration
use wrap_ansi::{wrap_ansi, WrapOptions};
// Using the builder pattern for clean configuration
let options = WrapOptions::builder()
.hard_wrap(true) // Break long words
.trim_whitespace(false) // Preserve whitespace
.word_wrap(true) // Respect word boundaries
.build();
let text = "supercalifragilisticexpialidocious word";
let wrapped = wrap_ansi(text, 10, Some(options));
println!("{}", wrapped);
// Output:
// supercalif
// ragilistic
// expialidoc
// ious wordΒ§π― Use Cases
Perfect for:
- CLI Applications: Format help text, error messages, and output
- Terminal UIs: Create responsive layouts that adapt to terminal width
- Documentation Tools: Generate formatted text with preserved styling
- Log Processing: Wrap log entries while maintaining color coding
- Code Formatters: Handle syntax-highlighted code with proper wrapping
Β§π Advanced Features
Β§Wrapping Modes
| Mode | Behavior | Use Case |
|---|---|---|
| Soft Wrap (default) | Long words move to next line intact | Natural text flow |
| Hard Wrap | Long words break at column boundary | Strict width constraints |
| Character Wrap | Break anywhere, ignore word boundaries | Monospace layouts |
Β§ANSI Sequence Support
β Foreground Colors: Standard (30-37) and bright (90-97) β Background Colors: Standard (40-47) and bright (100-107) β Text Styles: Bold, italic, underline, strikethrough β Color Resets: Proper handling of reset sequences (39, 49) β Hyperlinks: OSC 8 sequences for clickable terminal links β Custom SGR: Support for any Select Graphic Rendition codes
Β§Unicode & Internationalization
use wrap_ansi::{wrap_ansi, WrapOptions};
// CJK characters are properly counted as 2 columns
let chinese = "δ½ ε₯½δΈηοΌθΏζ―δΈδΈͺζ΅θ―";
let wrapped = wrap_ansi(chinese, 8, None);
// Emojis and combining characters work correctly
let emoji = "Hello π World π with emojis";
let options = WrapOptions::builder().hard_wrap(true).build();
let wrapped = wrap_ansi(emoji, 10, Some(options));Β§π§ Error Handling
For applications requiring robust error handling:
use wrap_ansi::{wrap_ansi_checked, WrapError};
let text = "Hello, world!";
match wrap_ansi_checked(text, 0, None) {
Ok(wrapped) => println!("Wrapped: {}", wrapped),
Err(WrapError::InvalidColumnWidth(width)) => {
eprintln!("Invalid width: {}", width);
}
Err(WrapError::InputTooLarge(size, max)) => {
eprintln!("Input too large: {} bytes (max: {})", size, max);
}
_ => {}
}Β§π Performance
- Zero-copy operations where possible
- Pre-compiled regex patterns for ANSI sequence parsing
- Efficient string operations with capacity pre-allocation
DoSprotection with configurable input size limits- Minimal allocations through careful memory management
Β§π€ Compatibility
- Rust Edition: 2021+
- MSRV: 1.70.0
- Platforms: All platforms supported by Rust
- Terminal Compatibility: Works with all ANSI-compatible terminals
Β§π Examples
Check out the examples directory for more comprehensive usage examples, including:
- Terminal UI layouts
- Progress bars with colors
- Syntax highlighting preservation
- Interactive CLI applications
StructsΒ§
- Wrap
Options - ποΈ Configuration options for advanced text wrapping behavior.
- Wrap
Options Builder - ποΈ Fluent builder for creating
WrapOptionswith a clean, readable interface.
EnumsΒ§
- Wrap
Error - Comprehensive error types for wrap-ansi operations.
FunctionsΒ§
- wrap_
ansi - π¨ The main text wrapping function with full ANSI escape sequence support.
- wrap_
ansi_ checked - π‘οΈ Wrap text with comprehensive error handling and input validation.