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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Allow many arguments for recursive search function - it needs context for deep traversal
#![allow(clippy::too_many_arguments)]
use crate::tree_node::TreeNodeRef;
use crossbeam_channel::{bounded, unbounded, Receiver, Sender};
use std::collections::HashSet;
use std::path::PathBuf;
use std::thread::{self, JoinHandle};
/// Maximum number of search results to collect (prevents O(n²) dedup stall on broad queries)
const MAX_SEARCH_RESULTS: usize = 500;
/// Maximum messages processed per `poll_results` call to keep the UI responsive
const MAX_POLL_BATCH: usize = 200;
/// Messages from search thread to main thread
#[derive(Debug, Clone)]
pub enum SearchMessage {
/// Found a matching path (path, is_dir, score, match_indices)
Result(PathBuf, bool, Option<i64>, Option<Vec<usize>>),
/// Progress update: number of directories scanned
Progress(usize),
/// Search completed
Done,
}
/// Search result with metadata
#[derive(Debug, Clone)]
pub struct SearchResult {
pub path: PathBuf,
pub is_dir: bool,
pub score: Option<i64>, // Fuzzy match score (None for exact match)
pub match_indices: Option<Vec<usize>>, // Character positions that matched (for highlighting)
}
/// Search functionality for finding files and directories
pub struct Search {
pub mode: bool,
pub query: String,
pub fuzzy_mode: bool, // True if query starts with '/'
pub results: Vec<SearchResult>,
pub selected: usize,
pub show_results: bool,
pub focus_on_results: bool,
// Async search state
pub is_searching: bool,
pub scanned_count: usize,
search_thread: Option<JoinHandle<()>>,
cancel_sender: Option<Sender<()>>,
result_receiver: Option<Receiver<SearchMessage>>,
seen_paths: HashSet<PathBuf>, // O(1) deduplication across Phase 1 + Phase 2
}
impl Default for Search {
fn default() -> Self {
Self::new()
}
}
impl Search {
pub fn new() -> Self {
Self {
mode: false,
query: String::new(),
fuzzy_mode: false,
results: Vec::new(),
selected: 0,
show_results: false,
focus_on_results: false,
is_searching: false,
scanned_count: 0,
search_thread: None,
cancel_sender: None,
result_receiver: None,
seen_paths: HashSet::new(),
}
}
/// Enter search mode
pub fn enter_mode(&mut self) {
self.mode = true;
self.query.clear();
self.fuzzy_mode = false;
}
/// Exit search mode
pub fn exit_mode(&mut self) {
self.mode = false;
self.query.clear();
self.fuzzy_mode = false;
}
/// Add character to query
pub fn add_char(&mut self, c: char) {
self.query.push(c);
self.update_fuzzy_mode();
}
/// Remove last character from query
pub fn backspace(&mut self) {
self.query.pop();
self.update_fuzzy_mode();
}
/// Update fuzzy mode based on query
fn update_fuzzy_mode(&mut self) {
self.fuzzy_mode = self.query.starts_with('/');
}
/// Get actual search query (without leading '/' if in fuzzy mode)
fn get_search_query(&self) -> &str {
if self.fuzzy_mode && self.query.len() > 1 {
&self.query[1..]
} else if self.fuzzy_mode {
"" // Only '/' entered, empty query
} else {
&self.query
}
}
/// Execute two-phase search: quick + deep background scan
pub fn perform_search(
&mut self,
root: &TreeNodeRef,
show_files: bool,
show_hidden: bool,
follow_symlinks: bool,
) {
use fuzzy_matcher::skim::SkimMatcherV2;
// Cancel any existing search
self.cancel_search();
self.results.clear();
self.seen_paths.clear();
self.selected = 0;
self.scanned_count = 0;
let search_query = self.get_search_query();
// Don't search if query is empty (e.g., user entered just '/')
if search_query.is_empty() {
self.show_results = false;
self.is_searching = false;
return;
}
let query_lower = search_query.to_lowercase();
let is_fuzzy = self.fuzzy_mode;
// Create matcher once for Phase 1 (not per-entry)
let matcher = is_fuzzy.then(SkimMatcherV2::default);
// Phase 1: Quick search through already loaded nodes
self.search_loaded_nodes(
root,
&query_lower,
show_files,
show_hidden,
is_fuzzy,
&matcher,
);
// Phase 2: Deep search in background thread
self.spawn_deep_search(
root,
query_lower,
show_files,
show_hidden,
follow_symlinks,
is_fuzzy,
);
self.show_results = true;
self.focus_on_results = true; // Always focus on results after search
self.mode = false;
self.is_searching = true;
}
/// Phase 1: Quick search through already loaded (visible) nodes
fn search_loaded_nodes(
&mut self,
node: &TreeNodeRef,
query: &str,
show_files: bool,
show_hidden: bool,
fuzzy: bool,
matcher: &Option<fuzzy_matcher::skim::SkimMatcherV2>,
) {
use fuzzy_matcher::FuzzyMatcher;
if self.results.len() >= MAX_SEARCH_RESULTS {
return;
}
let node_borrowed = node.borrow();
let name_lower = node_borrowed.name.to_lowercase();
// Skip hidden files/directories
if !show_hidden && node_borrowed.name.starts_with('.') {
return;
}
// Check current node
if show_files || node_borrowed.is_dir {
let path = node_borrowed.path.clone();
if !self.seen_paths.contains(&path) {
let result = if fuzzy {
matcher
.as_ref()
.and_then(|m| m.fuzzy_indices(&name_lower, query))
.map(|(score, indices)| SearchResult {
path: path.clone(),
is_dir: node_borrowed.is_dir,
score: Some(score),
match_indices: Some(indices),
})
} else if name_lower.contains(query) {
Some(SearchResult {
path: path.clone(),
is_dir: node_borrowed.is_dir,
score: None,
match_indices: None,
})
} else {
None
};
if let Some(r) = result {
self.seen_paths.insert(path);
self.results.push(r);
}
}
}
// Recursively search already loaded children
if node_borrowed.is_expanded {
let children = node_borrowed.children.clone();
drop(node_borrowed);
for child in &children {
if self.results.len() >= MAX_SEARCH_RESULTS {
return;
}
self.search_loaded_nodes(child, query, show_files, show_hidden, fuzzy, matcher);
}
}
}
/// Phase 2: Spawn background thread for deep search
fn spawn_deep_search(
&mut self,
root: &TreeNodeRef,
query: String,
show_files: bool,
show_hidden: bool,
follow_symlinks: bool,
fuzzy: bool,
) {
let (result_tx, result_rx) = unbounded();
let (cancel_tx, cancel_rx) = bounded(1);
// Clone root node for thread (Rc can't be sent across threads, so we need path)
let root_path = root.borrow().path.clone();
// Spawn search thread — matcher created once here, not per-entry
let handle = thread::spawn(move || {
use fuzzy_matcher::skim::SkimMatcherV2;
let matcher = fuzzy.then(SkimMatcherV2::default);
Self::deep_search_recursive(
&root_path,
&query,
&result_tx,
&cancel_rx,
show_files,
show_hidden,
follow_symlinks,
fuzzy,
&mut 0,
&matcher,
);
let _ = result_tx.send(SearchMessage::Done);
});
self.search_thread = Some(handle);
self.cancel_sender = Some(cancel_tx);
self.result_receiver = Some(result_rx);
}
/// Recursive deep search in background thread
fn deep_search_recursive(
path: &PathBuf,
query: &str,
result_tx: &Sender<SearchMessage>,
cancel_rx: &Receiver<()>,
show_files: bool,
show_hidden: bool,
follow_symlinks: bool,
fuzzy: bool,
scanned: &mut usize,
matcher: &Option<fuzzy_matcher::skim::SkimMatcherV2>,
) {
use fuzzy_matcher::FuzzyMatcher;
// Check for cancellation
if cancel_rx.try_recv().is_ok() {
return;
}
// Check if entry is a symlink and whether to follow it
if !follow_symlinks {
if let Ok(metadata) = std::fs::symlink_metadata(path) {
if metadata.is_symlink() {
return;
}
}
}
let is_dir = path.is_dir();
if !is_dir && !show_files {
return;
}
// Skip hidden files/directories
if !show_hidden {
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if name.starts_with('.') {
return;
}
}
}
// Check if name matches query
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
let name_lower = name.to_lowercase();
if fuzzy {
if let Some(m) = matcher {
if let Some((score, indices)) = m.fuzzy_indices(&name_lower, query) {
let _ = result_tx.send(SearchMessage::Result(
path.clone(),
is_dir,
Some(score),
Some(indices),
));
}
}
} else if name_lower.contains(query) {
let _ = result_tx.send(SearchMessage::Result(path.clone(), is_dir, None, None));
}
}
// If directory, scan children
if is_dir {
*scanned += 1;
if (*scanned).is_multiple_of(100) {
let _ = result_tx.send(SearchMessage::Progress(*scanned));
}
if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries.flatten() {
if cancel_rx.try_recv().is_ok() {
return;
}
let child_path = entry.path();
Self::deep_search_recursive(
&child_path,
query,
result_tx,
cancel_rx,
show_files,
show_hidden,
follow_symlinks,
fuzzy,
scanned,
matcher,
);
}
}
}
}
/// Poll for new search results from background thread
pub fn poll_results(&mut self) -> bool {
let mut has_updates = false;
let mut search_done = false;
let mut batch = 0;
if let Some(ref rx) = self.result_receiver {
while let Ok(msg) = rx.try_recv() {
batch += 1;
match msg {
SearchMessage::Result(path, is_dir, score, match_indices) => {
// O(1) dedup via HashSet (was O(n) Vec scan)
if !self.seen_paths.contains(&path) {
self.seen_paths.insert(path.clone());
self.results.push(SearchResult {
path,
is_dir,
score,
match_indices,
});
has_updates = true;
if self.results.len() >= MAX_SEARCH_RESULTS {
// Enough results — stop the background thread
if let Some(cancel_tx) = &self.cancel_sender {
let _ = cancel_tx.send(());
}
search_done = true;
break;
}
}
}
SearchMessage::Progress(count) => {
self.scanned_count = count;
has_updates = true;
}
SearchMessage::Done => {
search_done = true;
has_updates = true;
}
}
if batch >= MAX_POLL_BATCH {
// Yield to the event loop; remaining messages picked up next tick
break;
}
}
}
// Clean up if search is done
if search_done {
self.is_searching = false;
self.search_thread = None;
self.cancel_sender = None;
self.result_receiver = None;
// Sort results by score in fuzzy mode (highest score first)
if self.fuzzy_mode {
self.results.sort_by(|a, b| {
match (a.score, b.score) {
(Some(score_a), Some(score_b)) => score_b.cmp(&score_a), // Descending order
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
}
});
}
}
has_updates
}
/// Cancel current search
pub fn cancel_search(&mut self) {
if let Some(cancel_tx) = self.cancel_sender.take() {
let _ = cancel_tx.send(());
}
// Thread checks cancel_rx frequently — detach, no join needed
self.search_thread = None;
self.result_receiver = None;
self.is_searching = false;
self.seen_paths.clear();
}
/// Move selection down in results
pub fn move_down(&mut self) {
if self.selected < self.results.len().saturating_sub(1) {
self.selected += 1;
}
}
/// Move selection up in results
pub fn move_up(&mut self) {
self.selected = self.selected.saturating_sub(1);
}
/// Get number of search results
#[allow(dead_code)]
pub fn get_results_count(&self) -> usize {
self.results.len()
}
/// Set selected index (with bounds checking)
#[allow(dead_code)]
pub fn set_selected(&mut self, index: usize) {
if index < self.results.len() {
self.selected = index;
}
}
/// Get selected result path
pub fn get_selected_result(&self) -> Option<PathBuf> {
self.results.get(self.selected).map(|r| r.path.clone())
}
/// Close search results panel
pub fn close_results(&mut self) {
self.cancel_search();
self.show_results = false;
self.results.clear();
self.selected = 0;
self.focus_on_results = false;
self.scanned_count = 0;
}
/// Toggle focus between tree and search results
pub fn toggle_focus(&mut self) {
if self.show_results {
self.focus_on_results = !self.focus_on_results;
}
}
/// Check if search is active
pub fn is_active(&self) -> bool {
self.is_searching
}
}
impl Drop for Search {
fn drop(&mut self) {
self.cancel_search();
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::{Duration, Instant};
#[test]
fn test_cancel_search_does_not_block() {
// This test ensures that cancel_search() returns quickly
// even if the background thread is still running
let mut search = Search::new();
// Create a temporary directory with some files for testing
let test_dir = std::env::temp_dir().join("dtree_test_cancel");
std::fs::create_dir_all(&test_dir).unwrap();
let root = std::rc::Rc::new(std::cell::RefCell::new(
crate::tree_node::TreeNode::new(test_dir.clone(), 0).unwrap(),
));
// Start a search
search.enter_mode();
search.add_char('t');
search.add_char('e');
search.add_char('s');
search.add_char('t');
search.perform_search(&root, false, false, false);
// Give the background thread time to start
std::thread::sleep(Duration::from_millis(10));
// Now cancel the search and measure how long it takes
let start = Instant::now();
search.cancel_search();
let elapsed = start.elapsed();
// cancel_search() should return almost immediately (< 50ms)
// If it blocks on join(), it would take much longer
assert!(
elapsed < Duration::from_millis(50),
"cancel_search() took too long: {:?}",
elapsed
);
// Clean up
let _ = std::fs::remove_dir_all(&test_dir);
}
#[test]
fn test_repeated_search_does_not_hang() {
// This test simulates the bug scenario: starting a new search
// while another search is already running
let mut search = Search::new();
let test_dir = std::env::temp_dir().join("dtree_test_repeated");
std::fs::create_dir_all(&test_dir).unwrap();
let root = std::rc::Rc::new(std::cell::RefCell::new(
crate::tree_node::TreeNode::new(test_dir.clone(), 0).unwrap(),
));
// Start first search
search.enter_mode();
search.add_char('a');
search.perform_search(&root, false, false, false);
// Give it a moment to start
std::thread::sleep(Duration::from_millis(10));
// Start second search immediately (this should not hang)
let start = Instant::now();
search.enter_mode();
search.add_char('b');
search.perform_search(&root, false, false, false);
let elapsed = start.elapsed();
// The second search should start quickly without blocking
assert!(
elapsed < Duration::from_millis(100),
"Second search took too long: {:?}",
elapsed
);
// Start third search (stress test)
search.enter_mode();
search.add_char('c');
search.perform_search(&root, false, false, false);
// Clean up
search.cancel_search();
let _ = std::fs::remove_dir_all(&test_dir);
}
#[test]
fn test_rapid_search_start_stop() {
// Stress test: rapidly start and stop searches
let mut search = Search::new();
let test_dir = std::env::temp_dir().join("dtree_test_rapid");
std::fs::create_dir_all(&test_dir).unwrap();
let root = std::rc::Rc::new(std::cell::RefCell::new(
crate::tree_node::TreeNode::new(test_dir.clone(), 0).unwrap(),
));
let start = Instant::now();
// Rapidly start and cancel 10 searches
for i in 0..10 {
search.enter_mode();
search.add_char('a');
search.add_char((b'0' + (i % 10) as u8) as char);
search.perform_search(&root, false, false, false);
std::thread::sleep(Duration::from_millis(5));
}
let elapsed = start.elapsed();
// All 10 search starts should complete in < 1 second
// (each should take ~5ms + some overhead)
assert!(
elapsed < Duration::from_secs(1),
"Rapid searches took too long: {:?}",
elapsed
);
// Clean up
search.cancel_search();
let _ = std::fs::remove_dir_all(&test_dir);
}
}