summaryrefslogtreecommitdiffstats
path: root/src/commands/search.rs
blob: ed0d6ca075b7702065d5425ba35f2220a0c719c2 (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
use globset::GlobMatcher;

use crate::context::AppContext;
use crate::error::JoshutoResult;

use super::cursor_move;
use super::search_glob;
use super::search_string;

#[derive(Clone, Debug)]
pub enum SearchPattern {
    Glob(GlobMatcher),
    String(String),
}

pub fn search_next(context: &mut AppContext) -> JoshutoResult<()> {
    if let Some(s) = context.get_search_state() {
        let index = match s {
            SearchPattern::Glob(s) => {
                search_glob::search_glob_fwd(context.tab_context_ref().curr_tab_ref(), s)
            }
            SearchPattern::String(s) => {
                search_string::search_string_fwd(context.tab_context_ref().curr_tab_ref(), s)
            }
        };
        if let Some(index) = index {
            let _ = cursor_move::cursor_move(index, context);
        }
    }
    Ok(())
}

pub fn search_prev(context: &mut AppContext) -> JoshutoResult<()> {
    if let Some(s) = context.get_search_state() {
        let index = match s {
            SearchPattern::Glob(s) => {
                search_glob::search_glob_rev(context.tab_context_ref().curr_tab_ref(), s)
            }
            SearchPattern::String(s) => {
                search_string::search_string_rev(context.tab_context_ref().curr_tab_ref(), s)
            }
        };
        if let Some(index) = index {
            let _ = cursor_move::cursor_move(index, context);
        }
    }
    Ok(())
}