Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use existing db querying for history list #1589

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions atuin-client/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
53 changes: 13 additions & 40 deletions atuin/src/command/client/history.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
env,
fmt::{self, Display},
io::{self, Write},
time::Duration,
Expand All @@ -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")]
Expand Down Expand Up @@ -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],
ellie marked this conversation as resolved.
Show resolved Hide resolved
};

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

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion atuin/src/command/client/search/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<_>>())
Expand Down
5 changes: 2 additions & 3 deletions atuin/src/command/client/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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);
Expand Down