From 716c7722cda29bf612508bb96f51822a86e0f69e Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Sat, 20 Mar 2021 00:50:31 +0000 Subject: Add TUI, resolve #19, #17, #16 (#21) --- src/local/database.rs | 22 +++++++++++++++++++++- src/local/history.rs | 21 ++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) (limited to 'src/local') diff --git a/src/local/database.rs b/src/local/database.rs index 0c31566d..cba7142c 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -15,12 +15,17 @@ pub enum QueryParam { pub trait Database { fn save(&mut self, h: &History) -> Result<()>; fn save_bulk(&mut self, h: &[History]) -> Result<()>; + fn load(&self, id: &str) -> Result; fn list(&self) -> Result>; fn range(&self, from: chrono::DateTime, to: chrono::DateTime) -> Result>; - fn update(&self, h: &History) -> Result<()>; + fn query(&self, query: &str, params: &[QueryParam]) -> Result>; + fn update(&self, h: &History) -> Result<()>; + fn history_count(&self) -> Result; + + fn prefix_search(&self, query: &str) -> Result>; } // Intended for use on a developer machine and not a sync server. @@ -199,6 +204,21 @@ impl Database for Sqlite { Ok(history_iter.filter_map(Result::ok).collect()) } + + fn prefix_search(&self, query: &str) -> Result> { + self.query( + "select * from history where command like ?1 || '%' order by timestamp asc", + &[QueryParam::Text(query.to_string())], + ) + } + + fn history_count(&self) -> Result { + let res: i64 = + self.conn + .query_row_and_then("select count(1) from history;", params![], |row| row.get(0))?; + + Ok(res) + } } fn history_from_sqlite_row( diff --git a/src/local/history.rs b/src/local/history.rs index 05600b80..0ca112bd 100644 --- a/src/local/history.rs +++ b/src/local/history.rs @@ -1,8 +1,9 @@ use std::env; +use std::hash::{Hash, Hasher}; use crate::command::uuid_v4; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct History { pub id: String, pub timestamp: i64, @@ -42,3 +43,21 @@ impl History { } } } + +impl PartialEq for History { + // for the sakes of listing unique history only, we do not care about + // anything else + // obviously this does not refer to the *same* item of history, but when + // we only render the command, it looks the same + fn eq(&self, other: &Self) -> bool { + self.command == other.command + } +} + +impl Eq for History {} + +impl Hash for History { + fn hash(&self, state: &mut H) { + self.command.hash(state); + } +} -- cgit v1.3.1