widest-line
A high-performance Rust library for determining the display width of the widest line in multi-line strings.
Overview
widest-line provides precise measurement of string display widths with comprehensive support for modern text rendering scenarios. The library correctly handles complex Unicode characters, terminal escape sequences, and various text formatting elements that affect visual width calculations.
Key Features
- Unicode Compliance: Full support for wide characters including CJK (Chinese, Japanese, Korean) scripts
- ANSI Escape Sequence Handling: Accurately ignores color codes and terminal formatting when measuring width
- Zero-Width Character Support: Properly handles combining characters and other non-printing elements
- Performance Optimized: Efficient single-pass algorithm with minimal memory allocation
- Zero Dependencies: Lightweight implementation with minimal external dependencies
Installation
Add widest-line to your project dependencies:
[]
= "0.1.0"
Or install via cargo:
Quick Start
use widest_line;
API Reference
widest_line(string: &str) -> usize
Calculates and returns the display width of the widest line in the provided string.
Parameters
string: &str- Input text to analyze (may contain multiple lines)
Returns
usize- Display width in columns of the widest line. Returns0for empty input.
Time Complexity
- O(n) where n is the total number of characters in the input string
Usage Examples
Standard Text Processing
use widest_line;
let content = "Short line\nMedium length line\nThis is the longest line in the text";
assert_eq!;
Unicode and International Text
// CJK characters are properly measured as double-width
let multilingual = "Hello\nδΈη\nBonjour";
assert_eq!; // "Bonjour" is widest
Terminal Output with ANSI Codes
// ANSI escape sequences are ignored in width calculation
let terminal_output = "\x1b[31mError:\x1b[0m File not found\n\x1b[32mSuccess:\x1b[0m Operation completed";
assert_eq!; // "Success: Operation completed"
Edge Cases and Special Characters
// Empty input
assert_eq!;
// Whitespace-only lines
assert_eq!;
// Emoji and special characters
assert_eq!;
CLI Tool
This crate also includes a command-line tool for analyzing text files:
# Analyze a file
# Analyze text directly
# Read from stdin
|
# Run interactive demo
The CLI tool provides:
- Colorized output with detailed line analysis
- Support for files, stdin, or direct text input
- Interactive demo mode with various text examples
- Verbose mode showing per-line width calculations
Use Cases
- Terminal Applications: Calculating column widths for table formatting
- Text Processing: Determining layout requirements for multi-line content
- CLI Tools: Sizing output buffers and formatting console displays
- Documentation Tools: Measuring code block widths and text alignment
- Log Analysis: Processing terminal logs with embedded ANSI codes
Technical Details
This crate leverages the string-width library for precise Unicode width calculations, ensuring compatibility with:
- Unicode Standard: Full compliance with Unicode display width specifications
- Terminal Emulators: Accurate rendering across different terminal implementations
- Text Editors: Consistent width calculations for code formatting tools
Performance
- Memory Efficient: Single-pass algorithm with O(1) additional memory usage
- Fast Processing: Optimized for large text files and real-time applications
- No Allocations: Zero-copy string processing where possible
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
This project is licensed under either of
at your option.
Acknowledgments
Built with string-width for robust Unicode width calculations.