embedded-cli 0.2.1

CLI with autocompletion, help and history for embedded systems (like Arduino or STM32)
Documentation
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::{buffer::Buffer, utils};
use core::{
    fmt::Debug,
    ops::{Bound, RangeBounds},
};

#[cfg(feature = "autocomplete")]
use crate::autocomplete::{Autocompletion, Request};

pub struct Editor<B: Buffer> {
    buffer: B,

    /// Where next char will be inserted
    cursor: usize,

    /// How many bytes of valid utf-8 are stored in buffer
    valid: usize,
}

impl<B: Buffer> Debug for Editor<B> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Editor")
            .field("buffer", &self.buffer.as_slice())
            .field("cursor", &self.cursor)
            .field("valid", &self.valid)
            .finish()
    }
}

impl<B: Buffer> Editor<B> {
    pub fn new(buffer: B) -> Self {
        Self {
            buffer,
            cursor: 0,
            valid: 0,
        }
    }

    #[cfg(feature = "autocomplete")]
    /// Calls given function to create autocompletion of current input
    pub fn autocompletion(&mut self, f: impl FnOnce(Request<'_>, &mut Autocompletion<'_>)) {
        let text = self.text();

        let removed_spaces = if let Some(pos) = utils::char_byte_index(text, self.cursor) {
            // cursor is inside text, so trim all whitespace, that is on the right to the cursor
            let right = &text.as_bytes()[pos..];
            right
                .iter()
                .rev()
                .position(|&b| b != b' ')
                .unwrap_or(right.len())
        } else {
            0
        };
        let request_len = text.len() - removed_spaces;

        // SAFETY: request_len is always less than or equal to buffer len
        let (text, buf) = unsafe { utils::split_at_mut(self.buffer.as_slice_mut(), request_len) };
        // SAFETY: request_len is guaranteed to be inside text slice and at char boundary
        let text = unsafe { core::str::from_utf8_unchecked(text) };

        // SAFETY: in `new` we checked that Request can be created from this input
        if let Some(request) = Request::from_input(text) {
            let mut autocompletion = Autocompletion::new(buf);

            f(request, &mut autocompletion);

            // process autocompletion
            if let Some(autocompleted) = autocompletion.autocompleted() {
                let autocompleted = autocompleted.len();
                self.valid = request_len + autocompleted;
                if !autocompletion.is_partial() && self.valid < self.buffer.len() {
                    self.buffer.as_slice_mut()[self.valid] = b' ';
                    self.valid += 1;
                }
                self.cursor = self.len();
                return;
            }
        }

        // autocompletion was not successful, so restore removed spaces
        if removed_spaces > 0 {
            // SAFETY: given range is always inside slice
            unsafe {
                self.buffer
                    .as_slice_mut()
                    .get_unchecked_mut(self.valid - removed_spaces..self.valid)
                    .fill(b' ');
            }
        }
    }

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

    pub fn cursor(&self) -> usize {
        self.cursor
    }

    pub fn insert(&mut self, text: &str) -> Option<&str> {
        let remaining = self.buffer.len() - self.valid;
        let chars = utils::char_count(text);
        let text = text.as_bytes();
        if remaining < text.len() {
            //TODO: try to grow buffer
            return None;
        }
        let cursor = if let Some(cursor) = utils::char_byte_index(self.text(), self.cursor) {
            self.buffer
                .as_slice_mut()
                .copy_within(cursor..self.valid, cursor + text.len());
            cursor
        } else {
            self.valid
        };
        self.buffer.as_slice_mut()[cursor..cursor + text.len()].copy_from_slice(text);
        let text = &self.buffer.as_slice()[cursor..cursor + text.len()];
        self.cursor += chars;
        self.valid += text.len();
        //SAFETY: we just copied valid utf-8 from &str to this location
        Some(unsafe { core::str::from_utf8_unchecked(text) })
    }

    pub fn len(&self) -> usize {
        utils::char_count(self.text())
    }

    pub fn move_left(&mut self) -> bool {
        if self.cursor > 0 {
            self.cursor -= 1;
            true
        } else {
            false
        }
    }

    pub fn move_right(&mut self) -> bool {
        if self.cursor < self.len() {
            self.cursor += 1;
            true
        } else {
            false
        }
    }

