string-width 0.1.0

Accurate Unicode string width calculation for terminal applications, handling emoji, East Asian characters, combining marks, and ANSI escape sequences
Documentation
#![deny(missing_docs)]

//! # string-width
//!
//! Accurate Unicode string width calculation for terminal applications.
//!
//! This library correctly handles:
//! - **Emoji sequences** (πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦, πŸ‡ΊπŸ‡Έ, 1️⃣)
//! - **East Asian characters** (δ½ ε₯½, こんにけは, μ•ˆλ…•ν•˜μ„Έμš”)
//! - **Combining marks and diacritics** (Γ©, Γ±, ΓΌ)
//! - **Zero-width characters** (ZWJ, ZWNJ, format characters)
//! - **ANSI escape sequences** (colors, cursor movement)
//! - **Ambiguous width characters** (Β±, Γ—, Γ·)
//!
//! ## Quick Start
//!
//! ```rust
//! use string_width::string_width;
//!
//! // ASCII text
//! assert_eq!(string_width("Hello"), 5);
//!
//! // East Asian characters (full-width)
//! assert_eq!(string_width("δ½ ε₯½"), 4);
//!
//! // Emoji
//! assert_eq!(string_width("πŸ‘‹"), 2);
//! assert_eq!(string_width("πŸ‡ΊπŸ‡Έ"), 2);  // Flag
//! assert_eq!(string_width("1️⃣"), 2);   // Keycap sequence
//!
//! // Mixed content
//! assert_eq!(string_width("Hello πŸ‘‹ δΈ–η•Œ"), 13);
//!
//! // ANSI escape sequences are ignored by default
//! assert_eq!(string_width("\x1b[31mRed\x1b[0m"), 3);
//! ```
//!
//! ## Advanced Configuration
//!
//! ```rust
//! use string_width::{string_width, string_width_with_options, StringWidthOptions, AmbiguousWidthTreatment};
//!
//! // Direct configuration
//! let options = StringWidthOptions {
//!     count_ansi: true,  // Count ANSI escape sequences
//!     ambiguous_width: AmbiguousWidthTreatment::Wide,  // Treat ambiguous as wide
//! };
//!
//! assert_eq!(string_width_with_options("Β±Γ—Γ·", options.clone()), 6);  // Ambiguous chars as wide
//! assert_eq!(string_width_with_options("\x1b[31mRed\x1b[0m", options), 12);  // Count ANSI
//!
//! // Using the builder pattern (recommended)
//! let options = StringWidthOptions::builder()
//!     .count_ansi(true)
//!     .ambiguous_as_wide()
//!     .build();
//!
//! assert_eq!(string_width_with_options("Β±Γ—Γ·", options), 6);
//! ```
//!
//! ## Using the DisplayWidth Trait
//!
//! ```rust
//! use string_width::{DisplayWidth, StringWidthOptions};
//!
//! let text = "Hello 🌍";
//! println!("Width: {}", text.display_width());
//!
//! // With custom options
//! let options = StringWidthOptions::default();
//! println!("Width: {}", text.display_width_with_options(options));
//! ```

// Re-export the main functionality
pub use crate::options::{AmbiguousWidthTreatment, StringWidthOptions, StringWidthOptionsBuilder};
pub use crate::width_calculation::{DisplayWidth, string_width, string_width_with_options};

// Internal modules
/// Character classification utilities for width calculation
///
/// This module provides functions and types for classifying Unicode characters
/// into categories needed for accurate width calculation, including combining
/// marks, format characters, and prepend characters.
mod character_classification;

/// Emoji detection and classification
///
/// This module handles the complex logic of determining whether a grapheme
/// cluster represents an emoji that should be displayed with width 2,
/// including variation selectors, keycap sequences, and regional indicators.
mod emoji;
mod options;
mod unicode_constants;
mod width_calculation;

#[cfg(test)]
mod tests;