widest-line

Crates.iowidest-line
lib.rswidest-line
version0.1.0
created_at2025-09-07 23:36:16.404817+00
updated_at2025-09-07 23:36:16.404817+00
descriptionFind the widest line in a string with proper Unicode and ANSI escape code support
homepagehttps://github.com/sabry-awad97/widest-line
repositoryhttps://github.com/sabry-awad97/widest-line
max_upload_size
id1828608
size46,389
Sabry Awad (sabry-awad97)

documentation

README

widest-line

Crates.io Documentation License

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:

[dependencies]
widest-line = "0.1.0"

Or install via cargo:

cargo add widest-line

Quick Start

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
}

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. Returns 0 for 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::widest_line;

let content = "Short line\nMedium length line\nThis is the longest line in the text";
assert_eq!(widest_line(content), 33);

Unicode and International Text

// CJK characters are properly measured as double-width
let multilingual = "Hello\n世界\nBonjour";
assert_eq!(widest_line(multilingual), 7); // "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!(widest_line(terminal_output), 25); // "Success: Operation completed"

Edge Cases and Special Characters

// 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);

CLI Tool

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:

  • 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.

Commit count: 3

cargo fmt