From 10f465da8ff113819d435b0f8e5066783c5100af Mon Sep 17 00:00:00 2001 From: Peter Holloway Date: Fri, 19 Jan 2024 11:21:05 +0000 Subject: [PATCH] fix: Use existing db querying for history list (#1589) When printing the history list with either the session or cwd filter enabled, use to same query method as without either to ensure that the other options (hide deleted entries etc) are respected. --- atuin-client/src/database.rs | 20 ++++---- atuin/src/command/client/history.rs | 53 ++++++---------------- atuin/src/command/client/search/engines.rs | 2 +- atuin/src/command/client/stats.rs | 5 +- 4 files changed, 27 insertions(+), 53 deletions(-) diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index a6957093..05ff559a 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -78,7 +78,7 @@ pub trait Database: Send + Sync + 'static { async fn load(&self, id: &str) -> Result>; async fn list( &self, - filter: FilterMode, + filters: &[FilterMode], context: &Context, max: Option, unique: bool, @@ -271,7 +271,7 @@ impl Database for Sqlite { // make a unique list, that only shows the *newest* version of things async fn list( &self, - filter: FilterMode, + filters: &[FilterMode], context: &Context, max: Option, unique: bool, @@ -291,13 +291,15 @@ impl Database for Sqlite { context.cwd.clone() }; - match filter { - FilterMode::Global => &mut query, - FilterMode::Host => query.and_where_eq("hostname", quote(&context.hostname)), - FilterMode::Session => query.and_where_eq("session", quote(&context.session)), - FilterMode::Directory => query.and_where_eq("cwd", quote(&context.cwd)), - FilterMode::Workspace => query.and_where_like_left("cwd", git_root), - }; + for filter in filters { + match filter { + FilterMode::Global => &mut query, + FilterMode::Host => query.and_where_eq("hostname", quote(&context.hostname)), + FilterMode::Session => query.and_where_eq("session", quote(&context.session)), + FilterMode::Directory => query.and_where_eq("cwd", quote(&context.cwd)), + FilterMode::Workspace => query.and_where_like_left("cwd", &git_root), + }; + } if unique { query.and_where_eq( diff --git a/atuin/src/command/client/history.rs b/atuin/src/command/client/history.rs index 4178180c..7d4689f9 100644 --- a/atuin/src/command/client/history.rs +++ b/atuin/src/command/client/history.rs @@ -1,5 +1,4 @@ use std::{ - env, fmt::{self, Display}, io::{self, Write}, time::Duration, @@ -15,7 +14,10 @@ use atuin_client::{ encryption, history::{store::HistoryStore, History}, record::{self, sqlite_store::SqliteStore}, - settings::Settings, + settings::{ + FilterMode::{Directory, Global, Session}, + Settings, + }, }; #[cfg(feature = "sync")] @@ -349,37 +351,16 @@ impl Cmd { print0: bool, reverse: bool, ) -> Result<()> { - let session = if session { - Some(env::var("ATUIN_SESSION")?) - } else { - None - }; - let cwd = if cwd { - Some(utils::get_current_dir()) - } else { - None + let filters = match (session, cwd) { + (true, true) => [Session, Directory], + (true, false) => [Session, Global], + (false, true) => [Global, Directory], + (false, false) => [settings.filter_mode, Global], }; - let history = match (session, cwd) { - (None, None) => { - db.list(settings.filter_mode, &context, None, false, include_deleted) - .await? - } - (None, Some(cwd)) => { - let query = format!("select * from history where cwd = '{cwd}';"); - db.query_history(&query).await? - } - (Some(session), None) => { - let query = format!("select * from history where session = '{session}';"); - db.query_history(&query).await? - } - (Some(session), Some(cwd)) => { - let query = format!( - "select * from history where cwd = '{cwd}' and session = '{session}';", - ); - db.query_history(&query).await? - } - }; + let history = db + .list(&filters, &context, None, false, include_deleted) + .await?; print_list(&history, mode, format.as_deref(), print0, reverse); @@ -393,15 +374,7 @@ impl Cmd { ) -> Result<()> { println!("Importing all history.db data into records.db"); - let history = db - .list( - atuin_client::settings::FilterMode::Global, - &context, - None, - false, - true, - ) - .await?; + let history = db.list(&[], &context, None, false, true).await?; for i in history { println!("loaded {}", i.id); diff --git a/atuin/src/command/client/search/engines.rs b/atuin/src/command/client/search/engines.rs index ad84b6d7..105ce147 100644 --- a/atuin/src/command/client/search/engines.rs +++ b/atuin/src/command/client/search/engines.rs @@ -35,7 +35,7 @@ pub trait SearchEngine: Send + Sync + 'static { async fn query(&mut self, state: &SearchState, db: &mut dyn Database) -> Result> { if state.input.as_str().is_empty() { Ok(db - .list(state.filter_mode, &state.context, Some(200), true, false) + .list(&[state.filter_mode], &state.context, Some(200), true, false) .await? .into_iter() .collect::>()) diff --git a/atuin/src/command/client/stats.rs b/atuin/src/command/client/stats.rs index e990b70b..969ab1ae 100644 --- a/atuin/src/command/client/stats.rs +++ b/atuin/src/command/client/stats.rs @@ -9,7 +9,7 @@ use interim::parse_date_string; use atuin_client::{ database::{current_context, Database}, history::History, - settings::{FilterMode, Settings}, + settings::Settings, }; use time::{Duration, OffsetDateTime, Time}; @@ -92,8 +92,7 @@ impl Cmd { let last_night = now.replace_time(Time::MIDNIGHT); let history = if words.as_str() == "all" { - db.list(FilterMode::Global, &context, None, false, false) - .await? + db.list(&[], &context, None, false, false).await? } else if words.trim() == "today" { let start = last_night; let end = start + Duration::days(1);