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.
This commit is contained in:
Peter Holloway 2024-01-19 11:21:05 +00:00 committed by GitHub
parent 62f81a8c91
commit 10f465da8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 53 deletions

View File

@ -78,7 +78,7 @@ pub trait Database: Send + Sync + 'static {
async fn load(&self, id: &str) -> Result<Option<History>>; async fn load(&self, id: &str) -> Result<Option<History>>;
async fn list( async fn list(
&self, &self,
filter: FilterMode, filters: &[FilterMode],
context: &Context, context: &Context,
max: Option<usize>, max: Option<usize>,
unique: bool, unique: bool,
@ -271,7 +271,7 @@ impl Database for Sqlite {
// make a unique list, that only shows the *newest* version of things // make a unique list, that only shows the *newest* version of things
async fn list( async fn list(
&self, &self,
filter: FilterMode, filters: &[FilterMode],
context: &Context, context: &Context,
max: Option<usize>, max: Option<usize>,
unique: bool, unique: bool,
@ -291,13 +291,15 @@ impl Database for Sqlite {
context.cwd.clone() context.cwd.clone()
}; };
for filter in filters {
match filter { match filter {
FilterMode::Global => &mut query, FilterMode::Global => &mut query,
FilterMode::Host => query.and_where_eq("hostname", quote(&context.hostname)), FilterMode::Host => query.and_where_eq("hostname", quote(&context.hostname)),
FilterMode::Session => query.and_where_eq("session", quote(&context.session)), FilterMode::Session => query.and_where_eq("session", quote(&context.session)),
FilterMode::Directory => query.and_where_eq("cwd", quote(&context.cwd)), FilterMode::Directory => query.and_where_eq("cwd", quote(&context.cwd)),
FilterMode::Workspace => query.and_where_like_left("cwd", git_root), FilterMode::Workspace => query.and_where_like_left("cwd", &git_root),
}; };
}
if unique { if unique {
query.and_where_eq( query.and_where_eq(

View File

@ -1,5 +1,4 @@
use std::{ use std::{
env,
fmt::{self, Display}, fmt::{self, Display},
io::{self, Write}, io::{self, Write},
time::Duration, time::Duration,
@ -15,7 +14,10 @@ use atuin_client::{
encryption, encryption,
history::{store::HistoryStore, History}, history::{store::HistoryStore, History},
record::{self, sqlite_store::SqliteStore}, record::{self, sqlite_store::SqliteStore},
settings::Settings, settings::{
FilterMode::{Directory, Global, Session},
Settings,
},
}; };
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
@ -349,37 +351,16 @@ impl Cmd {
print0: bool, print0: bool,
reverse: bool, reverse: bool,
) -> Result<()> { ) -> Result<()> {
let session = if session { let filters = match (session, cwd) {
Some(env::var("ATUIN_SESSION")?) (true, true) => [Session, Directory],
} else { (true, false) => [Session, Global],
None (false, true) => [Global, Directory],
}; (false, false) => [settings.filter_mode, Global],
let cwd = if cwd {
Some(utils::get_current_dir())
} else {
None
}; };
let history = match (session, cwd) { let history = db
(None, None) => { .list(&filters, &context, None, false, include_deleted)
db.list(settings.filter_mode, &context, None, false, include_deleted) .await?;
.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?
}
};
print_list(&history, mode, format.as_deref(), print0, reverse); print_list(&history, mode, format.as_deref(), print0, reverse);
@ -393,15 +374,7 @@ impl Cmd {
) -> Result<()> { ) -> Result<()> {
println!("Importing all history.db data into records.db"); println!("Importing all history.db data into records.db");
let history = db let history = db.list(&[], &context, None, false, true).await?;
.list(
atuin_client::settings::FilterMode::Global,
&context,
None,
false,
true,
)
.await?;
for i in history { for i in history {
println!("loaded {}", i.id); println!("loaded {}", i.id);

View File

@ -35,7 +35,7 @@ pub trait SearchEngine: Send + Sync + 'static {
async fn query(&mut self, state: &SearchState, db: &mut dyn Database) -> Result<Vec<History>> { async fn query(&mut self, state: &SearchState, db: &mut dyn Database) -> Result<Vec<History>> {
if state.input.as_str().is_empty() { if state.input.as_str().is_empty() {
Ok(db Ok(db
.list(state.filter_mode, &state.context, Some(200), true, false) .list(&[state.filter_mode], &state.context, Some(200), true, false)
.await? .await?
.into_iter() .into_iter()
.collect::<Vec<_>>()) .collect::<Vec<_>>())

View File

@ -9,7 +9,7 @@ use interim::parse_date_string;
use atuin_client::{ use atuin_client::{
database::{current_context, Database}, database::{current_context, Database},
history::History, history::History,
settings::{FilterMode, Settings}, settings::Settings,
}; };
use time::{Duration, OffsetDateTime, Time}; use time::{Duration, OffsetDateTime, Time};
@ -92,8 +92,7 @@ impl Cmd {
let last_night = now.replace_time(Time::MIDNIGHT); let last_night = now.replace_time(Time::MIDNIGHT);
let history = if words.as_str() == "all" { let history = if words.as_str() == "all" {
db.list(FilterMode::Global, &context, None, false, false) db.list(&[], &context, None, false, false).await?
.await?
} else if words.trim() == "today" { } else if words.trim() == "today" {
let start = last_night; let start = last_night;
let end = start + Duration::days(1); let end = start + Duration::days(1);