improve user experience
This commit is contained in:
parent
af55eba6fe
commit
e93aa404ff
1 changed files with 18 additions and 0 deletions
18
src/main.rs
18
src/main.rs
|
@ -11,12 +11,23 @@ macro_rules! JISHO_URL {
|
||||||
const ITEM_LIMIT: usize = 4;
|
const ITEM_LIMIT: usize = 4;
|
||||||
|
|
||||||
fn main() -> Result<(), ureq::Error> {
|
fn main() -> Result<(), ureq::Error> {
|
||||||
|
// Get all parameter into one space separated query
|
||||||
let query = env::args()
|
let query = env::args()
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.map(|i| i.clone())
|
.map(|i| i.clone())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(" ");
|
.join(" ");
|
||||||
|
|
||||||
|
// Check query not being empty
|
||||||
|
if query.is_empty() {
|
||||||
|
println!(
|
||||||
|
"Usage: {} [<Keywords>]",
|
||||||
|
get_exec_name().unwrap_or("jisho-cli".to_owned())
|
||||||
|
);
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
// Do API request
|
// Do API request
|
||||||
let body: Value = ureq::get(&format!(JISHO_URL!(), query))
|
let body: Value = ureq::get(&format!(JISHO_URL!(), query))
|
||||||
.call()?
|
.call()?
|
||||||
|
@ -135,3 +146,10 @@ fn value_to_arr<'a>(value: &'a Value) -> &'a Vec<Value> {
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_exec_name() -> Option<String> {
|
||||||
|
std::env::current_exe()
|
||||||
|
.ok()
|
||||||
|
.and_then(|pb| pb.file_name().map(|s| s.to_os_string()))
|
||||||
|
.and_then(|s| s.into_string().ok())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue