summaryrefslogtreecommitdiffstats
path: root/src/modes/edit/search.rs
blob: 4f8e42c644ae2d236dd70352dcf72ef9c45a0224 (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
153
154
155
use anyhow::Result;

use crate::{
    app::{Status, Tab},
    modes::{Display, Flagged, Go, To, Tree},
};

#[derive(Clone, Debug, Default)]
pub struct Search {
    pub regex: Option<regex::Regex>,
    index: Option<usize>,
    nb_matches: Option<usize>,
}

impl Search {
    pub fn new(searched: &str) -> Result<Self> {
        Ok(Self {
            regex: regex::Regex::new(searched).ok(),
            index: None,
            nb_matches: None,
        })
    }

    pub fn complete(&mut self) -> Result<()> {
        Ok(())
    }

    pub fn leave(&self, status: &mut Status) -> Result<()> {
        match status.current_tab().display_mode {
            Display::Tree => {
                self.tree(&mut status.current_tab_mut().tree);
            }
            Display::Directory => {
                self.directory(status.current_tab_mut());
            }
            Display::Flagged => self.flagged(&mut status.menu.flagged),
            _ => (),
        };
        status.update_second_pane_for_preview()
    }

    /// Search in current directory for an file whose name contains `searched_name`,
    /// from a starting position `next_index`.
    /// We search forward from that position and start again from top if nothing is found.
    /// We move the selection to the first matching file.
    pub fn directory(&self, tab: &mut Tab) {
        let Some(re) = &self.regex else {
            return;
        };
        let current_index = tab.directory.index;
        let next_index = if let Some(index) = Self::search_from_index(tab, re, current_index) {
            index
        } else if let Some(index) = Self::search_from_top(tab, re, current_index) {
            index
        } else {
            return;
        };
        tab.go_to_index(next_index);
    }

    pub fn directory_search_next(&self, tab: &Tab) -> Option<usize> {
        let Some(re) = &self.regex else {
            return None;
        };
        let current_index = tab.directory.index;
        if let Some(index) = Self::search_from_index(tab, re, current_index) {
            Some(index)
        } else if let Some(index) = Self::search_from_top(tab, re, current_index) {
            Some(index)
        } else {
            None
        }
    }

    /// Search a file by filename from given index, moving down
    fn search_from_index(tab: &Tab, re: &regex::Regex, current_index: usize) -> Option<usize> {
        for (index, file) in tab.directory.enumerate().skip(current_index) {
            if re.is_match(&file.filename) {
                return Some(index);
            }
        }
        None
    }

    /// Search a file by filename from first line, moving down
    fn search_from_top(tab: &Tab, re: &regex::Regex, current_index: usize) -> Option<usize> {
        for (index, file) in tab.directory.enumerate().take(current_index) {
            if re.is_match(&file.filename) {
                return Some(index);
            }
        }
        None
    }

    pub fn tree(&self, tree: &mut Tree) {
        let path = self.tree_find_next_path(tree).to_owned();
        tree.go(To::Path(&path));
    }

    fn tree_find_next_path<'a>(&self, tree: &'a mut Tree) -> &'a std::path::Path {
        if let Some(pattern) = &self.regex {
            for line in tree
                .displayable()
                .lines()
                .iter()
                .skip(tree.displayable().index() + 1)
            {
                let Some(filename) = line.path.file_name() else {
                    continue;
                };
                if pattern.is_match(&filename.to_string_lossy()) {
                    return &line.path;
                }
            }
            for line in tree
                .displayable()
                .lines()
                .iter()
                .take(tree.displayable().index())
            {
                let Some(filename) = line.path.file_name() else {
                    continue;
                };
                if pattern.is_match(&filename.to_string_lossy()) {
                    return &line.path;
                }
            }
        }
        tree.selected_path()
    }

    pub fn flagged(&self, flagged: &mut Flagged) {
        if let Some(re) = &self.regex {
            let position = if let Some(pos) = flagged
                .content
                .iter()
                .skip(flagged.index + 1)
                .position(|path| re.is_match(&path.file_name().unwrap().to_string_lossy()))
            {
                pos + flagged.index + 1
            } else if let Some(pos) = flagged
                .content
                .iter()
                .take(flagged.index + 1)
                .position(|path| re.is_match(&path.file_name().unwrap().to_string_lossy()))
            {
                pos
            } else {
                return;
            };

            flagged.select_index(position);
        }
    }
}