1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
use crate::atuin_client::{
database::{ClientSqlite, Context, OptFilters},
history::{History, HistoryId},
settings::{FilterMode, SearchMode, Settings},
};
use async_trait::async_trait;
use eyre::Result;
use super::cursor::Cursor;
#[cfg(feature = "daemon")]
pub(crate) mod daemon;
pub(crate) mod db;
pub(crate) mod skim;
#[expect(unused)] // settings is only used if daemon feature is enabled
pub(crate) fn engine(search_mode: SearchMode, settings: &Settings) -> Box<dyn SearchEngine> {
match search_mode {
SearchMode::Skim => Box::new(skim::Search::new()) as Box<_>,
#[cfg(feature = "daemon")]
SearchMode::DaemonFuzzy => Box::new(daemon::Search::new(settings)) as Box<_>,
#[cfg(not(feature = "daemon"))]
SearchMode::DaemonFuzzy => {
// Fall back to fuzzy mode if daemon feature is not enabled
Box::new(db::Search(SearchMode::Fuzzy)) as Box<_>
}
mode => Box::new(db::Search(mode)) as Box<_>,
}
}
pub(crate) struct SearchState {
pub(crate) input: Cursor,
pub(crate) filter_mode: FilterMode,
pub(crate) context: Context,
pub(crate) custom_context: Option<HistoryId>,
}
impl SearchState {
pub(crate) fn rotate_filter_mode(&mut self, settings: &Settings, offset: isize) {
let mut i = settings
.search
.filters
.iter()
.position(|&m| m == self.filter_mode)
.unwrap_or_default();
for _ in 0..settings.search.filters.len() {
i = (i.wrapping_add_signed(offset)) % settings.search.filters.len();
let mode = settings.search.filters[i];
if self.filter_mode_available(mode, settings) {
self.filter_mode = mode;
break;
}
}
}
fn filter_mode_available(&self, mode: FilterMode, settings: &Settings) -> bool {
match mode {
FilterMode::Global | FilterMode::SessionPreload => self.custom_context.is_none(),
FilterMode::Workspace => settings.workspaces && self.context.git_root.is_some(),
_ => true,
}
}
}
#[async_trait]
pub(crate) trait SearchEngine: Send + Sync + 'static {
async fn full_query(
&mut self,
state: &SearchState,
db: &mut ClientSqlite,
) -> Result<Vec<History>>;
async fn query(&mut self, state: &SearchState, db: &mut ClientSqlite) -> Result<Vec<History>> {
if state.input.as_str().is_empty() {
Ok(db
.search(
SearchMode::FullText,
state.filter_mode,
&state.context,
"",
OptFilters {
limit: Some(200),
..Default::default()
},
)
.await?
.into_iter()
.collect::<Vec<_>>())
} else {
self.full_query(state, db).await
}
}
fn get_highlight_indices(&self, command: &str, search_input: &str) -> Vec<usize>;
}
|