leetcode_cli/cmd/
data.rs

1//! Cache manager
2use crate::{Error, cache::Cache, helper::Digit};
3use clap::Args;
4use colored::Colorize;
5
6/// Data command arguments
7#[derive(Args)]
8pub struct DataArgs {
9    /// Delete cache
10    #[arg(short, long)]
11    pub delete: bool,
12
13    /// Update cache
14    #[arg(short, long)]
15    pub update: bool,
16}
17
18impl DataArgs {
19    /// `data` handler
20    pub async fn run(&self) -> Result<(), Error> {
21        use std::fs::File;
22        use std::path::Path;
23
24        let cache = Cache::new()?;
25        let path = cache.0.conf.storage.cache()?;
26        let f = File::open(&path)?;
27        let len = format!("{}K", f.metadata()?.len() / 1000);
28
29        let out = format!(
30            "  {}{}",
31            Path::new(&path)
32                .file_name()
33                .ok_or(Error::NoneError)?
34                .to_string_lossy()
35                .to_string()
36                .digit(65 - (len.len() as i32))
37                .bright_green(),
38            len
39        );
40
41        let mut title = "\n  Cache".digit(63);
42        title.push_str("Size");
43        title.push_str("\n  ");
44        title.push_str(&"-".repeat(65));
45
46        let mut flags = 0;
47        if self.delete {
48            flags += 1;
49            cache.clean()?;
50            println!("{}", "ok!".bright_green());
51        }
52
53        if self.update {
54            flags += 1;
55            cache.update().await?;
56            println!("{}", "ok!".bright_green());
57        }
58
59        if flags == 0 {
60            println!("{}", title.bright_black());
61            println!("{}\n", out);
62        }
63
64        Ok(())
65    }
66}