Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d26cb4e

Browse files
Support picking question via name (clearloop#109)
* Fix rebasing conflicts * Include README example to pick by name * Only return question ID when searching by name * Clean up ID selecting code * fix: use get_problem_id_from_name() * fix: use mutable reference instead when filtering
1 parent 24e611e commit d26cb4e

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ scripts = 'scripts'
7575
leetcode pick 1
7676
```
7777

78+
```sh
79+
leetcode pick --name "Two Sum"
80+
```
81+
7882
```sh
7983
[1] Two Sum is on the run...
8084

‎src/cache/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ impl Cache {
122122
Ok(p)
123123
}
124124

125+
/// Get problem from name
126+
pub fn get_problem_id_from_name(&self, problem_name: &String) -> Result<i32, Error> {
127+
let p: Problem = problems
128+
.filter(name.eq(problem_name))
129+
.first(&mut self.conn()?)?;
130+
if p.category != "algorithms" {
131+
return Err(Error::FeatureError(
132+
"Not support database and shell questions for now".to_string(),
133+
));
134+
}
135+
Ok(p.fid)
136+
}
137+
125138
/// Get daily problem
126139
pub async fn get_daily_problem_id(&self) -> Result<i32, Error> {
127140
parser::daily(

‎src/cmds/pick.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ impl Command for PickCommand {
4747
ClapCommand::new("pick")
4848
.about("Pick a problem")
4949
.visible_alias("p")
50+
.arg(
51+
Arg::new("name")
52+
.short('n')
53+
.long("name")
54+
.value_parser(clap::value_parser!(String))
55+
.help("Problem name")
56+
.num_args(1),
57+
)
5058
.arg(
5159
Arg::new("id")
5260
.value_parser(clap::value_parser!(i32))
@@ -127,15 +135,32 @@ impl Command for PickCommand {
127135
None
128136
};
129137

130-
let fid = m
131-
.get_one::<i32>("id")
132-
.copied()
133-
.or(daily_id)
134-
.unwrap_or_else(|| {
135-
// Pick random without specify id
136-
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
137-
problem.fid
138-
});
138+
let fid = match m.contains_id("name") {
139+
//check for name specified
140+
true => {
141+
match m.get_one::<String>("name").map(|name| name) {
142+
Some(quesname) => match cache.get_problem_id_from_name(quesname) {
143+
Ok(p) => p,
144+
Err(_) => 1,
145+
},
146+
None => {
147+
// Pick random without specify id
148+
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
149+
problem.fid
150+
}
151+
}
152+
}
153+
false => {
154+
m.get_one::<i32>("id")
155+
.copied()
156+
.or(daily_id)
157+
.unwrap_or_else(|| {
158+
// Pick random without specify id
159+
let problem = &problems[rand::thread_rng().gen_range(0..problems.len())];
160+
problem.fid
161+
})
162+
}
163+
};
139164

140165
let r = cache.get_question(fid).await;
141166

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /