improve user experience

This commit is contained in:
jojii 2021-02-07 23:13:17 +01:00
parent af55eba6fe
commit e93aa404ff

View file

@ -11,12 +11,23 @@ macro_rules! JISHO_URL {
const ITEM_LIMIT: usize = 4;
fn main() -> Result<(), ureq::Error> {
// Get all parameter into one space separated query
let query = env::args()
.skip(1)
.map(|i| i.clone())
.collect::<Vec<String>>()
.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
let body: Value = ureq::get(&format!(JISHO_URL!(), query))
.call()?
@ -135,3 +146,10 @@ fn value_to_arr<'a>(value: &'a Value) -> &'a Vec<Value> {
_ => 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())
}