widest-line 0.1.0

Find the widest line in a string with proper Unicode and ANSI escape code support
Documentation
# widest-line


[![Crates.io](https://img.shields.io/crates/v/widest-line.svg)](https://crates.io/crates/widest-line)
[![Documentation](https://docs.rs/widest-line/badge.svg)](https://docs.rs/widest-line)
[![License](https://img.shields.io/crates/l/widest-line.svg)](https://github.com/sabry-awad97/widest-line#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:

```toml
[dependencies]
widest-line = "0.1.0"
```

Or install via cargo:

```bash
cargo add widest-line
```

## Quick Start


```rust
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


```rust
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


```rust
// 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


```rust
// 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


```rust
// 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:

```bash
# 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`](https://crates.io/crates/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

- [Apache License, Version 2.0]LICENSE-APACHE
- [MIT License]LICENSE-MIT

at your option.

## Acknowledgments


Built with [string-width](https://crates.io/crates/string-width) for robust Unicode width calculations.