    /// Removes char at cursor position
    pub fn remove(&mut self) {
        let cursor_pos = utils::char_byte_index(self.text(), self.cursor);
        let next_pos = if let Some(cursor_pos) = cursor_pos {
            // SAFETY: cursor_pos is at char boundary
            let text = unsafe { self.text().get_unchecked(cursor_pos..) };
            utils::char_byte_index(text, 1).map(|s| s + cursor_pos)
        } else {
            None
        };

        match (cursor_pos, next_pos) {
            (Some(cursor), None) => {
                // we are at the last char, so just decrease valid size
                self.valid = cursor;
            }
            (Some(cursor), Some(next)) => {
                self.buffer
                    .as_slice_mut()
                    .copy_within(next..self.valid, cursor);
                self.valid -= next - cursor;
            }
            _ => {} // nothing to remove
        }
    }

    pub fn text(&self) -> &str {
        // SAFETY: buffer stores only valid utf-8 bytes 0..valid range
        unsafe {
            core::str::from_utf8_unchecked(self.buffer.as_slice().get_unchecked(..self.valid))
        }
    }

    pub fn text_mut(&mut self) -> &mut str {
        // SAFETY: buffer stores only valid utf-8 bytes 0..valid range
        unsafe {
            core::str::from_utf8_unchecked_mut(
                self.buffer.as_slice_mut().get_unchecked_mut(..self.valid),
            )
        }
    }

    /// Returns text in subrange of this editor. start is including, end is exclusive
    #[allow(dead_code)]
    pub fn text_range(&self, range: impl RangeBounds<usize>) -> &str {
        let (start, num_chars) = match (range.start_bound(), range.end_bound()) {
            (Bound::Included(start), Bound::Included(end)) => {
                if end < start {
                    return "";
                }
                (*start, Some(end - start + 1))
            }
            (Bound::Included(start), Bound::Excluded(end)) => {
                if end <= start {
                    return "";
                }
                (*start, Some(end - start))
            }
            (Bound::Unbounded, Bound::Included(end)) => (0, Some(end + 1)),
            (Bound::Unbounded, Bound::Excluded(end)) => {
                if *end == 0 {
                    return "";
                }
                (0, Some(*end))
            }
            (Bound::Included(start), Bound::Unbounded) => (*start, None),
            (Bound::Unbounded, Bound::Unbounded) => (0, None),
            (Bound::Excluded(_), _) => unreachable!(),
        };

        let text = self.text();

        let (start, end) = if let Some(num_chars) = num_chars {
            if let Some(pos) = utils::char_byte_index(text, start) {
                // SAFETY: pos is at char boundary
                let text = unsafe { text.get_unchecked(pos..) };
                let b = utils::char_byte_index(text, num_chars).map(|s| s + pos);
                (Some(pos), b)
            } else {
                (None, None)
            }
        } else {
            (utils::char_byte_index(text, start), None)
        };

        match (start, end) {
            (Some(start), Some(end)) => {
                // SAFETY: we take substring from valid utf8 slice
                unsafe { core::str::from_utf8_unchecked(&text.as_bytes()[start..end]) }
            }
            (Some(start), None) => {
                // SAFETY: we take substring from valid utf8 slice
                unsafe { core::str::from_utf8_unchecked(&text.as_bytes()[start..]) }
            }
            _ => "",
        }
    }
}

#[cfg(test)]
mod tests {
    use core::ops::RangeBounds;
    use std::string::String;

    use rstest::rstest;

    use super::Editor;

    #[test]
    fn add_chars_to_back() {
        let mut editor = Editor::new([0; 128]);

        let text = "abcdабвг佐佗佟𑿁𑿆𑿌";

        for (i, b) in text.chars().enumerate() {
            let mut buffer = [0u8; 4];
            editor.insert(b.encode_utf8(&mut buffer));
            let exp: String = text.chars().take(i + 1).collect();
            assert_eq!(editor.text(), &exp);
        }
    }

    #[test]
    fn add_chars_to_front() {
        let mut editor = Editor::new([0; 128]);

        let text = "abcdабвг佐佗佟𑿁𑿆𑿌";

        for (i, b) in text.chars().enumerate() {
            let mut buffer = [0u8; 4];
            editor.insert(b.encode_utf8(&mut buffer));
            assert!(editor.move_left());
            let exp = text
                .chars()
                .take(i + 1)
                .collect::<String>()
                .chars()
                .rev()
                .collect::<String>();
            assert_eq!(editor.text(), &exp);
        }
    }

