click-rs 1.0.0

A Rust port of Python's Click library for creating command-line interfaces
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
//! Integration tests for terminal UI functions.
//!
//! These tests verify the public API of the termui module.

use click::termui::{
    style, strip_ansi_codes, Color, ProgressBar,
    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE,
    BRIGHT_BLACK, BRIGHT_RED, BRIGHT_GREEN, BRIGHT_YELLOW,
    BRIGHT_BLUE, BRIGHT_MAGENTA, BRIGHT_CYAN, BRIGHT_WHITE, RESET,
    get_terminal_size,
};

// ============================================================================
// Color Code Tests
// ============================================================================

#[test]
fn test_color_fg_codes() {
    assert_eq!(Color::Black.fg_code(), 30);
    assert_eq!(Color::Red.fg_code(), 31);
    assert_eq!(Color::Green.fg_code(), 32);
    assert_eq!(Color::Yellow.fg_code(), 33);
    assert_eq!(Color::Blue.fg_code(), 34);
    assert_eq!(Color::Magenta.fg_code(), 35);
    assert_eq!(Color::Cyan.fg_code(), 36);
    assert_eq!(Color::White.fg_code(), 37);
}

#[test]
fn test_color_bright_fg_codes() {
    assert_eq!(Color::BrightBlack.fg_code(), 90);
    assert_eq!(Color::BrightRed.fg_code(), 91);
    assert_eq!(Color::BrightGreen.fg_code(), 92);
    assert_eq!(Color::BrightYellow.fg_code(), 93);
    assert_eq!(Color::BrightBlue.fg_code(), 94);
    assert_eq!(Color::BrightMagenta.fg_code(), 95);
    assert_eq!(Color::BrightCyan.fg_code(), 96);
    assert_eq!(Color::BrightWhite.fg_code(), 97);
}

#[test]
fn test_color_bg_codes() {
    assert_eq!(Color::Black.bg_code(), 40);
    assert_eq!(Color::Red.bg_code(), 41);
    assert_eq!(Color::Green.bg_code(), 42);
    assert_eq!(Color::Yellow.bg_code(), 43);
    assert_eq!(Color::Blue.bg_code(), 44);
    assert_eq!(Color::Magenta.bg_code(), 45);
    assert_eq!(Color::Cyan.bg_code(), 46);
    assert_eq!(Color::White.bg_code(), 47);
}

#[test]
fn test_color_bright_bg_codes() {
    assert_eq!(Color::BrightBlack.bg_code(), 100);
    assert_eq!(Color::BrightRed.bg_code(), 101);
    assert_eq!(Color::BrightGreen.bg_code(), 102);
    assert_eq!(Color::BrightYellow.bg_code(), 103);
    assert_eq!(Color::BrightBlue.bg_code(), 104);
    assert_eq!(Color::BrightMagenta.bg_code(), 105);
    assert_eq!(Color::BrightCyan.bg_code(), 106);
    assert_eq!(Color::BrightWhite.bg_code(), 107);
}

#[test]
fn test_color_reset_codes() {
    assert_eq!(Color::Reset.fg_code(), 39);
    assert_eq!(Color::Reset.bg_code(), 49);
}

// ============================================================================
// Color Constant Tests
// ============================================================================

#[test]
fn test_color_constants() {
    assert_eq!(BLACK, Color::Black);
    assert_eq!(RED, Color::Red);
    assert_eq!(GREEN, Color::Green);
    assert_eq!(YELLOW, Color::Yellow);
    assert_eq!(BLUE, Color::Blue);
    assert_eq!(MAGENTA, Color::Magenta);
    assert_eq!(CYAN, Color::Cyan);
    assert_eq!(WHITE, Color::White);
    assert_eq!(BRIGHT_BLACK, Color::BrightBlack);
    assert_eq!(BRIGHT_RED, Color::BrightRed);
    assert_eq!(BRIGHT_GREEN, Color::BrightGreen);
    assert_eq!(BRIGHT_YELLOW, Color::BrightYellow);
    assert_eq!(BRIGHT_BLUE, Color::BrightBlue);
    assert_eq!(BRIGHT_MAGENTA, Color::BrightMagenta);
    assert_eq!(BRIGHT_CYAN, Color::BrightCyan);
    assert_eq!(BRIGHT_WHITE, Color::BrightWhite);
    assert_eq!(RESET, Color::Reset);
}

// ============================================================================
// Style String Generation Tests
// ============================================================================

#[test]
fn test_style_no_formatting() {
    let result = style("hello", None, None, false, false, false, false, false, false, false, false);
    assert_eq!(result, "hello");
}

#[test]
fn test_style_fg_color_only() {
    let result = style("hello", Some(Color::Red), None, false, false, false, false, false, false, false, true);
    assert_eq!(result, "\x1b[31mhello\x1b[0m");
}

