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
use smallvec::SmallVec;
const SEARCH_HISTORY_LIMIT: usize = 20;

#[derive(Default, Debug, Clone)]
pub struct SearchHistory {
    history: SmallVec<[String; SEARCH_HISTORY_LIMIT]>,
    selected: Option<usize>,
}

impl SearchHistory {
    pub fn add(&mut self, search_string: String) {
        if let Some(i) = self.selected {
            if search_string == self.history[i] {
                return;
            }
        }

        if let Some(previously_searched) = self.history.last_mut() {
            if search_string.find(previously_searched.as_str()).is_some() {
                *previously_searched = search_string;
                self.selected = Some(self.history.len() - 1);
                return;
            }
        }

        self.history.push(search_string);
        if self.history.len() > SEARCH_HISTORY_LIMIT {
            self.history.remove(0);
        }
        self.selected = Some(self.history.len() - 1);
    }

    pub fn next(&mut self) -> Option<&str> {
        let history_size = self.history.len();
        if history_size == 0 {
            return None;
        }

        let selected = self.selected?;
        if selected == history_size - 1 {
            return None;
        }
        let next_index = selected + 1;
        self.selected = Some(next_index);
        Some(&self.history[next_index])
    }

    pub fn current(&self) -> Option<&str> {
        Some(&self.history[self.selected?])
    }

    pub fn previous(&mut self) -> Option<&str> {
        let history_size = self.history.len();
        if history_size == 0 {
            return None;
        }

        let prev_index = match self.selected {
            Some(selected_index) => {
                if selected_index == 0 {
                    return None;
                } else {
                    selected_index - 1
                }
            }
            None => history_size - 1,
        };

        self.selected = Some(prev_index);
        Some(&self.history[prev_index])
    }

    pub fn reset_selection(&mut self) {
        self.selected = None;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add() {
        let mut search_history = SearchHistory::default();
        assert_eq!(
            search_history.current(),
            None,
            "No current selection should be set fo the default search history"
        );

        search_history.add("rust".to_string());
        assert_eq!(
            search_history.current(),
            Some("rust"),
            "Newly added item should be selected"
        );

        // check if duplicates are not added
        search_history.add("rust".to_string());
        assert_eq!(
            search_history.history.len(),
            1,
            "Should not add a duplicate"
        );
        assert_eq!(search_history.current(), Some("rust"));

        // check if new string containing the previous string replaces it
        search_history.add("rustlang".to_string());
        assert_eq!(
            search_history.history.len(),
            1,
            "Should replace previous item if it's a substring"
        );
        assert_eq!(search_history.current(), Some("rustlang"));

        // push enough items to test SEARCH_HISTORY_LIMIT
        for i in 0..SEARCH_HISTORY_LIMIT * 2 {
            search_history.add(format!("item{i}"));
        }
        assert!(search_history.history.len() <= SEARCH_HISTORY_LIMIT);
    }

    #[test]
    fn test_next_and_previous() {
        let mut search_history = SearchHistory::default();
        assert_eq!(
            search_history.next(),
            None,
            "Default search history should not have a next item"
        );

        search_history.add("Rust".to_string());
        assert_eq!(search_history.next(), None);
        search_history.add("JavaScript".to_string());
        assert_eq!(search_history.next(), None);
        search_history.add("TypeScript".to_string());
        assert_eq!(search_history.next(), None);

        assert_eq!(search_history.current(), Some("TypeScript"));

        assert_eq!(search_history.previous(), Some("JavaScript"));
        assert_eq!(search_history.current(), Some("JavaScript"));

        assert_eq!(search_history.previous(), Some("Rust"));
        assert_eq!(search_history.current(), Some("Rust"));

        assert_eq!(search_history.previous(), None);
        assert_eq!(search_history.current(), Some("Rust"));

        assert_eq!(search_history.next(), Some("JavaScript"));
        assert_eq!(search_history.current(), Some("JavaScript"));

        assert_eq!(search_history.next(), Some("TypeScript"));
        assert_eq!(search_history.current(), Some("TypeScript"));

        assert_eq!(search_history.next(), None);
        assert_eq!(search_history.current(), Some("TypeScript"));
    }

    #[test]
    fn test_reset_selection() {
        let mut search_history = SearchHistory::default();
        search_history.add("Rust".to_string());
        search_history.add("JavaScript".to_string());
        search_history.add("TypeScript".to_string());

        assert_eq!(search_history.current(), Some("TypeScript"));
        search_history.reset_selection();
        assert_eq!(search_history.current(), None);
        assert_eq!(
            search_history.previous(),
            Some("TypeScript"),
            "Should start from the end after reset on previous item query"
        );

        search_history.previous();
        assert_eq!(search_history.current(), Some("JavaScript"));
        search_history.previous();
        assert_eq!(search_history.current(), Some("Rust"));

        search_history.reset_selection();
        assert_eq!(search_history.current(), None);
    }
}