click-rs 1.0.2

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
//! Terminal UI demo - Rust port of Python Click's termui.py example.
//!
//! This script showcases different terminal UI helpers in click-rs.
//!
//! Run with: cargo run --example termui -- <subcommand>
//! Available subcommands: colordemo, pager, progress, open, locate, edit, clear, pause, menu

use std::thread;
use std::time::Duration;

use click::launch;
use click::{
    clear, echo, edit_text, getchar, pause, style, Argument, ClickOption, Color, Command,
    CommandLike, Group, ProgressBar,
};

fn main() {
    let cli = Group::new("termui")
        .help("This script showcases different terminal UI helpers in click-rs.")
        .command(colordemo_cmd())
        .command(pager_cmd())
        .command(progress_cmd())
        .command(open_cmd())
        .command(locate_cmd())
        .command(edit_cmd())
        .command(clear_cmd())
        .command(pause_cmd())
        .command(menu_cmd())
        .build();

    let args: Vec<String> = std::env::args().skip(1).collect();
    if let Err(e) = CommandLike::main(&cli, args) {
        let code = e.exit_code();
        if code == 0 {
            // Help was requested - already printed by make_context flow
            // For groups we need to print help manually since Group::main doesn't do it
            let ctx = click::ContextBuilder::new().info_name("termui").build();
            println!("{}", cli.get_help(&ctx));
        } else {
            eprintln!("{}", e.format_full());
        }
        std::process::exit(code);
    }
}

/// Demonstrates ANSI color support.
fn colordemo_cmd() -> Command {
    Command::new("colordemo")
        .help("Demonstrates ANSI color support.")
        .callback(|_ctx| {
            for color in ["red", "green", "blue"] {
                let fg_color = match color {
                    "red" => Color::Red,
                    "green" => Color::Green,
                    "blue" => Color::Blue,
                    _ => unreachable!(),
                };

                // Foreground colored
                let styled_fg = style(
                    &format!("I am colored {}", color),
                    Some(fg_color),
                    None,
                    false,
                    false,
                    false,
                    false,
                    false,
                    false,
                    false,
                    true,
                );
                echo(&styled_fg, true, false, None);

                // Background colored
                let styled_bg = style(
                    &format!("I am background colored {}", color),
                    None,
                    Some(fg_color),
                    false,
                    false,
                    false,
                    false,
                    false,
                    false,
                    false,
                    true,
                );
                echo(&styled_bg, true, false, None);
            }
            Ok(())
        })
        .build()
}

/// Demonstrates using the pager.
fn pager_cmd() -> Command {
    Command::new("pager")
        .help("Demonstrates using the pager.")
        .callback(|_ctx| {
            let mut lines = Vec::new();
            for x in 0..200 {
                let num_styled = style(
                    &x.to_string(),
                    Some(Color::Green),
                    None,
                    false,
                    false,
                    false,
                    false,
                    false,
                    false,
                    false,
                    true,
                );
                lines.push(format!("{}. Hello World!", num_styled));
            }
            click::termui::echo_via_pager(&lines.join("\n"), None);
            Ok(())
        })
        .build()
}

/// Demonstrates the progress bar.
fn progress_cmd() -> Command {
    Command::new("progress")
        .help("Demonstrates the progress bar.")
        .option(
            ClickOption::new(&["--count"])
                .default("8000")
                .help("The number of items to process.")
                .build(),
        )
        .callback(|ctx| {
            let count_str = ctx
                .get_param::<String>("count")
                .map(|s| s.as_str())
                .unwrap_or("8000");
            let count: usize = count_str.parse().unwrap_or(8000);
            let count = count.clamp(1, 100000);

            // Simulated slow processing
            fn process_slowly() {
                let delay_ms = (rand_simple() * 2.0) as u64;
                thread::sleep(Duration::from_millis(delay_ms));
            }

            // Simple pseudo-random number generator (0.0 to 1.0)
            fn rand_simple() -> f64 {
                use std::time::SystemTime;
                let nanos = SystemTime::now()
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .unwrap()
                    .subsec_nanos();
                (nanos % 1000) as f64 / 1000.0
            }

            // Progress bar 1: Processing accounts with custom characters
            {
                echo(
                    &format!("Processing {} accounts...", count),
                    true,
                    false,
                    None,
                );
                let mut bar =
                    ProgressBar::new(count, Some("Processing accounts"), true, true, true, 30)
                        .fill_char('â–ˆ')
                        .empty_char('â–‘');
                for _ in 0..count {
                    process_slowly();
                    bar.update(1);
                }
                bar.finish();
            }

            // Progress bar 2: Committing transaction (with item display)
            {
                let filtered_count = (count as f64 * 0.7) as usize; // ~70% pass filter
                echo(
                    &format!("\nCommitting ~{} transactions...", filtered_count),
                    true,
                    false,
                    None,
                );
                let mut bar = ProgressBar::new(
                    filtered_count,
                    Some("Committing transaction"),
                    true,
                    true,
                    true,
                    30,
                );
                let mut processed = 0;
                for _i in 0..count {
                    if rand_simple() > 0.3 {
                        process_slowly();
                        processed += 1;
                        bar.update(1);
                        if processed >= filtered_count {
                            break;
                        }
                    }
                }
                bar.finish();
            }

            // Progress bar 3: Counting with custom characters
            {
                echo(
                    &format!("\nCounting {} items with custom characters...", count),
                    true,
                    false,
                    None,
                );
                let mut bar = ProgressBar::new(count, Some("Counting"), true, true, true, 30)
                    .fill_char('=')
                    .empty_char(' ');
                for _ in 0..count {
                    process_slowly();
                    bar.update(1);
                }
                bar.finish();
            }

            // Progress bar 4: Minimal (no percent, no ETA)
            {
                echo(
                    &format!("\nMinimal progress bar ({} items)...", count),
                    true,
                    false,
                    None,
                );
                let mut bar = ProgressBar::new(count, None, false, false, false, 30);
                for _ in 0..count {
                    process_slowly();
                    bar.update(1);
                }
                bar.finish();
            }

            // Progress bar 5: Non-linear progress
            {
                let steps: Vec<f64> = (0..20)
                    .map(|x| (x as f64 * 1.0 / 20.0).exp() - 1.0)
                    .collect();
                let total: f64 = steps.iter().sum();
                let total_count = total as usize;

                echo(
                    &format!("\nSlowing progress bar ({} steps)...", total_count),
                    true,
                    false,
                    None,
                );
                let mut bar = ProgressBar::new(
                    total_count,
                    Some("Slowing progress bar"),
                    true,
                    false,
                    true,
                    30,
                );

                for step in steps {
                    let sleep_ms = (step * 1000.0) as u64;
                    thread::sleep(Duration::from_millis(sleep_ms));
                    bar.update(step as usize);
                }
                bar.finish();
            }

            Ok(())
        })
        .build()
}

