aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2021-02-15 21:30:13 +0000
committerEllie Huxtable <e@elm.sh>2021-02-15 21:30:19 +0000
commitba086f909d6c09c0d1c3636452c82a79c44468ff (patch)
treed156eeebdcd26226d32664d35c8aa15a3ce66805 /src
parentChange description (diff)
downloadatuin-ba086f909d6c09c0d1c3636452c82a79c44468ff.zip
Allow listing by current directory and/or session
Diffstat (limited to 'src')
-rw-r--r--src/command/history.rs32
-rw-r--r--src/local/database.rs23
2 files changed, 50 insertions, 5 deletions
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<Utc>, to: chrono::DateTime<Utc>)
-> Result<Vec<History>>;
fn update(&self, h: &History) -> Result<()>;
+ fn query(&self, query: &str, params: &[QueryParam]) -> Result<Vec<History>>;
}
// 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::types::ToSqlOutput<'_>, 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<Vec<History>> {
+ 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(