WrapOptionsBuilder

Struct WrapOptionsBuilder 

Source
pub struct WrapOptionsBuilder { /* private fields */ }
Expand description

🏗️ Fluent builder for creating WrapOptions with a clean, readable interface.

This builder provides a more ergonomic way to configure wrapping options compared to struct initialization, especially when you only need to change a few settings from the defaults.

§Examples

§Basic Usage

use wrap_ansi::WrapOptions;

let options = WrapOptions::builder()
    .hard_wrap(true)
    .build();

§Method Chaining

use wrap_ansi::{wrap_ansi, WrapOptions};

let result = wrap_ansi(
    "Some long text that needs wrapping",
    15,
    Some(WrapOptions::builder()
        .hard_wrap(true)
        .trim_whitespace(false)
        .word_wrap(true)
        .build())
);

§Conditional Configuration

use wrap_ansi::WrapOptions;

let preserve_formatting = true;
let strict_width = false;

let options = WrapOptions::builder()
    .trim_whitespace(!preserve_formatting)
    .hard_wrap(strict_width)
    .build();

Implementations§

Source§

impl WrapOptionsBuilder

Source

pub const fn hard_wrap(self, enabled: bool) -> Self

⚡ Configure hard wrapping behavior.

When enabled, long words that exceed the column limit will be broken at the boundary. When disabled (default), long words move to the next line.

§Examples
use wrap_ansi::WrapOptions;

let strict = WrapOptions::builder().hard_wrap(true).build();
let flexible = WrapOptions::builder().hard_wrap(false).build();
Examples found in repository?
examples/improved_features.rs (line 9)
3fn main() -> Result<(), WrapError> {
4    println!("=== Wrap-ANSI Improved Features Demo ===\n");
5
6    // 1. Builder Pattern for Options
7    println!("1. Builder Pattern for Options:");
8    let options = WrapOptions::builder()
9        .hard_wrap(true)
10        .trim_whitespace(false)
11        .word_wrap(true)
12        .build();
13
14    let text = "This is a verylongwordthatexceedslimit example";
15    let wrapped = wrap_ansi(text, 15, Some(options));
16    println!("Input: {}", text);
17    println!("Output:\n{}\n", wrapped);
18
19    // 2. Error Handling with Input Validation
20    println!("2. Error Handling:");
21
22    // Valid input
23    match wrap_ansi_checked("Hello world", 10, None) {
24        Ok(result) => println!("Valid input wrapped: {}", result.replace('\n', "\\n")),
25        Err(e) => println!("Error: {}", e),
26    }
27
28    // Invalid column width
29    match wrap_ansi_checked("Hello world", 0, None) {
30        Ok(result) => println!("Result: {}", result),
31        Err(e) => println!("Expected error: {}", e),
32    }
33
34    // Large input (simulated)
35    let large_text = "x".repeat(100);
36    match wrap_ansi_checked(&large_text, 10, None) {
37        Ok(result) => println!(
38            "Large input handled: {} chars -> {} chars",
39            large_text.len(),
40            result.len()
41        ),
42        Err(e) => println!("Error with large input: {}", e),
43    }
44
45    println!();
46
47    // 3. ANSI Color Preservation with Named Constants
48    println!("3. ANSI Color Preservation:");
49    let colored_text =
50        "\u{001B}[31mThis is red text that will be wrapped across multiple lines\u{001B}[39m";
51    let wrapped_colored = wrap_ansi(colored_text, 20, None);
52    println!("Colored text wrapped:");
53    println!("{}", wrapped_colored);
54    println!();
55
56    // 4. Hyperlink Preservation
57    println!("4. Hyperlink Preservation:");
58    let hyperlink_text = "\u{001B}]8;;https://example.com\u{0007}This is a clickable link that spans multiple lines\u{001B}]8;;\u{0007}";
59    let wrapped_hyperlink = wrap_ansi(hyperlink_text, 15, None);
60    println!("Hyperlink text wrapped:");
61    println!("{}", wrapped_hyperlink);
62    println!();
63
64    // 5. Performance with Complex ANSI Sequences
65    println!("5. Complex ANSI Sequences:");
66    let complex_text =
67        "\u{001B}[31m\u{001B}[42mRed text on green background\u{001B}[39m\u{001B}[49m normal text";
68    let wrapped_complex = wrap_ansi(complex_text, 12, None);
69    println!("Complex ANSI wrapped:");
70    println!("{}", wrapped_complex);
71    println!();
72
73    // 6. Unicode Support
74    println!("6. Unicode Support:");
75    let unicode_text = "Hello 世界 🌍 こんにちは";
76    let wrapped_unicode = wrap_ansi(unicode_text, 10, None);
77    println!("Unicode text wrapped:");
78    println!("{}", wrapped_unicode);
79
80    Ok(())
81}
Source

