leetcode_cli/cmd/
stat.rs

1//! status command
2use clap::Args;
3use colored::Colorize;
4
5/// Stat command arguments
6#[derive(Args)]
7pub struct StatArgs {}
8
9impl StatArgs {
10    /// `stat` handler
11    pub async fn run(&self) -> Result<(), crate::err::Error> {
12        use crate::{Cache, helper::Digit};
13
14        let cache = Cache::new()?;
15        let res = cache.get_problems()?;
16
17        let mut easy: f64 = 0.00;
18        let mut easy_ac: f64 = 0.00;
19        let mut medium: f64 = 0.00;
20        let mut medium_ac: f64 = 0.00;
21        let mut hard: f64 = 0.00;
22        let mut hard_ac: f64 = 0.00;
23
24        for i in res.into_iter() {
25            match i.level {
26                1 => {
27                    easy += 1.00;
28                    if i.status == "ac" {
29                        easy_ac += 1.00;
30                    }
31                }
32                2 => {
33                    medium += 1.00;
34                    if i.status == "ac" {
35                        medium_ac += 1.00;
36                    }
37                }
38                3 => {
39                    hard += 1.00;
40                    if i.status == "ac" {
41                        hard_ac += 1.00;
42                    }
43                }
44                _ => {}
45            }
46        }
47
48        // level: len = 8
49        // count: len = 10
50        // percent: len = 16
51        // chart: len = 32
52        // title
53        println!(
54            "\n{}",
55            "  Level      Count     Percent                                Chart".bright_black()
56        );
57        println!(
58            "{}",
59            "  -----------------------------------------------------------------".bright_black()
60        );
61
62        // lines
63        for (i, l) in [(easy, easy_ac), (medium, medium_ac), (hard, hard_ac)]
64            .iter()
65            .enumerate()
66        {
67            match i {
68                0 => {
69                    print!("  {}", "Easy".bright_green());
70                    print!("{}", " ".digit(4));
71                }
72                1 => {
73                    print!("  {}", "Medium".bright_yellow());
74                    print!("{}", " ".digit(2));
75                }
76                2 => {
77                    print!("  {}", "Hard".bright_red());
78                    print!("{}", " ".digit(4));
79                }
80                _ => continue,
81            }
82
83            let checked_div = |lhs: f64, rhs: f64| if rhs == 0. { 0. } else { lhs / rhs };
84            let count = format!("{}/{}", l.1, l.0);
85            let pct = format!("( {:.2} %)", checked_div(100.0 * l.1, l.0));
86            let mut line = "".to_string();
87            line.push_str(&" ".digit(10 - (count.len() as i32)));
88            line.push_str(&count);
89            line.push_str(&" ".digit(12 - (pct.len() as i32)));
90            line.push_str(&pct);
91            print!("{}", line);
92            print!("     ");
93
94            let done = "░"
95                .repeat(checked_div(32.00 * l.1, l.0) as usize)
96                .bright_green();
97            let udone = "░"
98                .repeat(32 - checked_div(32.00 * l.1, l.0) as usize)
99                .red();
100            print!("{}", done);
101            println!("{}", udone);
102        }
103        println!();
104        Ok(())
105    }
106}