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
//! Pick command
use super::Command;
use crate::err::Error;
use async_trait::async_trait;
use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand};
/// Abstract pick command
///
/// ```sh
/// leetcode-pick
/// Pick a problem
///
/// USAGE:
/// leetcode pick [OPTIONS] [id]
///
/// FLAGS:
/// -h, --help Prints help information
/// -V, --version Prints version information
///
/// OPTIONS:
/// -q, --query <query> Filter questions by conditions:
/// Uppercase means negative
/// e = easy E = m+h
/// m = medium M = e+h
/// h = hard H = e+m
/// d = done D = not done
/// l = locked L = not locked
/// s = starred S = not starred
///
/// ARGS:
/// <id> Problem id
/// ```
pub struct PickCommand;
static QUERY_HELP: &str = r#"Filter questions by conditions:
Uppercase means negative
e = easy E = m+h
m = medium M = e+h
h = hard H = e+m
d = done D = not done
l = locked L = not locked
s = starred S = not starred"#;
#[async_trait]
impl Command for PickCommand {
/// `pick` usage
fn usage() -> ClapCommand {
ClapCommand::new("pick")
.about("Pick a problem")
.visible_alias("p")
.arg(
Arg::new("name")
.short('n')
.long("name")
.value_parser(clap::value_parser!(String))
.help("Problem name")
.num_args(1),
)
.arg(
Arg::new("id")
.value_parser(clap::value_parser!(i32))
.help("Problem id")
.num_args(1),
)
.arg(
Arg::new("plan")
.short('p')
.long("plan")
.num_args(1)
.help("Invoking python scripts to filter questions"),
)
.arg(
Arg::new("query")
.short('q')
.long("query")
.num_args(1)
.help(QUERY_HELP),
)
.arg(
Arg::new("tag")
.short('t')
.long("tag")
.num_args(1)
.help("Filter questions by tag"),
)
.arg(
Arg::new("daily")
.short('d')
.long("daily")
.help("Pick today's daily challenge")
.action(ArgAction::SetTrue),
)
}
/// `pick` handler
async fn handler(m: &ArgMatches) -> Result<(), Error> {
use crate::cache::Cache;
use rand::Rng;
let cache = Cache::new()?;
let mut problems = cache.get_problems()?;
if problems.is_empty() {
cache.download_problems().await?;
Self::handler(m).await?;
return Ok(());
}
// filtering...
// pym scripts
#[cfg(feature = "pym")]
{
if m.contains_id("plan") {
let ids = crate::pym::exec(m.get_one::<String>("plan").unwrap_or(&"".to_string()))?;
crate::helper::squash(&mut problems, ids)?;
}
}
// tag filter
if m.contains_id("tag") {
let ids = cache
.clone()
.get_tagged_questions(m.get_one::<String>("tag").unwrap_or(&"".to_string()))
.await?;
crate::helper::squash(&mut problems, ids)?;
}
// query filter
if m.contains_id("query") {
let query = m.get_one::<String>("query").ok_or(Error::NoneError)?;
crate::helper::filter(&mut problems, query.to_string());
}
let daily_id = if m.contains_id("daily") {
Some(cache.get_daily_problem_id().await?)
} else {
None
};
let fid = match m.contains_id("name") {
//check for name specified
true => {
match m.get_one::<String>("name").map(|name| name) {
Some(quesname) => match cache.get_problem_id_from_name(quesname) {
Ok(p) => p,
Err(_) => 1,
},
None => {
// Pick random without specify id
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
problem.fid
}
}
}
false => {
m.get_one::<i32>("id")
.copied()
.or(daily_id)
.unwrap_or_else(|| {
// Pick random without specify id
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
problem.fid
})
}
};
let r = cache.get_question(fid).await;
match r {
Ok(q) => println!("{}", q.desc()),
Err(e) => {
eprintln!("{:?}", e);
if let Error::NetworkError(_) = e {
Self::handler(m).await?;
}
}
}
Ok(())
}
}