boxen 0.4.0

A Rust library for creating styled terminal boxes around text with performance optimizations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/// Performance tests for boxen library
use ::boxen::{
    BorderStyle, BoxenOptions, Color, Height, Spacing, TextAlignment, TitleAlignment, Width, boxen,
    builder,
};
use std::time::Instant;

// Performance test thresholds (in milliseconds)
const SMALL_TEXT_THRESHOLD: u128 = 30;
const MEDIUM_TEXT_THRESHOLD: u128 = 100;
const LARGE_TEXT_THRESHOLD: u128 = 200;
const COMPLEX_CONFIG_THRESHOLD: u128 = 20;
const REPEATED_RENDER_THRESHOLD: u128 = 500;

#[test]
fn test_performance_small_text() {
    let text = "Small text performance test";

    let start = Instant::now();
    let result = boxen(text, None);
    let duration = start.elapsed();

    assert!(result.is_ok());
    assert!(
        duration.as_millis() < SMALL_TEXT_THRESHOLD,
        "Small text took too long: {duration:?} (threshold: {SMALL_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_medium_text() {
    let text = "Medium length text for performance testing. ".repeat(50);

    let start = Instant::now();
    let result = boxen(
        &text,
        Some(BoxenOptions {
            width: Some(Width::Fixed(60)), // Reduce width to fit in smaller terminals
            height: Some(Height::Fixed(8)), // Add height constraint
            ..Default::default()
        }),
    );
    let duration = start.elapsed();

    assert!(result.is_ok());
    assert!(
        duration.as_millis() < MEDIUM_TEXT_THRESHOLD,
        "Medium text took too long: {duration:?} (threshold: {MEDIUM_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_large_text() {
    let text = "Large text content for performance testing. ".repeat(500);

    let start = Instant::now();
    let result = boxen(
        &text,
        Some(BoxenOptions {
            width: Some(Width::Fixed(60)),   // Reduce width
            height: Some(Height::Fixed(10)), // Smaller height to fit in terminal
            ..Default::default()
        }),
    );
    let duration = start.elapsed();

    assert!(result.is_ok());
    assert!(
        duration.as_millis() < LARGE_TEXT_THRESHOLD,
        "Large text took too long: {duration:?} (threshold: {LARGE_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_many_lines() {
    let many_lines = (0..1000)
        .map(|i| format!("Line number {i} with some content"))
        .collect::<Vec<_>>()
        .join("\n");

    let start = Instant::now();
    let result = boxen(
        &many_lines,
        Some(BoxenOptions {
            width: Some(Width::Fixed(60)),   // Reduce width
            height: Some(Height::Fixed(10)), // Smaller height to fit in terminal
            ..Default::default()
        }),
    );
    let duration = start.elapsed();

    assert!(result.is_ok());
    assert!(
        duration.as_millis() < LARGE_TEXT_THRESHOLD,
        "Many lines took too long: {duration:?} (threshold: {LARGE_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_complex_configuration() {
    let text = "Complex configuration performance test";

    let start = Instant::now();
    let result = builder()
        .border_style(BorderStyle::Double)
        .padding(1) // Reduced padding to fit in test terminal
        .margin(1) // Reduced margin to fit in test terminal
        .text_alignment(TextAlignment::Center)
        .title("Performance Test")
        .title_alignment(TitleAlignment::Center)
        .width(60)
        // Removed height constraint to avoid terminal size conflicts
        .border_color("red")
        .background_color("#ffffff")
        .dim_border(true)
        .render(text);
    let duration = start.elapsed();

    assert!(result.is_ok());
    assert!(
        duration.as_millis() < COMPLEX_CONFIG_THRESHOLD,
        "Complex configuration took too long: {duration:?} (threshold: {COMPLEX_CONFIG_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_unicode_content() {
    let unicode_text =
        "Unicode performance: 🌍🌎🌏 你好世界 🚀✨🎉 Émojis: àáâãäåæçèéêë ".repeat(100);

    let start = Instant::now();
    let result = boxen(
        &unicode_text,
        Some(BoxenOptions {
            width: Some(Width::Fixed(60)),   // Reduce width
            height: Some(Height::Fixed(10)), // Add height constraint
            text_alignment: TextAlignment::Center,
            ..Default::default()
        }),
    );
    let duration = start.elapsed();

    assert!(result.is_ok());
    assert!(
        duration.as_millis() < MEDIUM_TEXT_THRESHOLD,
        "Unicode content took too long: {duration:?} (threshold: {MEDIUM_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_repeated_rendering() {
    let text = "Repeated rendering performance test";
    let options = BoxenOptions {
        border_style: BorderStyle::Round,
        padding: Spacing::from(1),
        title: Some("Repeat Test".to_string()),
        ..Default::default()
    };

    let start = Instant::now();
    for _ in 0..100 {
        let result = boxen(text, Some(options.clone()));
        assert!(result.is_ok());
    }
    let duration = start.elapsed();

    assert!(
        duration.as_millis() < REPEATED_RENDER_THRESHOLD,
        "100 repeated renderings took too long: {duration:?} (threshold: {REPEATED_RENDER_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_builder_pattern() {
    let text = "Builder pattern performance test";

    let start = Instant::now();
    for _ in 0..50 {
        let result = builder()
            .border_style(BorderStyle::Bold)
            .padding(2)
            .title("Builder Test")
            .width(40)
            .border_color("blue")
            .render(text);
        assert!(result.is_ok());
    }
    let duration = start.elapsed();

    assert!(
        duration.as_millis() < REPEATED_RENDER_THRESHOLD / 2,
        "50 builder pattern renderings took too long: {:?} (threshold: {}ms)",
        duration,
        REPEATED_RENDER_THRESHOLD / 2
    );
}

#[test]
fn test_performance_different_border_styles() {
    let text = "Border style performance test";
    let styles = [
        BorderStyle::Single,
        BorderStyle::Double,
        BorderStyle::Round,
        BorderStyle::Bold,
        BorderStyle::SingleDouble,
        BorderStyle::DoubleSingle,
        BorderStyle::Classic,
        BorderStyle::None,
    ];

    let start = Instant::now();
    for style in &styles {
        for _ in 0..10 {
            let result = boxen(
                text,
                Some(BoxenOptions {
                    border_style: *style,
                    ..Default::default()
                }),
            );
            assert!(result.is_ok());
        }
    }
    let duration = start.elapsed();

    assert!(
        duration.as_millis() < REPEATED_RENDER_THRESHOLD,
        "Border style variations took too long: {duration:?} (threshold: {REPEATED_RENDER_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_color_combinations() {
    let text = "Color performance test";
    let colors = [
        Color::Named("red".to_string()),
        Color::Named("blue".to_string()),
        Color::Hex("#ff0000".to_string()),
        Color::Hex("#00ff00".to_string()),
        Color::Rgb(255, 0, 255),
        Color::Rgb(0, 255, 255),
    ];

    let start = Instant::now();
    for border_color in &colors {
        for background_color in &colors {
            let result = boxen(
                text,
                Some(BoxenOptions {
                    border_color: Some(border_color.clone()),
                    background_color: Some(background_color.clone()),
                    ..Default::default()
                }),
            );
            assert!(result.is_ok());
        }
    }
    let duration = start.elapsed();

    assert!(
        duration.as_millis() < REPEATED_RENDER_THRESHOLD,
        "Color combinations took too long: {duration:?} (threshold: {REPEATED_RENDER_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_text_alignment_variations() {
    let multiline_text = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5";
    let alignments = [
        TextAlignment::Left,
        TextAlignment::Center,
        TextAlignment::Right,
    ];

    let start = Instant::now();
    for alignment in &alignments {
        for width in &[20, 40, 60, 80] {
            let result = boxen(
                multiline_text,
                Some(BoxenOptions {
                    text_alignment: *alignment,
                    width: Some(boxen::Width::Fixed(*width)),
                    ..Default::default()
                }),
            );
            assert!(result.is_ok());
        }
    }
    let duration = start.elapsed();

    assert!(
        duration.as_millis() < MEDIUM_TEXT_THRESHOLD,
        "Text alignment variations took too long: {duration:?} (threshold: {MEDIUM_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_spacing_variations() {
    let text = "Spacing performance test";
    let spacing_values = [
        Spacing::from(0),
        Spacing::from(1),
        Spacing::from(2),
        Spacing::from((1, 2, 3, 4)),
        Spacing::from([2, 4]),
    ];

    let start = Instant::now();
    for padding in &spacing_values {
        for margin in &spacing_values {
            // Skip combinations that would exceed terminal size
            let total_vertical = padding.vertical() + margin.vertical();
            if total_vertical > 10 {
                continue;
            }

            let result = boxen(
                text,
                Some(BoxenOptions {
                    padding: *padding,
                    margin: *margin,
                    width: Some(Width::Fixed(40)),
                    height: Some(Height::Fixed(8)), // Add height constraint
                    ..Default::default()
                }),
            );
            if result.is_err() {
                continue; // Skip problematic combinations
            }
            assert!(result.is_ok());
        }
    }
    let duration = start.elapsed();

    assert!(
        duration.as_millis() < MEDIUM_TEXT_THRESHOLD,
        "Spacing variations took too long: {duration:?} (threshold: {MEDIUM_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_dimension_scaling() {
    let text = "Dimension scaling test content";

    // Test width scaling (limit to reasonable sizes)
    let start = Instant::now();
    for width in &[10, 20, 40, 60] {
        let result = boxen(
            text,
            Some(BoxenOptions {
                width: Some(boxen::Width::Fixed(*width)),
                height: Some(Height::Fixed(5)), // Add height constraint
                ..Default::default()
            }),
        );
        if result.is_err() {
            continue; // Skip problematic sizes
        }
        assert!(result.is_ok());
    }
    let width_duration = start.elapsed();

    // Test height scaling with long content
    let long_content = (0..50)
        .map(|i| format!("Line {i}"))
        .collect::<Vec<_>>()
        .join("\n"); // Reduce content
    let start = Instant::now();
    for height in &[5, 8, 10, 12] {
        // Smaller heights
        let result = boxen(
            &long_content,
            Some(BoxenOptions {
                height: Some(boxen::Height::Fixed(*height)),
                width: Some(Width::Fixed(60)), // Reduce width
                ..Default::default()
            }),
        );
        if result.is_err() {
            continue; // Skip problematic sizes
        }
        assert!(result.is_ok());
    }
    let height_duration = start.elapsed();

    assert!(
        width_duration.as_millis() < SMALL_TEXT_THRESHOLD,
        "Width scaling took too long: {width_duration:?} (threshold: {SMALL_TEXT_THRESHOLD}ms)"
    );

    assert!(
        height_duration.as_millis() < MEDIUM_TEXT_THRESHOLD,
        "Height scaling took too long: {height_duration:?} (threshold: {MEDIUM_TEXT_THRESHOLD}ms)"
    );
}

#[test]
fn test_performance_memory_efficiency() {
    // Test that repeated allocations don't cause performance degradation
    let text = "Memory efficiency test";
    let mut durations = Vec::new();

    // Measure performance over multiple batches
    for batch in 0..5 {
        let start = Instant::now();

        for _ in 0..20 {
            let result = builder()
                .border_style(BorderStyle::Double)
                .padding(2)
                .title(format!("Batch {batch}"))
                .width(50)
                .render(text);
            assert!(result.is_ok());

            // Force the result to be used to prevent optimization
            let output = result.unwrap();
            let _length = output.len();
        }

        durations.push(start.elapsed());
    }

    // Performance should not degrade significantly over time
    let first_batch = durations[0];
    let last_batch = durations[durations.len() - 1];

    // Last batch should not be more than 2x slower than first batch
    assert!(
        last_batch.as_millis() <= first_batch.as_millis() * 2,
        "Performance degraded over time: first={first_batch:?}, last={last_batch:?}"
    );
}

#[test]
fn test_performance_edge_cases() {
    // Test performance with edge case inputs
    let edge_cases = [
        ("Empty text", ""),
        ("Single character", "A"),
        ("Very long single line", &"A".repeat(1000)),
        ("Many empty lines", &"\n".repeat(100)),
        (
            "Mixed content",
            &format!("{}\n{}\n{}", "Short", "A".repeat(100), "Short again"),
        ),
    ];

    for (desc, text) in &edge_cases {
        let start = Instant::now();
        let result = boxen(
            text,
            Some(BoxenOptions {
                width: Some(Width::Fixed(60)),   // Reduce width
                height: Some(Height::Fixed(10)), // Smaller height
                ..Default::default()
            }),
        );
        let duration = start.elapsed();

        if result.is_err() {
            continue; // Skip problematic edge cases
        }
        assert!(result.is_ok(), "Failed for edge case: {desc}");
        assert!(
            duration.as_millis() < SMALL_TEXT_THRESHOLD,
            "Edge case '{desc}' took too long: {duration:?} (threshold: {SMALL_TEXT_THRESHOLD}ms)"
        );
    }
}