summaryrefslogtreecommitdiffstats
path: root/src/commands/search.rs
blob: 23180c4daa6d532947d5bb5b1b7f035638a80c8b (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
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
use lazy_static::lazy_static;
use std::sync::Mutex;

use crate::commands::{cursor_move, JoshutoCommand, JoshutoRunnable};
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::tab::JoshutoTab;
use crate::ui::TuiBackend;

lazy_static! {
    static ref SEARCH_PATTERN: Mutex<Option<String>> = Mutex::new(None);
}

#[derive(Clone, Debug)]
pub struct Search {
    pattern: String,
}

impl Search {
    pub fn new(pattern: &str) -> Self {
        Search {
            pattern: pattern.to_lowercase(),
        }
    }
    pub const fn command() -> &'static str {
        "search"
    }
    pub fn search(curr_tab: &JoshutoTab, pattern: &str) -> Option<usize> {
        let curr_list = curr_tab.curr_list_ref()?;

        let offset = curr_list.index? + 1;
        let contents_len = curr_list.contents.len();
        for i in 0..contents_len {
            let file_name_lower = curr_list.contents[(offset + i) % contents_len]
                .file_name()
                .to_lowercase();
            if file_name_lower.contains(pattern) {
                return Some((offset + i) % contents_len);
            }
        }
        None
    }
    pub fn search_rev(curr_tab: &JoshutoTab, pattern: &str) -> Option<usize> {
        let curr_list = curr_tab.curr_list_ref()?;

        let offset = curr_list.index?;
        let contents_len = curr_list.contents.len();
        for i in (0..contents_len).rev() {
            let file_name_lower = curr_list.contents[(offset + i) % contents_len]
                .file_name()
                .to_lowercase();
            if file_name_lower.contains(pattern) {
                return Some((offset + i) % contents_len);
            }
        }
        None
    }
}

impl JoshutoCommand for Search {}

impl std::fmt::Display for Search {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{} {}", Self::command(), self.pattern)
    }
}

impl JoshutoRunnable for Search {
    fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> {
        let index = Self::search(&context.tabs[context.curr_tab_index], &self.pattern);
        if let Some(index) = index {
            cursor_move::cursor_move(index, context);
        }
        let mut data = SEARCH_PATTERN.lock().unwrap();
        match data.as_ref() {
            Some(s) => {
                if *s != self.pattern {
                    *data = Some(self.pattern.clone());
                }
            }
            None => *data = Some(self.pattern.clone()),
        }
        Ok(())
    }
}

fn search_with_func(
    context: &mut JoshutoContext,
    search_func: fn(&JoshutoTab, &str) -> Option<usize>,
) {
    let data = SEARCH_PATTERN.lock().unwrap();
    if let Some(s) = (*data).as_ref() {
        let index = search_func(&context.tabs[context.curr_tab_index], s);
        if let Some(index) = index {
            cursor_move::cursor_move(index, context);
        }
    }
}

#[derive(Clone, Debug)]
pub struct SearchNext;

impl SearchNext {
    pub fn new() -> Self {
        SearchNext
    }
    pub const fn command() -> &'static str {
        "search_next"
    }
}

impl JoshutoCommand for SearchNext {}

impl std::fmt::Display for SearchNext {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(Self::command())
    }
}

impl JoshutoRunnable for SearchNext {
    fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> {
        search_with_func(context, Search::search);
        Ok(())
    }
}

#[derive(Clone, Debug)]
pub struct SearchPrev;

impl SearchPrev {
    pub fn new() -> Self {
        SearchPrev
    }
    pub const fn command() -> &'static str {
        "search_prev"
    }
}

impl JoshutoCommand for SearchPrev {}

impl std::fmt::Display for SearchPrev {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(Self::command())
    }
}

impl JoshutoRunnable for SearchPrev {
    fn execute(&self, context: &mut JoshutoContext, _: &mut TuiBackend) -> JoshutoResult<()> {
        search_with_func(context, Search::search_rev);
        Ok(())
    }
}