mgeisler / textwrap

An efficient and powerful Rust library for word wrapping text.
MIT License
446 stars 44 forks source link

Unexpected output on `wrap()` #536

Closed rsuu closed 5 months ago

rsuu commented 5 months ago
output:

[examples/bug.rs:10:5] &success = "\u{1b}[0m\u{1b}[32mTest Wrap\u{1b}[0m"
Test-Wrap

// expected output: `Test-Wr`
// current        : `Test-`
// And missed `\u{1b}[0m` at the end.
[examples/bug.rs:16:5] &failed = "\u{1b}[0m\u{1b}[32mTest" 
Test-
fn main() {
    let width = 9;
    let text = "\u{1b}[0m\u{1b}[32mTest-Wrap\u{1b}[0m";

    let success = textwrap::wrap(&text, width)[0].to_string();
    dbg!(&success);
    println!("{success}");

    let failed = textwrap::wrap(&text, width - 2)[0].to_string();
        //                             ^^^^^^^^^ here                      
    dbg!(&failed);
    println!("{failed}");
}
rsuu commented 5 months ago

my bad...

Describes how to wrap words into lines. https://docs.rs/textwrap/latest/textwrap/wrap_algorithms/enum.WrapAlgorithm.html