From d8aacb4a806c097fb316305dab8c481546d72b8b Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Sat, 13 Feb 2021 22:06:27 +0000 Subject: Add fuzzy history search and distinct arg --- src/local/database.rs | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'src/local') diff --git a/src/local/database.rs b/src/local/database.rs index e9882fd7..2b014bc0 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -11,7 +11,7 @@ pub trait Database { fn save(&mut self, h: History) -> Result<()>; fn save_bulk(&mut self, h: &Vec) -> Result<()>; fn load(&self, id: &str) -> Result; - fn list(&self) -> Result<()>; + fn list(&self, distinct: bool) -> Result<()>; fn update(&self, h: History) -> Result<()>; } @@ -149,33 +149,29 @@ impl Database for SqliteDatabase { Ok(()) } - fn list(&self) -> Result<()> { + fn list(&self, distinct: bool) -> Result<()> { debug!("listing history"); - let mut stmt = self.conn.prepare( - "SELECT id, timestamp, duration, exit, command, cwd, session, hostname FROM history", - )?; + let mut stmt = match distinct { + false => self + .conn + .prepare("SELECT command FROM history order by timestamp asc")?, + + true => self + .conn + .prepare("SELECT distinct command FROM history order by timestamp asc")?, + }; let history_iter = stmt.query_map(params![], |row| { - Ok(History { - id: row.get(0)?, - timestamp: row.get(1)?, - duration: row.get(2)?, - exit: row.get(3)?, - command: row.get(4)?, - cwd: row.get(5)?, - session: row.get(6)?, - hostname: row.get(7)?, - }) + let command: String = row.get(0)?; + + Ok(command) })?; for h in history_iter { let h = h.unwrap(); - println!( - "{} | {} | {} | {} | {} | {} | {}", - h.timestamp, h.hostname, h.session, h.cwd, h.duration, h.exit, h.command - ); + println!("{}", h); } Ok(()) -- cgit v1.3.1