diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-19 21:49:45 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-19 22:01:40 +0100 |
| commit | 22f19cf9f50ae4ce96b175366602b9a2816332a6 (patch) | |
| tree | 0aa85a1e876425222ebb37764a81d1c0529199eb | |
| parent | e1e7a94d0c781fa3c8a3c82a74c671ee1e92af92 (diff) | |
| download | mihi-22f19cf9f50ae4ce96b175366602b9a2816332a6.tar.gz mihi-22f19cf9f50ae4ce96b175366602b9a2816332a6.zip | |
words: add the poke subcommand
This subcommand allows the user to update the `updated_at`
timestamp, which is quite relevant as this timestamp is used when
selecting words.
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
| -rw-r--r-- | crates/cli/src/words.rs | 27 | ||||
| -rw-r--r-- | lib/mihi/src/lib.rs | 18 |
2 files changed, 45 insertions, 0 deletions
diff --git a/crates/cli/src/words.rs b/crates/cli/src/words.rs index 413d193..db8280c 100644 --- a/crates/cli/src/words.rs +++ b/crates/cli/src/words.rs @@ -74,6 +74,7 @@ fn help(msg: Option<&str>) { 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!(" poke\t\t\tUpdate the timestamp for a word."); println!(" rm\t\t\tRemove a word from the database."); println!(" show\t\t\tShow information from a word."); } @@ -682,6 +683,29 @@ fn show_info(word: Word) -> Result<(), String> { Ok(()) } +fn poke(mut args: IntoIter<String>) -> i32 { + if args.len() > 1 { + help(Some( + "error: words: only one argument. If it's an enunciate, wrap it in double quotes", + )); + return 1; + } + + let enunciated = match select_single_word(args.next()) { + Ok(word) => word, + Err(e) => { + println!("error: words: {e}."); + return 1; + } + }; + + if mihi::update_timestamp(enunciated.as_str()).is_ok() { + 0 + } else { + 1 + } +} + fn show(mut args: IntoIter<String>) -> i32 { if args.len() > 1 { help(Some( @@ -776,6 +800,9 @@ pub fn run(args: Vec<String>) { "ls" => { std::process::exit(ls(it)); } + "poke" => { + std::process::exit(poke(it)); + } "rm" => { std::process::exit(rm(it)); } diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs index d75965d..0c44335 100644 --- a/lib/mihi/src/lib.rs +++ b/lib/mihi/src/lib.rs @@ -646,6 +646,24 @@ pub fn update_word(word: Word) -> Result<(), String> { } } +/// Update the `updated_at` timestamp for any word matching the given +/// `enunciated` string. In theory the given enunciated should identify only a +/// single word, but nothing forbids the caller from updating every word which +/// somehow matches the given string. +pub fn update_timestamp(enunciated: &str) -> Result<(), String> { + let conn = get_connection()?; + + match conn.execute( + "UPDATE words \ + SET updated_at = datetime('now') \ + WHERE enunciated = ?1", + params![enunciated], + ) { + Ok(_) => Ok(()), + Err(e) => Err(format!("could not update '{}': {}", enunciated, e)), + } +} + pub fn select_enunciated(filter: Option<String>) -> Result<Vec<String>, String> { let conn = get_connection()?; |
