bmrk 0.3.0

A fast TUI for directory navigation and bookmark management
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
// Allow many arguments for event handler functions
#![allow(clippy::too_many_arguments)]

use anyhow::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
use std::path::PathBuf;
use std::time::{Duration, Instant};

use crate::bookmarks::Bookmarks;
use crate::config::Config;
use crate::disks::Disks;
use crate::navigation::Navigation;
use crate::search::Search;
use crate::ui::UI;

/// Event handler for keyboard and mouse input
pub struct EventHandler {
    pub last_click_time: Option<(Instant, usize)>,
}

impl Default for EventHandler {
    fn default() -> Self {
        Self::new()
    }
}

impl EventHandler {
    pub fn new() -> Self {
        Self {
            last_click_time: None,
        }
    }

    /// Handle keyboard events
    pub fn handle_key(
        &mut self,
        key: KeyEvent,
        nav: &mut Navigation,
        search: &mut Search,
        bookmarks: &mut Bookmarks,
        disks: &mut Disks,
        _ui: &UI,
        config: &Config,
    ) -> Result<Option<PathBuf>> {
        // Search input mode
        if search.mode {
            return self.handle_search_input(key, search, nav, config);
        }

        // Disk selection mode
        if disks.is_selecting {
            match key.code {
                _ if config.keybindings.is_exit(key.code) => {
                    self.last_click_time = None;
                    disks.exit_selection_mode();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Enter => {
                    if let Some(disk) = disks.get_selected() {
                        let path = disk.mount_point.clone();
                        disks.exit_selection_mode();
                        let _ = nav.go_to_directory(path, false);
                    } else {
                        disks.exit_selection_mode();
                    }
                    return Ok(Some(PathBuf::new()));
                }
                _ if config.keybindings.is_quit(key.code) => {
                    if let Some(disk) = disks.get_selected() {
                        return Ok(Some(disk.mount_point.clone()));
                    }
                    return Ok(None);
                }
                KeyCode::Char('j') | KeyCode::Down => {
                    disks.move_down();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Char('k') | KeyCode::Up => {
                    disks.move_up();
                    return Ok(Some(PathBuf::new()));
                }
                _ => return Ok(Some(PathBuf::new())),
            }
        }

        // Bookmark selection mode
        if bookmarks.is_selecting {
            match key.code {
                _ if config.keybindings.is_exit(key.code) => {
                    self.last_click_time = None;
                    bookmarks.exit_selection_mode();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Tab => {
                    bookmarks.toggle_filter_mode();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Enter => {
                    if let Some(bookmark) = bookmarks.get_selected_bookmark() {
                        let path = bookmark.path.clone();
                        bookmarks.exit_selection_mode();
                        let _ = nav.go_to_directory(path, false);
                    } else {
                        bookmarks.exit_selection_mode();
                    }
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Char('j') | KeyCode::Down if !bookmarks.filter_mode => {
                    bookmarks.move_down();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Char('k') | KeyCode::Up if !bookmarks.filter_mode => {
                    bookmarks.move_up();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Char('d') if !bookmarks.filter_mode => {
                    if let Err(e) = bookmarks.handle_deletion_key() {
                        bookmarks.bookmark_error = Some(e.to_string());
                    }
                    return Ok(Some(PathBuf::new()));
                }
                _ if config.keybindings.is_quit(key.code) && !bookmarks.filter_mode => {
                    if let Some(bookmark) = bookmarks.get_selected_bookmark() {
                        let path = bookmark.path.clone();
                        bookmarks.exit_selection_mode();
                        return Ok(Some(path));
                    }
                    bookmarks.exit_selection_mode();
                    return Ok(None);
                }
                KeyCode::Char(c) if bookmarks.filter_mode => {
                    bookmarks.add_char(c);
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Backspace if bookmarks.filter_mode => {
                    bookmarks.backspace();
                    return Ok(Some(PathBuf::new()));
                }
                _ => return Ok(Some(PathBuf::new())),
            }
        }

        // Bookmark creation mode
        if bookmarks.is_creating {
            if key.modifiers.contains(KeyModifiers::CONTROL) {
                match key.code {
                    KeyCode::Char('j') | KeyCode::Char('J') | KeyCode::Down => {
                        nav.center_selection = true;
                        nav.move_down();
                        return Ok(Some(PathBuf::new()));
                    }
                    KeyCode::Char('k') | KeyCode::Char('K') | KeyCode::Up => {
                        nav.center_selection = true;
                        nav.move_up();
                        return Ok(Some(PathBuf::new()));
                    }
                    _ => {}
                }
            }

            match key.code {
                _ if config.keybindings.is_exit(key.code) => {
                    bookmarks.exit_creation_mode();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Enter => {
                    let bookmark_name = bookmarks.get_input().to_string();
                    if !bookmark_name.is_empty() {
                        if let Some(node) = nav.get_selected_node() {
                            let node_borrowed = node.borrow();
                            let path = if node_borrowed.is_dir {
                                node_borrowed.path.clone()
                            } else {
                                node_borrowed
                                    .path
                                    .parent()
                                    .map(|p| p.to_path_buf())
                                    .unwrap_or_else(|| node_borrowed.path.clone())
                            };
                            let dir_name = path
                                .file_name()
                                .and_then(|n| n.to_str())
                                .map(|s| s.to_string());
                            drop(node_borrowed);
                            if let Err(e) = bookmarks.add(bookmark_name, path, dir_name) {
                                bookmarks.bookmark_error = Some(e.to_string());
                                return Ok(Some(PathBuf::new()));
                            }
                        }
                    }
                    bookmarks.exit_creation_mode();
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Char(c) if c != ' ' => {
                    bookmarks.add_char(c);
                    return Ok(Some(PathBuf::new()));
                }
                KeyCode::Backspace => {
                    bookmarks.backspace();
                    return Ok(Some(PathBuf::new()));
                }
                _ => return Ok(Some(PathBuf::new())),
            }
        }

        // exit — cancel search / quit without output
        if config.keybindings.is_exit(key.code) {
            if search.is_active() {
                search.cancel_search();
                return Ok(Some(PathBuf::new()));
            } else if search.show_results {
                search.close_results();
                return Ok(Some(PathBuf::new()));
            } else {
                return Ok(None);
            }
        }

        // quit — exit with selected search result when focused on results
        if config.keybindings.is_quit(key.code) && search.focus_on_results && search.show_results {
            if let Some(result) = search.results.get(search.selected) {
                let path = result.path.clone();
                if result.is_dir {
                    return Ok(Some(path));
                } else if let Some(parent) = path.parent() {
                    return Ok(Some(parent.to_path_buf()));
                }
            }
            return Ok(None);
        }

        // quit — exit and output path of selected directory for shell
        if config.keybindings.is_quit(key.code) {
            if let Some(node) = nav.get_selected_node() {
                let node_borrowed = node.borrow();
                if node_borrowed.is_dir {
                    return Ok(Some(node_borrowed.path.clone()));
                } else if let Some(parent) = node_borrowed.path.parent() {
                    return Ok(Some(parent.to_path_buf()));
                }
            }
            return Ok(None);
        }

        match key.code {
            _ if config.keybindings.is_search(key.code) => {
                self.last_click_time = None;
                search.enter_mode();
                return Ok(Some(PathBuf::new()));
            }
            KeyCode::Tab => {
                search.toggle_focus();
                return Ok(Some(PathBuf::new()));
            }
            KeyCode::Char('j') | KeyCode::Down => {
                if search.focus_on_results {
                    search.center_selection = true;
                    search.move_down();
                } else {
                    nav.center_selection = true;
                    nav.move_down();
                }
            }
            KeyCode::Char('k') | KeyCode::Up => {
                if search.focus_on_results {
                    search.center_selection = true;
                    search.move_up();
                } else {
                    nav.center_selection = true;
                    nav.move_up();
                }
            }
            KeyCode::Enter => {
                if search.focus_on_results && search.show_results {
                    if let Some(path) = search.get_selected_result() {
                        let _ = nav.expand_path_to_node(&path, false);
                        search.focus_on_results = false;
                    }
                    return Ok(Some(PathBuf::new()));
                } else if let Some(node) = nav.get_selected_node() {
                    let node_borrowed = node.borrow();
                    if node_borrowed.is_dir {
                        let path = node_borrowed.path.clone();
                        drop(node_borrowed);
                        let _ = nav.go_to_directory(path, false);
                    }
                }
            }
            KeyCode::Char('l') | KeyCode::Right if !search.focus_on_results => {
                if let Some(node) = nav.get_selected_node() {
                    let node_borrowed = node.borrow();
                    if node_borrowed.is_dir {
                        let path = node_borrowed.path.clone();
                        drop(node_borrowed);
                        let _ = nav.toggle_node(&path, false);
                    }
                }
            }
            KeyCode::Char('h') | KeyCode::Left => {
                if let Some(node) = nav.get_selected_node() {
                    let node_borrowed = node.borrow();
                    let is_expanded_dir = node_borrowed.is_dir && node_borrowed.is_expanded;
                    let depth = node_borrowed.depth;
                    let path = node_borrowed.path.clone();
                    drop(node_borrowed);
                    if is_expanded_dir {
                        let _ = nav.toggle_node(&path, false)?;
                    } else if depth == 0 {
                        nav.go_to_parent(false)?;
                    } else {
                        nav.select_parent_node();
                        if let Some(parent) = nav.get_selected_node() {
                            let parent_borrowed = parent.borrow();
                            if parent_borrowed.is_dir && parent_borrowed.is_expanded {
                                let parent_path = parent_borrowed.path.clone();
                                drop(parent_borrowed);
                                let _ = nav.toggle_node(&parent_path, false)?;
                            }
                        }
                    }
                } else {
                    nav.go_to_parent(false)?;
                }
            }
            _ if config.keybindings.is_go_to_parent(key.code) => {
                nav.go_to_parent(false)?;
            }
            _ if config.keybindings.is_go_back(key.code) => {
                nav.go_back(false)?;
            }
            _ if config.keybindings.is_create_bookmark(key.code) => {
                bookmarks.enter_creation_mode();
            }
            _ if config.keybindings.is_select_bookmark(key.code) => {
                self.last_click_time = None;
                bookmarks.enter_selection_mode();
            }
            _ if config.keybindings.is_select_disk(key.code) => {
                self.last_click_time = None;
                let current_path = nav.root.borrow().path.clone();
                disks.enter_selection_mode(Some(&current_path));
            }
            _ => {}
        }

        Ok(Some(PathBuf::new()))
    }

    fn handle_search_input(
        &mut self,
        key: KeyEvent,
        search: &mut Search,
        nav: &Navigation,
        config: &Config,
    ) -> Result<Option<PathBuf>> {
        match key.code {
            _ if config.keybindings.is_exit(key.code) => {
                self.last_click_time = None;
                search.exit_mode();
                Ok(Some(PathBuf::new()))
            }
            KeyCode::Enter => {
                search.perform_search(&nav.root, false, nav.show_hidden, nav.follow_symlinks);
                Ok(Some(PathBuf::new()))
            }
            KeyCode::Char(c) => {
                search.add_char(c);
                Ok(Some(PathBuf::new()))
            }
            KeyCode::Backspace => {
                search.backspace();
                Ok(Some(PathBuf::new()))
            }
            _ => Ok(Some(PathBuf::new())),
        }
    }

    /// Handle mouse events
    pub fn handle_mouse(
        &mut self,
        mouse: MouseEvent,
        nav: &mut Navigation,
        search: &mut Search,
        bookmarks: &mut Bookmarks,
        disks: &mut Disks,
        ui: &mut UI,
        config: &Config,
    ) -> Result<()> {
        match mouse.kind {
            MouseEventKind::Down(MouseButton::Left) => {
                self.handle_mouse_click(mouse, nav, search, bookmarks, disks, ui, config)?;
            }
            MouseEventKind::ScrollUp => {
                self.handle_scroll_up(mouse, nav, search, bookmarks, disks, ui, config)?;
            }
            MouseEventKind::ScrollDown => {
                self.handle_scroll_down(mouse, nav, search, bookmarks, disks, ui, config)?;
            }
            _ => {}
        }
        Ok(())
    }

    fn handle_mouse_click(
        &mut self,
        mouse: MouseEvent,
        nav: &mut Navigation,
        search: &mut Search,
        bookmarks: &mut Bookmarks,
        disks: &mut Disks,
        ui: &UI,
        config: &Config,
    ) -> Result<()> {
        if disks.is_selecting {
            if mouse.row >= ui.tree_area_top && mouse.row < ui.tree_area_top + ui.tree_area_height {
                let clicked_row_visible = mouse.row.saturating_sub(ui.tree_area_top) as usize;
                let clicked_disk = clicked_row_visible + ui.disk_scroll_offset;

                if clicked_disk < disks.disks.len() {
                    let now = Instant::now();
                    let is_double_click = if let Some((last_time, last_idx)) = self.last_click_time
                    {
                        clicked_disk == last_idx
                            && now.duration_since(last_time)
                                < Duration::from_millis(config.behavior.double_click_timeout_ms)
                    } else {
                        false
                    };

                    if is_double_click {
                        let path = disks.disks[clicked_disk].mount_point.clone();
                        disks.exit_selection_mode();
                        let _ = nav.go_to_directory(path, false);
                        self.last_click_time = None;
                    } else {
                        disks.selected_index = clicked_disk;
                        disks.center_selection = false;
                        self.last_click_time = Some((now, clicked_disk));
                    }
                }
            }
            return Ok(());
        }

        if bookmarks.is_selecting {
            if mouse.row >= ui.tree_area_top && mouse.row < ui.tree_area_top + ui.tree_area_height {
                let clicked_row_visible = mouse.row.saturating_sub(ui.tree_area_top) as usize;
                let clicked_idx = clicked_row_visible + ui.bookmark_scroll_offset;
                let filtered_len = bookmarks.get_filtered_bookmarks().len();

                if clicked_idx < filtered_len {
                    let now = Instant::now();
                    let is_double_click = if let Some((last_time, last_idx)) = self.last_click_time
                    {
                        clicked_idx == last_idx
                            && now.duration_since(last_time)
                                < Duration::from_millis(config.behavior.double_click_timeout_ms)
                    } else {
                        false
                    };

                    if is_double_click {
                        let path = bookmarks
                            .get_filtered_bookmarks()
                            .get(clicked_idx)
                            .map(|b| b.path.clone());
                        if let Some(path) = path {
                            bookmarks.exit_selection_mode();
                            let _ = nav.go_to_directory(path, false);
                        }
                        self.last_click_time = None;
                    } else {
                        bookmarks.selected_index = clicked_idx;
                        bookmarks.center_selection = false;
                        self.last_click_time = Some((now, clicked_idx));
                    }
                }
            }
            return Ok(());
        }

        if search.show_results && search.focus_on_results {
            if mouse.row >= ui.tree_area_top && mouse.row < ui.tree_area_top + ui.tree_area_height {
                let clicked_row_visible = mouse.row.saturating_sub(ui.tree_area_top) as usize;
                let clicked_idx = clicked_row_visible + ui.search_scroll_offset;

                if clicked_idx < search.results.len() {
                    let now = Instant::now();
                    let is_double_click = if let Some((last_time, last_idx)) = self.last_click_time
                    {
                        clicked_idx == last_idx
                            && now.duration_since(last_time)
                                < Duration::from_millis(config.behavior.double_click_timeout_ms)
                    } else {
                        false
                    };

                    if is_double_click {
                        if let Some(path) = search.results.get(clicked_idx).map(|r| r.path.clone())
                        {
                            let _ = nav.expand_path_to_node(&path, false);
                            search.focus_on_results = false;
                        }
                        self.last_click_time = None;
                    } else {
                        search.selected = clicked_idx;
                        search.center_selection = false;
                        self.last_click_time = Some((now, clicked_idx));
                    }
                }
            }
            return Ok(());
        }

        if mouse.column >= ui.tree_area_start
            && mouse.column < ui.tree_area_end
            && mouse.row >= ui.tree_area_top
            && mouse.row < ui.tree_area_top + ui.tree_area_height
        {
            let clicked_row_visible = mouse.row.saturating_sub(ui.tree_item_top) as usize;
            let clicked_row = clicked_row_visible + ui.tree_scroll_offset;

            if clicked_row < nav.flat_list.len() {
                let now = Instant::now();
                let is_double_click = if let Some((last_time, last_idx)) = self.last_click_time {
                    clicked_row == last_idx
                        && now.duration_since(last_time)
                            < Duration::from_millis(config.behavior.double_click_timeout_ms)
                } else {
                    false
                };

                if is_double_click {
                    let node = &nav.flat_list[clicked_row];
                    let node_borrowed = node.borrow();
                    if node_borrowed.is_dir {
                        let path = node_borrowed.path.clone();
                        drop(node_borrowed);
                        let _ = nav.toggle_node(&path, false);
                    }
                    self.last_click_time = None;
                } else {
                    nav.selected = clicked_row;
                    nav.center_selection = false;
                    self.last_click_time = Some((now, clicked_row));
                }
            }
        }
        Ok(())
    }

    fn handle_scroll_up(
        &mut self,
        _mouse: MouseEvent,
        nav: &mut Navigation,
        search: &mut Search,
        bookmarks: &mut Bookmarks,
        disks: &mut Disks,
        ui: &UI,
        config: &Config,
    ) -> Result<()> {
        if disks.is_selecting {
            disks.move_up();
            disks.center_selection = false;
            return Ok(());
        }
        if bookmarks.is_selecting {
            bookmarks.move_up();
            bookmarks.center_selection = false;
            return Ok(());
        }
        if search.show_results && search.focus_on_results {
            search.move_up();
            search.center_selection = false;
            return Ok(());
        }
        if ui.bottom_panel_height > 0 && bookmarks.is_creating {
            bookmarks.scroll_up();
            return Ok(());
        }
        for _ in 0..config.behavior.mouse_scroll_lines {
            nav.move_up();
        }
        nav.center_selection = false;
        Ok(())
    }

    fn handle_scroll_down(
        &mut self,
        _mouse: MouseEvent,
        nav: &mut Navigation,
        search: &mut Search,
        bookmarks: &mut Bookmarks,
        disks: &mut Disks,
        ui: &UI,
        config: &Config,
    ) -> Result<()> {
        if disks.is_selecting {
            disks.move_down();
            disks.center_selection = false;
            return Ok(());
        }
        if bookmarks.is_selecting {
            bookmarks.move_down();
            bookmarks.center_selection = false;
            return Ok(());
        }
        if search.show_results && search.focus_on_results {
            search.move_down();
            search.center_selection = false;
            return Ok(());
        }
        if ui.bottom_panel_height > 0 && bookmarks.is_creating {
            let max_visible = ui.bookmark_panel_height.max(1);
            bookmarks.scroll_down(max_visible);
            return Ok(());
        }
        for _ in 0..config.behavior.mouse_scroll_lines {
            if nav.selected < nav.flat_list.len().saturating_sub(1) {
                nav.move_down();
            }
        }
        nav.center_selection = false;
        Ok(())
    }
}