    #[rstest]
    #[case("abc", 1, "Ж", "abЖc")]
    #[case("abc", 2, "Ж", "aЖbc")]
    #[case("abc", 3, "Ж ", "Ж abc")]
    #[case("abc", 4, "Ж ", "Ж abc")]
    #[case("adbc佐佗𑿌", 2, "Ж", "adbc佐Ж佗𑿌")]
    fn move_left_insert(
        #[case] initial: &str,
        #[case] count: usize,
        #[case] inserted: &str,
        #[case] expected: &str,
    ) {
        let mut editor = Editor::new([0; 128]);

        editor.insert(initial);

        for _ in 0..count {
            editor.move_left();
        }

        editor.insert(inserted);

        assert_eq!(editor.text_range(..), expected);
    }

    #[rstest]
    #[case("abc", 3, 1, "Ж", "aЖbc")]
    #[case("абв", 3, 2, "Ж", "абЖв")]
    #[case("абв", 1, 1, "Ж ", "абвЖ ")]
    #[case("абв", 1, 2, "Ж ", "абвЖ ")]
    #[case("adbc佐佗𑿌", 4, 2, "Ж", "adbc佐Ж佗𑿌")]
    fn move_left_then_right_insert(
        #[case] initial: &str,
        #[case] count_left: usize,
        #[case] count_right: usize,
        #[case] inserted: &str,
        #[case] expected: &str,
    ) {
        let mut editor = Editor::new([0; 128]);

        editor.insert(initial);

        for _ in 0..count_left {
            editor.move_left();
        }
        for _ in 0..count_right {
            editor.move_right();
        }

        editor.insert(inserted);

        assert_eq!(editor.text_range(..), expected);
    }

    #[test]
    fn remove() {
        let mut editor = Editor::new([0; 128]);

        editor.insert("adbc佐佗𑿌");
        assert_eq!(editor.cursor, 7);
        editor.remove();

        assert_eq!(editor.text(), "adbc佐佗𑿌");
        assert_eq!(editor.cursor, 7);

        editor.move_left();
        editor.remove();

        assert_eq!(editor.text(), "adbc佐佗");
        assert_eq!(editor.cursor, 6);

        editor.move_left();
        editor.move_left();
        editor.remove();

        assert_eq!(editor.text(), "adbc佗");
        assert_eq!(editor.cursor, 4);

        editor.move_left();
        editor.move_left();
        editor.move_left();
        editor.remove();

        assert_eq!(editor.text(), "abc佗");
        assert_eq!(editor.cursor, 1);

        editor.move_left();
        editor.remove();

        assert_eq!(editor.text(), "bc佗");
        assert_eq!(editor.cursor, 0);

        editor.remove();
        assert_eq!(editor.text(), "c佗");

        editor.remove();
        assert_eq!(editor.text(), "");

        editor.remove();
        assert_eq!(editor.text(), "");
    }

    #[rstest]
    #[case(1, "adbc佐佗")]
    #[case(2, "adbc佐𑿌")]
    #[case(3, "adbc佗𑿌")]
    #[case(4, "adb佐佗𑿌")]
    #[case(5, "adc佐佗𑿌")]
    #[case(6, "abc佐佗𑿌")]
    #[case(7, "dbc佐佗𑿌")]
    fn remove_inside(#[case] dist: usize, #[case] expected: &str) {
        let mut editor = Editor::new([0; 128]);

        editor.insert("adbc佐佗𑿌");

        for _ in 0..dist {
            editor.move_left();
        }
        editor.remove();

        assert_eq!(editor.text(), expected);
    }

    #[rstest]
    #[case(.., "adbc佐佗𑿌")]
    #[case(..2, "ad")]
    #[case(0..2, "ad")]
    #[case(2.., "bc佐佗𑿌")]
    #[case(5.., "佗𑿌")]
    #[case(..6, "adbc佐佗")]
    #[case(..7, "adbc佐佗𑿌")]
    #[case(..=6, "adbc佐佗𑿌")]
    #[case(3..5, "c佐")]
    #[case(3..6, "c佐佗")]
    #[case(3..3, "")]
    #[case(..0, "")]
    #[case(1..=0, "")]
    #[case(5..=5, "")]
    fn text_range(#[case] range: impl RangeBounds<usize>, #[case] expected: &str) {
        let mut editor = Editor::new([0; 128]);

        editor.insert("adbc佐佗𑿌");

        assert_eq!(editor.text_range(range), expected);
    }
}