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 list(
&self,
filter: FilterMode,
filters: &[FilterMode],
context: &Context,
max: Option<usize>,
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<usize>,
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(

View File

@ -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);

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>> {
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::<Vec<_>>())

View File

@ -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);