/// Opens a file or URL in the default application.
fn open_cmd() -> Command {
    Command::new("open")
        .help("Opens a file or URL in the default application.")
        .argument(
            Argument::new("url")
                .help("URL or file path to open")
                .build(),
        )
        .callback(|ctx| {
            let url = ctx
                .get_param::<String>("url")
                .ok_or_else(|| click::ClickError::usage("URL argument is required"))?;
            launch(url, false, false)?;
            Ok(())
        })
        .build()
}

/// Opens a file location in the file manager.
fn locate_cmd() -> Command {
    Command::new("locate")
        .help("Opens the file location in a file manager.")
        .argument(Argument::new("url").help("File path to locate").build())
        .callback(|ctx| {
            let url = ctx
                .get_param::<String>("url")
                .ok_or_else(|| click::ClickError::usage("URL argument is required"))?;
            launch(url, false, true)?;
            Ok(())
        })
        .build()
}

/// Opens an editor with some text in it.
fn edit_cmd() -> Command {
    Command::new("edit")
        .help("Opens an editor with some text in it.")
        .callback(|_ctx| {
            let marker = "# Everything below is ignored\n";
            let initial_text = format!("\n\n{}", marker);

            match edit_text(Some(&initial_text), None, "txt", true)? {
                Some(message) => {
                    // Split at marker and take content before it
                    let msg = message
                        .split(marker)
                        .next()
                        .unwrap_or("")
                        .trim_end_matches('\n');

                    if msg.is_empty() {
                        echo("Empty message!", true, false, None);
                    } else {
                        echo(&format!("Message:\n{}", msg), true, false, None);
                    }
                }
                None => {
                    echo("You did not enter anything!", true, false, None);
                }
            }
            Ok(())
        })
        .build()
}

/// Clears the entire screen.
fn clear_cmd() -> Command {
    Command::new("clear")
        .help("Clears the entire screen.")
        .callback(|_ctx| {
            clear();
            Ok(())
        })
        .build()
}

/// Waits for the user to press a button.
fn pause_cmd() -> Command {
    Command::new("pause")
        .help("Waits for the user to press a button.")
        .callback(|_ctx| {
            pause(None);
            Ok(())
        })
        .build()
}

/// Shows a simple menu.
fn menu_cmd() -> Command {
    Command::new("menu")
        .help("Shows a simple menu.")
        .callback(|_ctx| {
            let mut menu = "main";

            loop {
                match menu {
                    "main" => {
                        echo("Main menu:", true, false, None);
                        echo("  d: debug menu", true, false, None);
                        echo("  q: quit", true, false, None);

                        let c = getchar(false)?;
                        match c {
                            'd' => menu = "debug",
                            'q' => menu = "quit",
                            _ => echo("Invalid input", true, false, None),
                        }
                    }
                    "debug" => {
                        echo("Debug menu", true, false, None);
                        echo("  b: back", true, false, None);

                        let c = getchar(false)?;
                        if c == 'b' {
                            menu = "main";
                        } else {
                            echo("Invalid input", true, false, None);
                        }
                    }
                    "quit" => return Ok(()),
                    _ => unreachable!(),
                }
            }
        })
        .build()
}