pub const fn trim_whitespace(self, enabled: bool) -> Self

🧹 Configure whitespace trimming behavior.

When enabled (default), leading and trailing whitespace is removed from wrapped lines. When disabled, all whitespace is preserved.

§Examples
use wrap_ansi::WrapOptions;

let clean = WrapOptions::builder().trim_whitespace(true).build();
let preserve = WrapOptions::builder().trim_whitespace(false).build();
Examples found in repository?
examples/improved_features.rs (line 10)
3fn main() -> Result<(), WrapError> {
4    println!("=== Wrap-ANSI Improved Features Demo ===\n");
5
6    // 1. Builder Pattern for Options
7    println!("1. Builder Pattern for Options:");
8    let options = WrapOptions::builder()
9        .hard_wrap(true)
10        .trim_whitespace(false)
11        .word_wrap(true)
12        .build();
13
14    let text = "This is a verylongwordthatexceedslimit example";
15    let wrapped = wrap_ansi(text, 15, Some(options));
16    println!("Input: {}", text);
17    println!("Output:\n{}\n", wrapped);
18
19    // 2. Error Handling with Input Validation
20    println!("2. Error Handling:");
21
22    // Valid input
23    match wrap_ansi_checked("Hello world", 10, None) {
24        Ok(result) => println!("Valid input wrapped: {}", result.replace('\n', "\\n")),
25        Err(e) => println!("Error: {}", e),
26    }
27
28    // Invalid column width
29    match wrap_ansi_checked("Hello world", 0, None) {
30        Ok(result) => println!("Result: {}", result),
31        Err(e) => println!("Expected error: {}", e),
32    }
33
34    // Large input (simulated)
35    let large_text = "x".repeat(100);
36    match wrap_ansi_checked(&large_text, 10, None) {
37        Ok(result) => println!(
38            "Large input handled: {} chars -> {} chars",
39            large_text.len(),
40            result.len()
41        ),
42        Err(e) => println!("Error with large input: {}", e),
43    }
44
45    println!();
46
47    // 3. ANSI Color Preservation with Named Constants
48    println!("3. ANSI Color Preservation:");
49    let colored_text =
50        "\u{001B}[31mThis is red text that will be wrapped across multiple lines\u{001B}[39m";
51    let wrapped_colored = wrap_ansi(colored_text, 20, None);
52    println!("Colored text wrapped:");
53    println!("{}", wrapped_colored);
54    println!();
55
56    // 4. Hyperlink Preservation
57    println!("4. Hyperlink Preservation:");
58    let hyperlink_text = "\u{001B}]8;;https://example.com\u{0007}This is a clickable link that spans multiple lines\u{001B}]8;;\u{0007}";
59    let wrapped_hyperlink = wrap_ansi(hyperlink_text, 15, None);
60    println!("Hyperlink text wrapped:");
61    println!("{}", wrapped_hyperlink);
62    println!();
63
64    // 5. Performance with Complex ANSI Sequences
65    println!("5. Complex ANSI Sequences:");
66    let complex_text =
67        "\u{001B}[31m\u{001B}[42mRed text on green background\u{001B}[39m\u{001B}[49m normal text";
68    let wrapped_complex = wrap_ansi(complex_text, 12, None);
69    println!("Complex ANSI wrapped:");
70    println!("{}", wrapped_complex);
71    println!();
72
73    // 6. Unicode Support
74    println!("6. Unicode Support:");
75    let unicode_text = "Hello 世界 🌍 こんにちは";
76    let wrapped_unicode = wrap_ansi(unicode_text, 10, None);
77    println!("Unicode text wrapped:");
78    println!("{}", wrapped_unicode);
79
80    Ok(())
81}
Source

