1use std::time::Instant;
2
3use clap::Parser;
4
5use crate::{loading_bar::LoadingBar, on_key_press, basic_counters::BasicCounter};
6
7#[derive(Parser)]
8#[command(author, about, version)]
9pub struct Cli {
10 #[arg(short, long, help="A description to display with the counter")]
11 description: Option<String>,
12 #[arg(short, long, help="Whether you would like to display a loading bar")]
13 loading_bar: bool,
14 #[arg(short='y', long, help="Whether you would like to display a tally score")]
15 tally: bool,
16 #[arg(short, long, help="The total / value to count towards")]
17 total: Option<u32>,
18 #[arg(short, long, help="The amount to increment the loading bar by (defaults to \x1b[33m1\x1b[0m)")]
19 increment: Option<u32>,
20 #[arg(short='k', long="key", help="The key to indicate an increment (defaults to '\x1b[33mi\x1b[0m')")]
21 increment_key: Option<char>,
22}
23
24impl Cli {
25 pub fn run(self) {
26 let increment = if let Some(increment) = self.increment { increment } else { 1 };
27 let key = if let Some(key) = self.increment_key { key } else { 'i' };
28 let description = if let Some(description) = self.description { description } else { "loading".into() };
29
30 if self.loading_bar {
31 if self.tally { println!("\x1b[31;1merror: \x1b[0mloading bar cannot have tally mark display"); std::process::exit(1) };
32 let total = if let Some(total) = self.total { total } else { println!("\x1b[31;1merror: \x1b[0mloading bar must have a total / value to count towards"); std::process::exit(1) };
33
34 let mut loading_bar = LoadingBar::new(total);
35 let mut time = Instant::now();
36 loading_bar.draw(&description);
37
38 on_key_press(key, || {
39 if loading_bar.update(time.elapsed().as_secs_f32(), increment)
40 .draw(&description) { return true };
41 time = Instant::now();
42 false
43 });
44 } else {
45 let mut basic_counter = BasicCounter::new(self.total);
46 let mut time = Instant::now();
47 if self.tally { basic_counter.draw_tally(&description) } else { basic_counter.draw_data(&description) };
48
49 on_key_press(key, || {
50 if basic_counter.update(time.elapsed().as_secs_f32(), increment) {
51 if self.tally { basic_counter.draw_tally(&description) } else { basic_counter.draw_data(&description) };
52 return true
53 }; if self.tally { basic_counter.draw_tally(&description) } else { basic_counter.draw_data(&description) };
54 time = Instant::now();
55 false
56 });
57 }
58 }
59}