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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiffSide {
Left,
Right,
}
/// SearchOrigin is the pane index where the search was initiated.
pub type SearchOrigin = usize;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SearchMatch {
/// Matches in a list-based pane (file tree, branch list, commit log, reflog, etc.)
ListEntry(usize),
/// Matches in the diff view
DiffLine {
row: usize,
col_start: usize,
col_end: usize,
side: DiffSide,
},
/// Matches in a plain text pane (the Files preview): a char range on
/// one displayed line.
TextLine {
row: usize,
col_start: usize,
col_end: usize,
},
}
#[derive(Debug, Clone)]
pub struct SearchState {
pub active: bool,
pub input: String,
pub query: Option<String>,
pub origin: SearchOrigin,
pub matches: Vec<SearchMatch>,
pub current_match_idx: Option<usize>,
/// Last confirmed query — preserved across clear() for n/N reuse
pub last_query: Option<String>,
/// Search history (oldest first)
pub history: Vec<String>,
/// Current position in history during input (None = editing new input)
pub(crate) history_idx: Option<usize>,
/// Saved input before browsing history
saved_input: String,
}
impl SearchState {
pub fn new() -> Self {
Self {
active: false,
input: String::new(),
query: None,
origin: 0,
matches: Vec::new(),
current_match_idx: None,
last_query: None,
history: Vec::new(),
history_idx: None,
saved_input: String::new(),
}
}
pub fn start(&mut self, origin: SearchOrigin) {
self.active = true;
self.input.clear();
self.query = None;
self.origin = origin;
self.matches.clear();
self.current_match_idx = None;
self.history_idx = None;
self.saved_input.clear();
}
pub fn reset_matches(&mut self) {
self.matches.clear();
self.current_match_idx = None;
}
/// Clear highlights but preserve last_query and history for n/N reuse
pub fn clear(&mut self) {
self.active = false;
self.input.clear();
if self.query.is_some() {
self.last_query = self.query.take();
}
self.matches.clear();
self.current_match_idx = None;
}
/// Handle a key event during search input mode.
/// Returns `true` if the user confirmed a search query (Enter), signaling
/// the caller to execute the search and jump to the first match.
pub fn handle_input_key(&mut self, key: KeyEvent) -> bool {
match key.code {
KeyCode::Enter => {
let query = self.input.clone();
if query.is_empty() {
self.active = false;
return false;
}
self.push_history(&query);
self.active = false;
self.query = Some(query);
true
}
KeyCode::Esc => {
self.active = false;
self.input.clear();
false
}
KeyCode::Backspace => {
self.input.pop();
self.history_idx = None;
false
}
KeyCode::Up | KeyCode::Char('p')
if key.code == KeyCode::Up || key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.history_prev();
false
}
KeyCode::Down | KeyCode::Char('n')
if key.code == KeyCode::Down || key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.history_next();
false
}
KeyCode::Char(c) => {
self.input.push(c);
self.history_idx = None;
false
}
_ => false,
}
}
/// Navigate to previous history entry
pub fn history_prev(&mut self) {
if self.history.is_empty() {
return;
}
match self.history_idx {
None => {
// Save current input, jump to most recent history
self.saved_input = self.input.clone();
let idx = self.history.len() - 1;
self.history_idx = Some(idx);
self.input = self.history[idx].clone();
}
Some(idx) if idx > 0 => {
let new_idx = idx - 1;
self.history_idx = Some(new_idx);
self.input = self.history[new_idx].clone();
}
_ => {}
}
}
/// Navigate to next history entry (or back to saved input)
pub fn history_next(&mut self) {
if let Some(idx) = self.history_idx {
if idx + 1 < self.history.len() {
let new_idx = idx + 1;
self.history_idx = Some(new_idx);
self.input = self.history[new_idx].clone();
} else {
// Back to the input the user was typing
self.history_idx = None;
self.input = self.saved_input.clone();
}
}
}
/// Add query to history (deduplicates consecutive)
pub fn push_history(&mut self, query: &str) {
if query.is_empty() {
return;
}
if self.history.last().map(|s| s.as_str()) != Some(query) {
self.history.push(query.to_string());
}
}
}