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
/// Basic string width calculation examples
///
/// Run with: cargo run --example basic_usage
use string_width::{
    AmbiguousWidthTreatment, StringWidthOptions, string_width, string_width_with_options,
};

fn main() {
    println!("=== Basic String Width Examples ===\n");

    // Basic ASCII text
    let text = "Hello, World!";
    println!("Text: '{}'", text);
    println!("Width: {}\n", string_width(text));

    // East Asian characters (full-width)
    let text = "你好世界";
    println!("Text: '{}'", text);
    println!("Width: {}\n", string_width(text));

    // Mixed content
    let text = "Hello 世界!";
    println!("Text: '{}'", text);
    println!("Width: {}\n", string_width(text));

    // Emoji examples
    let text = "Hello 👋 World 🌍";
    println!("Text: '{}'", text);
    println!("Width: {}\n", string_width(text));

    // Complex emoji sequences
    let text = "Family: 👨‍👩‍👧‍👦";
    println!("Text: '{}'", text);
    println!("Width: {}\n", string_width(text));

    // Keycap sequences
    let text = "Numbers: 1️⃣ 2️⃣ 3️⃣";
    println!("Text: '{}'", text);
    println!("Width: {}\n", string_width(text));

    // Flag sequences
    let text = "Flags: 🇺🇸 🇯🇵 🇬🇧";
    println!("Text: '{}'", text);
    println!("Width: {}\n", string_width(text));

    // ANSI escape sequences (stripped by default)
    let text = "\x1b[31mRed text\x1b[0m";
    println!("Text with ANSI: '{}'", text);
    println!("Width (ANSI stripped): {}", string_width(text));

    let options = StringWidthOptions {
        count_ansi: true,
        ambiguous_width: AmbiguousWidthTreatment::Narrow,
    };
    println!(
        "Width (ANSI counted): {}\n",
        string_width_with_options(text, options)
    );

    // Ambiguous width characters
    let text = "±×÷";
    println!("Text: '{}'", text);
    println!("Width (narrow): {}", string_width(text));

    let options = StringWidthOptions {
        count_ansi: false,
        ambiguous_width: AmbiguousWidthTreatment::Wide,
    };
    println!(
        "Width (wide): {}\n",
        string_width_with_options(text, options)
    );

    // Zero-width characters
    let text = "a\u{200B}b\u{200C}c";
    println!("Text with zero-width chars: 'a\\u{{200B}}b\\u{{200C}}c'");
    println!("Width: {}\n", string_width(text));

    // Combining characters
    let text = "e\u{0301}"; // e with acute accent
    println!("Text with combining: 'e\\u{{0301}}'");
    println!("Width: {}\n", string_width(text));
}