From 7867eb0e785ecf7241e2e62f67b09880b42fbb0c Mon Sep 17 00:00:00 2001 From: Leonidas Loucas <311356+merc1031@users.noreply.github.com> Date: Mon, 15 Dec 2025 10:21:02 -0800 Subject: 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: atuinsh/atuin#2700 Might Fix: atuinsh/atuin#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: https://github.com/atuinsh/atuin/issues/2536#issuecomment-2808053862) ``` 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) ## 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 --- crates/atuin-client/src/settings.rs | 68 +++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'crates/atuin-client/src') diff --git a/crates/atuin-client/src/settings.rs b/crates/atuin-client/src/settings.rs index 489c1a83..87ed9383 100644 --- a/crates/atuin-client/src/settings.rs +++ b/crates/atuin-client/src/settings.rs @@ -735,10 +735,20 @@ impl Settings { None } - pub fn default_filter_mode(&self) -> FilterMode { + pub fn default_filter_mode(&self, git_root: bool) -> FilterMode { self.filter_mode .filter(|x| self.search.filters.contains(x)) - .or(self.search.filters.first().copied()) + .or_else(|| { + self.search + .filters + .iter() + .find(|x| match (x, git_root, self.workspaces) { + (FilterMode::Workspace, true, true) => true, + (FilterMode::Workspace, _, _) => false, + (_, _, _) => true, + }) + .copied() + }) .unwrap_or(FilterMode::Global) } @@ -979,4 +989,58 @@ mod tests { Ok(()) } + + #[test] + fn can_choose_workspace_filters_when_in_git_context() -> Result<()> { + let mut settings = super::Settings::default(); + settings.search.filters = vec![ + super::FilterMode::Workspace, + super::FilterMode::Host, + super::FilterMode::Directory, + super::FilterMode::Session, + super::FilterMode::Global, + ]; + settings.workspaces = true; + + assert_eq!( + settings.default_filter_mode(true), + super::FilterMode::Workspace, + ); + + Ok(()) + } + + #[test] + fn wont_choose_workspace_filters_when_not_in_git_context() -> Result<()> { + let mut settings = super::Settings::default(); + settings.search.filters = vec![ + super::FilterMode::Workspace, + super::FilterMode::Host, + super::FilterMode::Directory, + super::FilterMode::Session, + super::FilterMode::Global, + ]; + settings.workspaces = true; + + assert_eq!(settings.default_filter_mode(false), super::FilterMode::Host,); + + Ok(()) + } + + #[test] + fn wont_choose_workspace_filters_when_workspaces_disabled() -> Result<()> { + let mut settings = super::Settings::default(); + settings.search.filters = vec![ + super::FilterMode::Workspace, + super::FilterMode::Host, + super::FilterMode::Directory, + super::FilterMode::Session, + super::FilterMode::Global, + ]; + settings.workspaces = false; + + assert_eq!(settings.default_filter_mode(true), super::FilterMode::Host,); + + Ok(()) + } } -- cgit v1.3.1