From e93aa404ff640585915cfc4314574f4023e353fe Mon Sep 17 00:00:00 2001 From: jojii Date: Sun, 7 Feb 2021 23:13:17 +0100 Subject: [PATCH] improve user experience --- src/main.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main.rs b/src/main.rs index 4659f14..d03ea42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::>() .join(" "); + // Check query not being empty + if query.is_empty() { + println!( + "Usage: {} []", + 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 { _ => unreachable!(), } } + +fn get_exec_name() -> Option { + std::env::current_exe() + .ok() + .and_then(|pb| pb.file_name().map(|s| s.to_os_string())) + .and_then(|s| s.into_string().ok()) +}