From ba086f909d6c09c0d1c3636452c82a79c44468ff Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Mon, 15 Feb 2021 21:30:13 +0000 Subject: Allow listing by current directory and/or session --- src/command/history.rs | 32 +++++++++++++++++++++++++++----- src/local/database.rs | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/command/history.rs b/src/command/history.rs index 5d2a8050..09dd4364 100644 --- a/src/command/history.rs +++ b/src/command/history.rs @@ -3,7 +3,7 @@ use std::env; use eyre::Result; use structopt::StructOpt; -use crate::local::database::Database; +use crate::local::database::{Database, QueryParam}; use crate::local::history::History; #[derive(StructOpt)] @@ -29,8 +29,11 @@ pub enum Cmd { aliases=&["l", "li", "lis"], )] List { - #[structopt(long)] - distinct: bool, + #[structopt(long, short)] + dir: bool, + + #[structopt(long, short)] + session: bool, }, } @@ -74,8 +77,27 @@ impl Cmd { Ok(()) } - Self::List { .. } => { - let history = db.list()?; + Self::List { session, dir, .. } => { + const QUERY_SESSION: &str = "select * from history where session = ?;"; + const QUERY_DIR: &str = "select * from history where cwd = ?;"; + const QUERY_SESSION_DIR: &str = + "select * from history where cwd = ?1 and session = ?2;"; + + let params = (session, dir); + + let cwd = env::current_dir()?.display().to_string(); + let session = env::var("ATUIN_SESSION")?; + + let history = match params { + (false, false) => db.list()?, + (true, false) => db.query(QUERY_SESSION, &[QueryParam::Text(session)])?, + (false, true) => db.query(QUERY_DIR, &[QueryParam::Text(cwd)])?, + (true, true) => db.query( + QUERY_SESSION_DIR, + &[QueryParam::Text(cwd), QueryParam::Text(session)], + )?, + }; + print_list(&history); Ok(()) diff --git a/src/local/database.rs b/src/local/database.rs index 5b98bb36..3133578b 100644 --- a/src/local/database.rs +++ b/src/local/database.rs @@ -8,6 +8,10 @@ use rusqlite::{Transaction, NO_PARAMS}; use super::history::History; +pub enum QueryParam { + Text(String), +} + pub trait Database { fn save(&mut self, h: &History) -> Result<()>; fn save_bulk(&mut self, h: &[History]) -> Result<()>; @@ -16,6 +20,7 @@ pub trait Database { fn range(&self, from: chrono::DateTime, to: chrono::DateTime) -> Result>; fn update(&self, h: &History) -> Result<()>; + fn query(&self, query: &str, params: &[QueryParam]) -> Result>; } // Intended for use on a developer machine and not a sync server. @@ -95,6 +100,16 @@ impl Sqlite { } } +impl rusqlite::ToSql for QueryParam { + fn to_sql(&self) -> Result, rusqlite::Error> { + use rusqlite::types::{ToSqlOutput, Value}; + + match self { + QueryParam::Text(s) => Ok(ToSqlOutput::Owned(Value::Text(s.clone()))), + } + } +} + impl Database for Sqlite { fn save(&mut self, h: &History) -> Result<()> { debug!("saving history to sqlite"); @@ -176,6 +191,14 @@ impl Database for Sqlite { Ok(history_iter.filter_map(Result::ok).collect()) } + + fn query(&self, query: &str, params: &[QueryParam]) -> Result> { + let mut stmt = self.conn.prepare(query)?; + + let history_iter = stmt.query_map(params, |row| history_from_sqlite_row(None, row))?; + + Ok(history_iter.filter_map(Result::ok).collect()) + } } fn history_from_sqlite_row( -- cgit v1.3.1