diff --git a/README.md b/README.md index fe8e0dd..4e68afc 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,10 @@ scripts = 'scripts' leetcode pick 1 ``` +```sh +leetcode pick --name "Two Sum" +``` + ```sh [1] Two Sum is on the run... diff --git a/src/cache/mod.rs b/src/cache/mod.rs index b15f677..03e138e 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -122,6 +122,19 @@ impl Cache { Ok(p) } + /// Get problem from name + pub fn get_problem_id_from_name(&self, problem_name: &String) -> Result { + let p: Problem = problems + .filter(name.eq(problem_name)) + .first(&mut self.conn()?)?; + if p.category != "algorithms" { + return Err(Error::FeatureError( + "Not support database and shell questions for now".to_string(), + )); + } + Ok(p.fid) + } + /// Get daily problem pub async fn get_daily_problem_id(&self) -> Result { parser::daily( diff --git a/src/cmds/pick.rs b/src/cmds/pick.rs index a814143..967e205 100644 --- a/src/cmds/pick.rs +++ b/src/cmds/pick.rs @@ -47,6 +47,14 @@ impl Command for PickCommand { 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)) @@ -127,15 +135,32 @@ impl Command for PickCommand { None }; - let fid = m - .get_one::("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 fid = match m.contains_id("name") { + //check for name specified + true => { + match m.get_one::("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::("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;