aboutsummaryrefslogtreecommitdiff
path: root/src/command.rs
blob: 064d767db54a08cb1d52d8a1a56a8a63101f3872 (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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#[derive(Debug)]
pub struct CommandBox {
    pub history: History<String>,
    pub hist_idx: Option<usize>,
    pub text: String,
    pub cursor: usize,
}

impl CommandBox {
    pub fn new() -> Self {
        CommandBox {
            history: History::new(64),
            hist_idx: None,
            text: String::new(),
            cursor: 0,
        }
    }

    pub fn forward(&mut self) {
        if self.cursor < self.text.len() {
            self.cursor += 1;
        }
    }

    fn cursor_end(&mut self) {
        self.cursor = self.text.len();
    }

    fn cursor_start(&mut self) {
        self.cursor = 0;
    }

    pub fn backward(&mut self) {
        self.cursor = self.cursor.saturating_sub(1);
    }

    pub fn backspace(&mut self) {
        if self.cursor != 0 {
            self.text.remove(self.cursor - 1);
            self.backward();
        }
    }

    pub fn delete(&mut self) {
        if self.cursor < self.text.len() {
            self.text.remove(self.cursor);
        }
    }

    pub fn push_str(&mut self, v: &str) {
        self.text.push_str(v);
        self.cursor += v.len();
    }

    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }

    pub fn clear(&mut self) {
        self.text.clear();
        self.cursor = 0;
    }

    pub fn hist_append(&mut self) {
        self.history.append(self.text.drain(..).collect());
        self.cursor_start();
    }

    fn get_from_hist(&self) -> String {
        let size = self.history.items.len();
        self.history.items[size - 1 - self.hist_idx.unwrap()].clone()
    }

    pub fn hist_prev(&mut self) {
        if let Some(idx) = self.hist_idx {
            if !(idx + 1 >= self.history.items.len()) {
                self.hist_idx = Some(idx + 1);
                self.text = self.get_from_hist();
                self.cursor_end();
            }
        } else {
            self.hist_idx = Some(0);
            self.text = self.get_from_hist();
            self.cursor_end();
        }
    }

    pub fn hist_next(&mut self) {
        if let Some(idx) = self.hist_idx {
            // most recent hist item, reset command box
            if idx == 0 {
                self.hist_idx = None;
                self.text = "(".into();
            } else {
                self.hist_idx = Some(idx - 1);
                self.text = self.get_from_hist();
            }
            self.cursor_end();
        }
    }
}

#[derive(Debug)]
pub struct History<T> {
    pub items: Vec<T>,
    pub max_size: usize,
}

impl<T> History<T> {
    pub fn new(max_size: usize) -> Self {
        if max_size == 0 {
            panic!();
        }
        Self {
            items: vec![],
            max_size,
        }
    }

    pub fn append(&mut self, item: T) {
        if self.items.len() >= self.max_size {
            self.items.remove(0);
        }
        self.items.push(item);
    }
}

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

    fn setup_with(text: &str) -> CommandBox {
        let mut cmd = CommandBox::new();
        cmd.push_str(text);
        cmd
    }

    #[test]
    fn entering_text() {
        let cmd = setup_with("save as file.png");
        assert_eq!(&cmd.text, "save as file.png");
        assert_eq!(cmd.cursor, 16)
    }

    #[test]
    fn backspacing_from_end() {
        let mut cmd = setup_with("save");
        cmd.backspace();
        assert_eq!(&cmd.text, "sav");
        assert_eq!(cmd.cursor, 3);
    }

    #[test]
    fn backspacing_from_middle() {
        let mut cmd = setup_with("save");
        cmd.backward();
        cmd.backspace();
        assert_eq!(&cmd.text, "sae");
        assert_eq!(cmd.cursor, 2);
    }

    #[test]
    fn delete() {
        let mut cmd = setup_with("save");
        cmd.backward();
        cmd.delete();
        assert_eq!(&cmd.text, "sav");
        assert_eq!(cmd.cursor, 3);
    }

    #[test]
    fn delete_end() {
        let mut cmd = setup_with("save");
        cmd.delete();
        assert_eq!(&cmd.text, "save");
    }

    #[test]
    fn delete_all() {
        let mut cmd = setup_with("save");
        for _ in 0..4 {
            cmd.backward();
        }
        for _ in 0..4 {
            cmd.delete();
        }
        assert_eq!(&cmd.text, "");
        assert_eq!(cmd.cursor, 0);
    }

    #[test]
    fn seeking() {
        let mut cmd = setup_with("save");
        for _ in 0..4 {
            cmd.backward();
        }
        assert_eq!(cmd.cursor, 0);
        cmd.forward();
        assert_eq!(cmd.cursor, 1);
    }

    #[test]
    fn hist_append() {
        let mut cmd = setup_with("hello");
        cmd.hist_append();
        cmd.push_str("another");
        cmd.hist_append();
        cmd.push_str("one");
        cmd.hist_append();
        assert_eq!(cmd.history.items.len(), 3);
    }

    #[test]
    fn hist_prev() {
        let mut cmd = setup_with("hello");
        cmd.hist_append();
        cmd.push_str("another");
        cmd.hist_append();
        cmd.push_str("one");
        cmd.hist_append();

        cmd.hist_prev();
        assert_eq!(&cmd.text, "one");
        cmd.hist_prev();
        assert_eq!(&cmd.text, "another");
    }

    #[test]
    fn hist_next() {
        let mut cmd = setup_with("hello");
        cmd.hist_append();
        cmd.push_str("another");
        cmd.hist_append();
        cmd.push_str("one");
        cmd.hist_append();

        cmd.hist_prev();
        cmd.hist_prev();
        cmd.hist_next();
        assert_eq!(&cmd.text, "one");
    }
}

#[cfg(test)]
mod history_tests {
    use super::*;
    #[test]
    fn append() {
        let mut h = History::<u32>::new(4);
        h.append(5);
        h.append(6);
        h.append(7);
        h.append(8);
        h.append(9);
        assert_eq!(h.items, vec![6, 7, 8, 9]);
    }
}