#[test]
fn test_style_bg_color_only() {
    let result = style("hello", None, Some(Color::Blue), false, false, false, false, false, false, false, true);
    assert_eq!(result, "\x1b[44mhello\x1b[0m");
}

#[test]
fn test_style_both_colors() {
    let result = style("hello", Some(Color::White), Some(Color::Red), false, false, false, false, false, false, false, true);
    assert!(result.starts_with("\x1b["));
    assert!(result.contains("37")); // white fg
    assert!(result.contains("41")); // red bg
    assert!(result.ends_with("\x1b[0m"));
}

#[test]
fn test_style_bold() {
    let result = style("hello", None, None, true, false, false, false, false, false, false, true);
    assert_eq!(result, "\x1b[1mhello\x1b[0m");
}

#[test]
fn test_style_dim() {
    let result = style("hello", None, None, false, true, false, false, false, false, false, true);
    assert_eq!(result, "\x1b[2mhello\x1b[0m");
}

#[test]
fn test_style_italic() {
    let result = style("hello", None, None, false, false, false, false, true, false, false, true);
    assert_eq!(result, "\x1b[3mhello\x1b[0m");
}

#[test]
fn test_style_underline() {
    let result = style("hello", None, None, false, false, true, false, false, false, false, true);
    assert_eq!(result, "\x1b[4mhello\x1b[0m");
}

#[test]
fn test_style_blink() {
    let result = style("hello", None, None, false, false, false, false, false, true, false, true);
    assert_eq!(result, "\x1b[5mhello\x1b[0m");
}

#[test]
fn test_style_overline() {
    let result = style("hello", None, None, false, false, false, true, false, false, false, true);
    assert_eq!(result, "\x1b[53mhello\x1b[0m");
}

#[test]
fn test_style_strikethrough() {
    let result = style("hello", None, None, false, false, false, false, false, false, true, true);
    assert_eq!(result, "\x1b[9mhello\x1b[0m");
}

#[test]
fn test_style_no_reset() {
    let result = style("hello", Some(Color::Green), None, false, false, false, false, false, false, false, false);
    assert_eq!(result, "\x1b[32mhello");
    assert!(!result.contains("\x1b[0m"));
}

#[test]
fn test_style_multiple_attributes() {
    let result = style(
        "hello",
        Some(Color::Red),
        Some(Color::White),
        true,  // bold
        false,
        true,  // underline
        false,
        false,
        false,
        false,
        true,  // reset
    );

    // Should contain bold (1), underline (4), red fg (31), white bg (47)
    assert!(result.starts_with("\x1b["));
    assert!(result.contains("1"));
    assert!(result.contains("4"));
    assert!(result.contains("31"));
    assert!(result.contains("47"));
    assert!(result.contains("hello"));
    assert!(result.ends_with("\x1b[0m"));
}

#[test]
fn test_style_all_attributes() {
    let result = style(
        "test",
        Some(Color::Blue),
        Some(Color::Yellow),
        true,  // bold
        true,  // dim
        true,  // underline
        true,  // overline
        true,  // italic
        true,  // blink
        true,  // strikethrough
        true,  // reset
    );

    assert!(result.starts_with("\x1b["));
    assert!(result.contains("1"));  // bold
    assert!(result.contains("2"));  // dim
    assert!(result.contains("3"));  // italic
    assert!(result.contains("4"));  // underline
    assert!(result.contains("5"));  // blink
    assert!(result.contains("9"));  // strikethrough
    assert!(result.contains("53")); // overline
    assert!(result.contains("34")); // blue fg
    assert!(result.contains("43")); // yellow bg
    assert!(result.ends_with("\x1b[0m"));
}

// ============================================================================
// Strip ANSI Codes Tests
// ============================================================================

#[test]
fn test_strip_ansi_plain_text() {
    let result = strip_ansi_codes("hello world");
    assert_eq!(result, "hello world");
}

#[test]
fn test_strip_ansi_simple_color() {
    let result = strip_ansi_codes("\x1b[31mred\x1b[0m");
    assert_eq!(result, "red");
}

#[test]
fn test_strip_ansi_multiple_codes() {
    let result = strip_ansi_codes("\x1b[1;31;40mbold red on black\x1b[0m");
    assert_eq!(result, "bold red on black");
}

#[test]
fn test_strip_ansi_mixed_content() {
    let result = strip_ansi_codes("before \x1b[32mgreen\x1b[0m after");
    assert_eq!(result, "before green after");
}

#[test]
fn test_strip_ansi_nested() {
    let result = strip_ansi_codes("\x1b[1m\x1b[31mred\x1b[0m\x1b[32mgreen\x1b[0m");
    assert_eq!(result, "redgreen");
}

