Skip to content

Support picking question via name #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ scripts = 'scripts'
leetcode pick 1
```

```sh
leetcode pick --name "Two Sum"
```

```sh
[1] Two Sum is on the run...

Expand Down
13 changes: 13 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ impl Cache {
Ok(p)
}

/// Get problem from name
pub fn get_problem_id_from_name(&self, problem_name: &String) -> Result<i32, Error> {
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<i32, Error> {
parser::daily(
Expand Down
43 changes: 34 additions & 9 deletions src/cmds/pick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -127,15 +135,32 @@ impl Command for PickCommand {
None
};

let fid = 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 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;

Expand Down