| Crates.io | widest-line |
| lib.rs | widest-line |
| version | 0.1.0 |
| created_at | 2025-09-07 23:36:16.404817+00 |
| updated_at | 2025-09-07 23:36:16.404817+00 |
| description | Find the widest line in a string with proper Unicode and ANSI escape code support |
| homepage | https://github.com/sabry-awad97/widest-line |
| repository | https://github.com/sabry-awad97/widest-line |
| max_upload_size | |
| id | 1828608 |
| size | 46,389 |
A high-performance Rust library for determining the display width of the widest line in multi-line strings.
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.
Add widest-line to your project dependencies:
[dependencies]
widest-line = "0.1.0"
Or install via cargo:
cargo add widest-line
use widest_line::widest_line;
fn main() {
let text = "Hello\nWorld!\nThis is a longer line";
let width = widest_line(text);
println!("Widest line is {} characters wide", width); // Output: 21
}
widest_line(string: &str) -> usizeCalculates and returns the display width of the widest line in the provided string.
string: &str - Input text to analyze (may contain multiple lines)usize - Display width in columns of the widest line. Returns 0 for empty input.use widest_line::widest_line;
let content = "Short line\nMedium length line\nThis is the longest line in the text";
assert_eq!(widest_line(content), 33);
// CJK characters are properly measured as double-width
let multilingual = "Hello\n世界\nBonjour";
assert_eq!(widest_line(multilingual), 7); // "Bonjour" is widest
// 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!(widest_line(terminal_output), 25); // "Success: Operation completed"
// Empty input
assert_eq!(widest_line(""), 0);
// Whitespace-only lines
assert_eq!(widest_line("\n \n\t\n"), 2);
// Emoji and special characters
assert_eq!(widest_line("Text with emoji 🦀\nRegular text"), 18);
This crate also includes a command-line tool for analyzing text files:
# Analyze a file
cargo run --example cli -- --file myfile.txt --verbose
# Analyze text directly
cargo run --example cli -- --text "Hello\nWorld!" --verbose
# Read from stdin
echo -e "Line 1\nLonger line 2" | cargo run --example cli -- --file - --verbose
# Run interactive demo
cargo run --example cli -- --demo --verbose
The CLI tool provides:
This crate leverages the string-width library for precise Unicode width calculations, ensuring compatibility with:
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.
This project is licensed under either of
at your option.
Built with string-width for robust Unicode width calculations.