#[test]
fn test_strip_ansi_empty_string() {
    let result = strip_ansi_codes("");
    assert_eq!(result, "");
}

#[test]
fn test_strip_ansi_only_codes() {
    let result = strip_ansi_codes("\x1b[31m\x1b[0m");
    assert_eq!(result, "");
}

// ============================================================================
// Progress Bar Tests
// ============================================================================

#[test]
fn test_progress_bar_render_initial() {
    let bar = ProgressBar::new(100, Some("Processing"), false, true, true, 20);
    let output = bar.render();

    assert!(output.contains("Processing"));
    assert!(output.contains("["));
    assert!(output.contains("]"));
    assert!(output.contains("0%"));
    assert!(output.contains("0/100"));
}

#[test]
fn test_progress_bar_render_half() {
    let mut bar = ProgressBar::new(100, None, false, true, true, 20);
    bar.set_position(50);
    let output = bar.render();

    assert!(output.contains("50%"));
    assert!(output.contains("50/100"));
}

#[test]
fn test_progress_bar_render_complete() {
    let mut bar = ProgressBar::new(100, None, false, true, true, 20);
    bar.finish();
    let output = bar.render();

    assert!(output.contains("100%"));
    assert!(output.contains("100/100"));
}

#[test]
fn test_progress_bar_update_incremental() {
    let mut bar = ProgressBar::new(100, None, false, true, false, 20);

    bar.update(25);
    let output1 = bar.render();
    assert!(output1.contains("25%"));

    bar.update(25);
    let output2 = bar.render();
    assert!(output2.contains("50%"));

    bar.update(50);
    let output3 = bar.render();
    assert!(output3.contains("100%"));
}

#[test]
fn test_progress_bar_zero_length() {
    let bar = ProgressBar::new(0, None, false, true, false, 20);
    let output = bar.render();

    // Should not panic and should show 0%
    assert!(output.contains("0%"));
}

#[test]
fn test_progress_bar_no_label() {
    let bar = ProgressBar::new(100, None, false, true, true, 20);
    let output = bar.render();

    // Should start with the bar
    assert!(output.starts_with("["));
}

#[test]
fn test_progress_bar_bar_chars() {
    let mut bar = ProgressBar::new(10, None, false, false, false, 10);
    bar.set_position(5);
    let output = bar.render();

    // Bar should have 5 filled and 5 empty
    assert!(output.contains("#####-----"));
}

#[test]
fn test_progress_bar_width() {
    let bar = ProgressBar::new(100, None, false, false, false, 30);
    let output = bar.render();

    // Count the dashes (should be 30 for 0% progress)
    let bar_content: String = output.chars()
        .skip_while(|c| *c != '[')
        .skip(1)
        .take_while(|c| *c != ']')
        .collect();

    assert_eq!(bar_content.len(), 30);
}

#[test]
fn test_progress_bar_overflow_protection() {
    let mut bar = ProgressBar::new(100, None, false, true, true, 20);

    // Try to update beyond the length
    bar.update(150);
    let output = bar.render();

    // Should cap at 100%
    assert!(output.contains("100%"));
    assert!(output.contains("100/100"));
}

#[test]
fn test_progress_bar_finish_idempotent() {
    let mut bar = ProgressBar::new(100, None, false, true, false, 20);

    bar.finish();
    let output1 = bar.render();

    bar.finish(); // Second call should be no-op
    let output2 = bar.render();

    assert_eq!(output1, output2);
}

#[test]
fn test_progress_bar_update_after_finish() {
    let mut bar = ProgressBar::new(100, None, false, true, true, 20);

    bar.finish();
    bar.update(10); // Should be ignored

    let output = bar.render();
    assert!(output.contains("100%"));
    assert!(output.contains("100/100"));
}

// ============================================================================
// Terminal Size Tests
// ============================================================================

#[test]
fn test_get_terminal_size_returns_valid() {
    let (width, height) = get_terminal_size();

    // Should return positive values
    assert!(width > 0, "Terminal width should be positive");
    assert!(height > 0, "Terminal height should be positive");

    // Should return reasonable values (not absurdly large)
    assert!(width < 10000, "Terminal width should be reasonable");
    assert!(height < 10000, "Terminal height should be reasonable");
}

// ============================================================================
// Color Equality Tests
// ============================================================================

#[test]
fn test_color_equality() {
    assert_eq!(Color::Red, Color::Red);
    assert_ne!(Color::Red, Color::Blue);
    assert_eq!(Color::BrightRed, Color::BrightRed);
    assert_ne!(Color::Red, Color::BrightRed);
}

#[test]
fn test_color_clone() {
    let color = Color::Green;
    let cloned = color;
    assert_eq!(color, cloned);
}

#[test]
fn test_color_debug() {
    let debug_str = format!("{:?}", Color::Red);
    assert_eq!(debug_str, "Red");
}