pub const fn word_wrap(self, enabled: bool) -> Self

🔤 Configure word boundary respect.

When enabled (default), text wraps at word boundaries (spaces). When disabled, text can wrap at any character position.

§Examples
use wrap_ansi::WrapOptions;

let word_aware = WrapOptions::builder().word_wrap(true).build();
let char_level = WrapOptions::builder().word_wrap(false).build();
Examples found in repository?
examples/improved_features.rs (line 11)
3fn main() -> Result<(), WrapError> {
4    println!("=== Wrap-ANSI Improved Features Demo ===\n");
5
6    // 1. Builder Pattern for Options
7    println!("1. Builder Pattern for Options:");
8    let options = WrapOptions::builder()
9        .hard_wrap(true)
10        .trim_whitespace(false)
11        .word_wrap(true)
12        .build();
13
14    let text = "This is a verylongwordthatexceedslimit example";
15    let wrapped = wrap_ansi(text, 15, Some(options));
16    println!("Input: {}", text);
17    println!("Output:\n{}\n", wrapped);
18
19    // 2. Error Handling with Input Validation
20    println!("2. Error Handling:");
21
22    // Valid input
23    match wrap_ansi_checked("Hello world", 10, None) {
24        Ok(result) => println!("Valid input wrapped: {}", result.replace('\n', "\\n")),
25        Err(e) => println!("Error: {}", e),
26    }
27
28    // Invalid column width
29    match wrap_ansi_checked("Hello world", 0, None) {
30        Ok(result) => println!("Result: {}", result),
31        Err(e) => println!("Expected error: {}", e),
32    }
33
34    // Large input (simulated)
35    let large_text = "x".repeat(100);
36    match wrap_ansi_checked(&large_text, 10, None) {
37        Ok(result) => println!(
38            "Large input handled: {} chars -> {} chars",
39            large_text.len(),
40            result.len()
41        ),
42        Err(e) => println!("Error with large input: {}", e),
43    }
44
45    println!();
46
47    // 3. ANSI Color Preservation with Named Constants
48    println!("3. ANSI Color Preservation:");
49    let colored_text =
50        "\u{001B}[31mThis is red text that will be wrapped across multiple lines\u{001B}[39m";
51    let wrapped_colored = wrap_ansi(colored_text, 20, None);
52    println!("Colored text wrapped:");
53    println!("{}", wrapped_colored);
54    println!();
55
56    // 4. Hyperlink Preservation
57    println!("4. Hyperlink Preservation:");
58    let hyperlink_text = "\u{001B}]8;;https://example.com\u{0007}This is a clickable link that spans multiple lines\u{001B}]8;;\u{0007}";
59    let wrapped_hyperlink = wrap_ansi(hyperlink_text, 15, None);
60    println!("Hyperlink text wrapped:");
61    println!("{}", wrapped_hyperlink);
62    println!();
63
64    // 5. Performance with Complex ANSI Sequences
65    println!("5. Complex ANSI Sequences:");
66    let complex_text =
67        "\u{001B}[31m\u{001B}[42mRed text on green background\u{001B}[39m\u{001B}[49m normal text";
68    let wrapped_complex = wrap_ansi(complex_text, 12, None);
69    println!("Complex ANSI wrapped:");
70    println!("{}", wrapped_complex);
71    println!();
72
73    // 6. Unicode Support
74    println!("6. Unicode Support:");
75    let unicode_text = "Hello 世界 🌍 こんにちは";
76    let wrapped_unicode = wrap_ansi(unicode_text, 10, None);
77    println!("Unicode text wrapped:");
78    println!("{}", wrapped_unicode);
79
80    Ok(())
81}
Source

