diff --git a/src/command/history.rs b/src/command/history.rs index 73be66f..5959fc5 100644 --- a/src/command/history.rs +++ b/src/command/history.rs @@ -34,6 +34,12 @@ pub enum Cmd { }, } +fn print_list(h: &Vec) { + for i in h { + println!("{}", i.command); + } +} + impl Cmd { pub fn run(&self, db: &mut Sqlite) -> Result<()> { match self { @@ -68,7 +74,12 @@ impl Cmd { Ok(()) } - Self::List { distinct } => db.list(*distinct), + Self::List { .. } => { + let history = db.list()?; + print_list(&history); + + Ok(()) + } } } } diff --git a/src/local/database.rs b/src/local/database.rs index 4f99d8a..43b17e5 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -1,3 +1,4 @@ +use chrono::Utc; use std::path::Path; use eyre::{eyre, Result}; @@ -11,7 +12,8 @@ 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, distinct: bool) -> Result<()>; + fn list(&self) -> Result>; + fn since(&self, date: chrono::DateTime) -> Result>; fn update(&self, h: History) -> Result<()>; } @@ -150,29 +152,49 @@ impl Database for Sqlite { Ok(()) } - fn list(&self, distinct: bool) -> Result<()> { + fn list(&self) -> Result> { debug!("listing history"); - let mut stmt = if distinct { - self.conn - .prepare("SELECT command FROM history order by timestamp asc")? - } else { - self.conn - .prepare("SELECT distinct command FROM history order by timestamp asc")? - }; + let mut stmt = self + .conn + .prepare("SELECT * FROM history order by timestamp asc")?; let history_iter = stmt.query_map(params![], |row| { - let command: String = row.get(0)?; - - Ok(command) + 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)?, + }) })?; - for h in history_iter { - let h = h.unwrap(); + Ok(history_iter.filter_map(|x| x.ok()).collect()) + } - println!("{}", h); - } + fn since(&self, date: chrono::DateTime) -> Result> { + debug!("listing history"); - Ok(()) + let mut stmt = self.conn.prepare( + "SELECT distinct command FROM history where timestamp > ?1 order by timestamp asc", + )?; + + let history_iter = stmt.query_map(params![date.timestamp_nanos()], |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)?, + }) + })?; + + Ok(history_iter.filter_map(|x| x.ok()).collect()) } }