diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-07-25 16:40:44 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-07-25 16:42:37 +0200 |
| commit | 33e42deaf62e80864066b815654e1ee427f67353 (patch) | |
| tree | ea5f6ee7d77f9b90be31c16d75f025127feb2d86 /crates/cli/src | |
| parent | ca51a35d5a579a3497caf53873db3bb67f815a53 (diff) | |
| download | mihi-33e42deaf62e80864066b815654e1ee427f67353.tar.gz mihi-33e42deaf62e80864066b815654e1ee427f67353.zip | |
words: Move away the selection of a word
This is in preparation to the implementation of the new 'edit' and
'show' subcommands for 'word', which will also need to operate on
exactly one word.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'crates/cli/src')
| -rw-r--r-- | crates/cli/src/words.rs | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/crates/cli/src/words.rs b/crates/cli/src/words.rs index 7ba8dc5..832fa01 100644 --- a/crates/cli/src/words.rs +++ b/crates/cli/src/words.rs @@ -68,6 +68,7 @@ fn help(msg: Option<&str>) { println!("\nSubcommands:"); println!(" create\t\tCreate a new word."); + println!(" edit\t\t\tEdit information from a word."); println!(" ls\t\t\tList the words from the database."); println!(" rm\t\t\tRemove a word from the database."); println!(" show\t\t\tShow information from a word."); @@ -375,7 +376,6 @@ fn ls(mut args: IntoIter<String>) -> i32 { } }; - // TODO: not just the enunciated, but being able to edit for enunciated in words { println!("{enunciated}"); } @@ -383,36 +383,51 @@ fn ls(mut args: IntoIter<String>) -> i32 { 0 } +// Given a search parameter, returns the word that match the enunciate. If +// multiple words match the same search parameter, then the user is asked to +// select one from a list of candidates. +fn select_single_word(search: Option<String>) -> Result<String, String> { + let words = select_enunciated(search)?; + + match words.len() { + 0 => Err("not found".to_string()), + 1 => Ok(words.first().unwrap().to_owned()), + _ => match Select::new("Which word?", words) + .with_page_size(20) + .prompt() + { + Ok(choice) => Ok(choice), + Err(_) => Err("abort!".to_string()), + }, + } +} + +fn edit(mut _args: IntoIter<String>) -> i32 { + 0 +} + +fn show(mut _args: IntoIter<String>) -> i32 { + 0 +} + fn rm(mut args: IntoIter<String>) -> i32 { if args.len() > 1 { help(Some("error: words: too many filters")); return 1; } - let words = match select_enunciated(args.next()) { - Ok(words) => words, + let selection = match select_single_word(args.next()) { + Ok(word) => word, Err(e) => { println!("error: words: {e}"); return 1; } }; - let selection: String = match words.len() { - 0 => { - println!("errors: words: not found!"); - return 1; - } - 1 => words.first().unwrap().to_owned(), - _ => match Select::new("Which word?", words).prompt() { - Ok(choice) => choice, - Err(_) => return 1, - }, - }; - let ans = Confirm::new( format!("Do you really want to remove '{selection}' from the database?").as_str(), ) - .with_default(true) + .with_default(false) .prompt(); match ans { @@ -451,12 +466,18 @@ pub fn run(args: Vec<String>) { "create" => { std::process::exit(create(it)); } + "edit" => { + std::process::exit(edit(it)); + } "ls" => { std::process::exit(ls(it)); } "rm" => { std::process::exit(rm(it)); } + "show" => { + std::process::exit(show(it)); + } _ => { help(Some( format!("error: words: unknown flag or command '{first}'").as_str(), |