pub const fn build(self) -> WrapOptions

🏁 Build the final WrapOptions configuration.

Consumes the builder and returns the configured WrapOptions struct.

§Examples
use wrap_ansi::WrapOptions;

let options = WrapOptions::builder()
    .hard_wrap(true)
    .trim_whitespace(false)
    .build();
Examples found in repository?
examples/improved_features.rs (line 12)
3fn main() -> Result<(), WrapError> {
4    println!("=== Wrap-ANSI Improved Features Demo ===\n");
5
6    // 1. Builder Pattern for Options
7    println!("1. Builder Pattern for Options:");
8    let options = WrapOptions::builder()
9        .hard_wrap(true)
10        .trim_whitespace(false)
11        .word_wrap(true)
12        .build();
13
14    let text = "This is a verylongwordthatexceedslimit example";
15    let wrapped = wrap_ansi(text, 15, Some(options));
16    println!("Input: {}", text);
17    println!("Output:\n{}\n", wrapped);
18
19    // 2. Error Handling with Input Validation
20    println!("2. Error Handling:");
21
22    // Valid input
23    match wrap_ansi_checked("Hello world", 10, None) {
24        Ok(result) => println!("Valid input wrapped: {}", result.replace('\n', "\\n")),
25        Err(e) => println!("Error: {}", e),
26    }
27
28    // Invalid column width
29    match wrap_ansi_checked("Hello world", 0, None) {
30        Ok(result) => println!("Result: {}", result),
31        Err(e) => println!("Expected error: {}", e),
32    }
33
34    // Large input (simulated)
35    let large_text = "x".repeat(100);
36    match wrap_ansi_checked(&large_text, 10, None) {
37        Ok(result) => println!(
38            "Large input handled: {} chars -> {} chars",
39            large_text.len(),
40            result.len()
41        ),
42        Err(e) => println!("Error with large input: {}", e),
43    }
44
45    println!();
46
47    // 3. ANSI Color Preservation with Named Constants
48    println!("3. ANSI Color Preservation:");
49    let colored_text =
50        "\u{001B}[31mThis is red text that will be wrapped across multiple lines\u{001B}[39m";
51    let wrapped_colored = wrap_ansi(colored_text, 20, None);
52    println!("Colored text wrapped:");
53    println!("{}", wrapped_colored);
54    println!();
55
56    // 4. Hyperlink Preservation
57    println!("4. Hyperlink Preservation:");
58    let hyperlink_text = "\u{001B}]8;;https://example.com\u{0007}This is a clickable link that spans multiple lines\u{001B}]8;;\u{0007}";
59    let wrapped_hyperlink = wrap_ansi(hyperlink_text, 15, None);
60    println!("Hyperlink text wrapped:");
61    println!("{}", wrapped_hyperlink);
62    println!();
63
64    // 5. Performance with Complex ANSI Sequences
65    println!("5. Complex ANSI Sequences:");
66    let complex_text =
67        "\u{001B}[31m\u{001B}[42mRed text on green background\u{001B}[39m\u{001B}[49m normal text";
68    let wrapped_complex = wrap_ansi(complex_text, 12, None);
69    println!("Complex ANSI wrapped:");
70    println!("{}", wrapped_complex);
71    println!();
72
73    // 6. Unicode Support
74    println!("6. Unicode Support:");
75    let unicode_text = "Hello 世界 🌍 こんにちは";
76    let wrapped_unicode = wrap_ansi(unicode_text, 10, None);
77    println!("Unicode text wrapped:");
78    println!("{}", wrapped_unicode);
79
80    Ok(())
81}

Trait Implementations§

Source§

impl Clone for WrapOptionsBuilder

Source§

fn clone(&self) -> WrapOptionsBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WrapOptionsBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WrapOptionsBuilder

Source§

fn default() -> WrapOptionsBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.