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
//! Cache managger
use super::Command;
use crate::{cache::Cache, helper::Digit, Error};
use async_trait::async_trait;
use clap::{App, Arg, ArgMatches, SubCommand};
use colored::Colorize;

/// Abstract `data` command
///
/// ```sh
/// leetcode-data
/// Manage Cache
///
/// USAGE:
///     leetcode data [FLAGS]
///
/// FLAGS:
///     -d, --delete     Delete cache
///     -u, --update     Update cache
///     -h, --help       Prints help information
///     -V, --version    Prints version information
/// ```
pub struct DataCommand;

#[async_trait]
impl Command for DataCommand {
    /// `data` command usage
    fn usage<'a, 'cache>() -> App<'a, 'cache> {
        SubCommand::with_name("data")
            .about("Manage Cache")
            .visible_alias("d")
            .arg(
                Arg::with_name("delete")
                    .display_order(1)
                    .short("d")
                    .long("delete")
                    .help("Delete cache"),
            )
            .arg(
                Arg::with_name("update")
                    .display_order(2)
                    .short("u")
                    .long("update")
                    .help("Update cache"),
            )
    }

    /// `data` handler
    async fn handler(m: &ArgMatches<'_>) -> Result<(), Error> {
        use std::fs::File;
        use std::path::Path;

        let cache = Cache::new()?;
        let path = cache.0.conf.storage.cache()?;
        let f = File::open(&path)?;
        let len = format!("{}K", f.metadata()?.len() / 1000);

        let out = format!(
            "  {}{}",
            Path::new(&path)
                .file_name().ok_or(Error::NoneError)?
                .to_string_lossy()
                .to_string()
                .digit(65 - (len.len() as i32))
                .bright_green(),
            len
        );

        let mut title = "\n  Cache".digit(63);
        title.push_str("Size");
        title.push_str("\n  ");
        title.push_str(&"-".repeat(65));

        let mut flags = 0;
        if m.is_present("delete") {
            flags += 1;
            cache.clean()?;
            println!("{}", "ok!".bright_green());
        }

        if m.is_present("update") {
            flags += 1;
            cache.update().await?;
            println!("{}", "ok!".bright_green());
        }

        if flags == 0 {
            println!("{}", title.bright_black());
            println!("{}\n", out);
        }

        Ok(())
    }
}