aboutsummaryrefslogtreecommitdiff
path: root/src/event_handler/search_keys.rs
blob: db59fa5d25014e9ec90adb58ce664c280385bbc1 (plain)
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
use crate::{
    app::{App, AppResult, SelectedTab},
    ui::InputMode,
};
use crossterm::event::{KeyCode, KeyEvent};
use rust_fuzzy_search::{self, fuzzy_search_sorted};

pub fn handle_search_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
    match app.selected_tab {
        SelectedTab::DirectoryBrowser => {
            let list: Vec<&str> = app
                .browser
                .filetree
                .iter()
                .map(|(_, f)| f.as_str())
                .collect::<Vec<&str>>();

            let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
            let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();

            for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
                if item.contains(res.first().unwrap()) {
                    app.browser.selected = i;
                }
            }
        }

        SelectedTab::Queue => {
            let list: Vec<&str> = app
                .queue_list
                .list
                .iter()
                .map(|f| f.file.as_str())
                .collect::<Vec<&str>>();
            let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
            let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();

            for (i, item) in app.queue_list.list.iter().enumerate() {
                if item.file.contains(res.first().unwrap()) {
                    app.queue_list.index = i;
                }
            }
        }

        SelectedTab::Playlists => {
            let list: Vec<&str> = app
                .pl_list
                .list
                .iter()
                .map(|f| f.as_str())
                .collect::<Vec<&str>>();
            let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
            let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();

            for (i, item) in app.pl_list.list.iter().enumerate() {
                if item.contains(res.first().unwrap()) {
                    app.pl_list.index = i;
                }
            }
        }
    }

    // Keybind for searching
    //
    // Keybinds for when the search prompt is visible
    match key_event.code {
        KeyCode::Esc => {
            app.inputmode = InputMode::Normal;
        }
        KeyCode::Char(to_insert) => {
            app.enter_char(to_insert);
        }
        KeyCode::Enter => {
            let list: Vec<&str> = app
                .browser
                .filetree
                .iter()
                .map(|(_, f)| f.as_str())
                .collect::<Vec<&str>>();

            let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
            let (res, _) = res.first().unwrap();

            for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
                if item.contains(res) {
                    app.browser.selected = i;
                }
            }

            app.search_input.clear();
            app.reset_cursor();
            app.inputmode = InputMode::Normal;
        }

        KeyCode::Backspace => {
            app.delete_char();
        }

        KeyCode::Left => {
            app.move_cursor_left();
        }

        KeyCode::Right => {
            app.move_cursor_right();
        }

        _ => {}
    }
    Ok(())
}