Skip to content

Commit 7867eb0

Browse files
merc1031BinaryMuse
andauthored
fix: Move thorough search through search.filters w/ workspaces (#2703)
Do not just pick first from search.filters, consider all options, filtering based on git-root and workspaces config Fix: #2700 Might Fix: #2536 Wrote a few simple unit tests, and tried it by running the client locally. Not sure about which of "[search] filters" vs "workspaces" should be more respected, so i made it so if workspaces = false, regardless of whats in "[search] filters" workspace gets filtered out. First time looking at atuins code, let me know if this needs any changes. edit: (from my post here: #2536 (comment)) ``` workspaces = true [search] filters = [ "workspace", "directory", "session", "global" ] ``` | `^R` for the ... time| in a git repo | any other dir| | ------------- | ------------- | ------------- | | first | workspace | directory| | second | directory | session| | third| session | global| | fourth | global | directory | | fifth | workspace | session| | sixth | directory | global | ![Image](https://github.com/user-attachments/assets/33150e0a-d219-44d1-8994-b1f98accdfc3) <!-- Thank you for making a PR! Bug fixes are always welcome, but if you're adding a new feature or changing an existing one, we'd really appreciate if you open an issue, post on the forum, or drop in on Discord --> ## Checks - [X] I am happy for maintainers to push small adjustments to this PR, to speed up the review cycle - [X] I have checked that there are no existing pull requests for the same thing - #1655 might be related, but it seems to also implement code that is already merged, and has been open for over a year, hope its ok to suggest this --------- Co-authored-by: Michelle Tilley <[email protected]>
1 parent 2ba93a8 commit 7867eb0

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
lines changed

crates/atuin-client/src/settings.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,10 +735,20 @@ impl Settings {
735735
None
736736
}
737737

738-
pub fn default_filter_mode(&self) -> FilterMode {
738+
pub fn default_filter_mode(&self, git_root: bool) -> FilterMode {
739739
self.filter_mode
740740
.filter(|x| self.search.filters.contains(x))
741-
.or(self.search.filters.first().copied())
741+
.or_else(|| {
742+
self.search
743+
.filters
744+
.iter()
745+
.find(|x| match (x, git_root, self.workspaces) {
746+
(FilterMode::Workspace, true, true) => true,
747+
(FilterMode::Workspace, _, _) => false,
748+
(_, _, _) => true,
749+
})
750+
.copied()
751+
})
742752
.unwrap_or(FilterMode::Global)
743753
}
744754

@@ -979,4 +989,58 @@ mod tests {
979989

980990
Ok(())
981991
}
992+
993+
#[test]
994+
fn can_choose_workspace_filters_when_in_git_context() -> Result<()> {
995+
let mut settings = super::Settings::default();
996+
settings.search.filters = vec![
997+
super::FilterMode::Workspace,
998+
super::FilterMode::Host,
999+
super::FilterMode::Directory,
1000+
super::FilterMode::Session,
1001+
super::FilterMode::Global,
1002+
];
1003+
settings.workspaces = true;
1004+
1005+
assert_eq!(
1006+
settings.default_filter_mode(true),
1007+
super::FilterMode::Workspace,
1008+
);
1009+
1010+
Ok(())
1011+
}
1012+
1013+
#[test]
1014+
fn wont_choose_workspace_filters_when_not_in_git_context() -> Result<()> {
1015+
let mut settings = super::Settings::default();
1016+
settings.search.filters = vec![
1017+
super::FilterMode::Workspace,
1018+
super::FilterMode::Host,
1019+
super::FilterMode::Directory,
1020+
super::FilterMode::Session,
1021+
super::FilterMode::Global,
1022+
];
1023+
settings.workspaces = true;
1024+
1025+
assert_eq!(settings.default_filter_mode(false), super::FilterMode::Host,);
1026+
1027+
Ok(())
1028+
}
1029+
1030+
#[test]
1031+
fn wont_choose_workspace_filters_when_workspaces_disabled() -> Result<()> {
1032+
let mut settings = super::Settings::default();
1033+
settings.search.filters = vec![
1034+
super::FilterMode::Workspace,
1035+
super::FilterMode::Host,
1036+
super::FilterMode::Directory,
1037+
super::FilterMode::Session,
1038+
super::FilterMode::Global,
1039+
];
1040+
settings.workspaces = false;
1041+
1042+
assert_eq!(settings.default_filter_mode(true), super::FilterMode::Host,);
1043+
1044+
Ok(())
1045+
}
9821046
}

crates/atuin/src/command/client/history.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,10 @@ impl Cmd {
514514
(true, true) => [Session, Directory],
515515
(true, false) => [Session, Global],
516516
(false, true) => [Global, Directory],
517-
(false, false) => [settings.default_filter_mode(), Global],
517+
(false, false) => [
518+
settings.default_filter_mode(context.git_root.is_some()),
519+
Global,
520+
],
518521
};
519522

520523
let history = db

crates/atuin/src/command/client/scripts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl Cmd {
230230
let context = atuin_client::database::current_context();
231231

232232
// Get the last N+1 commands, filtering by the default mode
233-
let filters = [settings.default_filter_mode()];
233+
let filters = [settings.default_filter_mode(context.git_root.is_some())];
234234

235235
let mut history = history_db
236236
.list(&filters, &context, Some(count), false, false)

crates/atuin/src/command/client/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ async fn run_non_interactive(
314314
..filter_options
315315
};
316316

317-
let filter_mode = settings.default_filter_mode();
317+
let filter_mode = settings.default_filter_mode(context.git_root.is_some());
318318

319319
let results = db
320320
.search(

crates/atuin/src/command/client/search/interactive.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ use super::{
1818
use atuin_client::{
1919
database::{Database, current_context},
2020
history::{History, HistoryId, HistoryStats, store::HistoryStore},
21-
settings::{
22-
CursorStyle, ExitMode, FilterMode, KeymapMode, PreviewStrategy, SearchMode, Settings,
23-
},
21+
settings::{CursorStyle, ExitMode, KeymapMode, PreviewStrategy, SearchMode, Settings},
2422
};
2523

2624
use crate::command::client::search::history_list::HistoryHighlighter;
@@ -1260,9 +1258,7 @@ pub async fn history(
12601258
filter_mode: settings
12611259
.filter_mode_shell_up_key_binding
12621260
.filter(|_| settings.shell_up_key_binding)
1263-
.or_else(|| Some(settings.default_filter_mode()))
1264-
.filter(|&x| x != FilterMode::Workspace || context.git_root.is_some())
1265-
.unwrap_or(FilterMode::Global),
1261+
.unwrap_or_else(|| settings.default_filter_mode(context.git_root.is_some())),
12661262
context,
12671263
},
12681264
engine: engines::engine(search_mode),

0 commit comments

Comments
 (0)