nucleo-picker 0.6.3

A fuzzy picker tui library based on nucleo
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

/// Mutate a given string in-place, removing ASCII control characters and converting newlines,
/// carriage returns, and TABs to ASCII space.
pub fn normalize_query_string(s: &mut String) {
    *s = s
        .chars()
        .filter_map(normalize_char)
        .map(|(ch, _)| ch)
        .collect();
}

/// Normalize a single char, returning the resulting char as well as the width.
///
/// This automaticlly removes control characters since `ch.width()` returns `None` for control
/// characters.
#[inline]
fn normalize_char(ch: char) -> Option<(char, usize)> {
    match ch {
        '\n' | '\t' => Some((' ', 1)),
        ch => ch.width().map(|w| (ch, w)),
    }
}

/// An edit to apply to an [`EditableString`].
#[derive(Debug, PartialEq, Eq)]
pub enum Edit {
    /// Insert a [`char`] at the current cursor position.
    Insert(char),
    /// Delete a grapheme immediately preceding the current cursor position.
    Backspace,
    /// Delete a grapheme immediately following the current cursor position.
    Delete,
    /// Paste a [`String`] at the current cursor position.
    Paste(String),
    /// Move the cursor left.
    Left,
    /// Move the cursor left an entire word.
    WordLeft,
    /// Move the cursor right.
    Right,
    /// Move the cursor right an entire word.
    WordRight,
    /// Move the cursor to the start.
    ToStart,
    /// Move the cursor to the end.
    ToEnd,
    /// Delete everything before the cursor.
    ClearBefore,
    /// Delete everything after the cursor.
    ClearAfter,
}

/// A movement to apply to an [`EditableString`].
#[derive(Debug, PartialEq, Eq)]
enum CursorMovement {
    /// Move the cursor left.
    Left,
    /// Move the cursor left an entire word.
    WordLeft,
    /// Move the cursor right.
    Right,
    /// Move the cursor right an entire word.
    WordRight,
    /// Move the cursor to the start.
    ToStart,
    /// Move the cursor to the end.
    ToEnd,
}

#[derive(Debug)]
pub struct EditableString {
    contents: String,
    offset: usize,
    screen_offset: u16,
    width: u16,
    left_padding: u16,
    right_padding: u16,
}

impl EditableString {
    /// Create a new editable string with the provided screen width and padding.
    pub fn new(width: u16, padding: u16) -> Self {
        let prompt_padding = padding.min(width.saturating_sub(1) / 2);
        Self {
            contents: String::new(),
            offset: 0,
            screen_offset: 0,
            width,
            left_padding: prompt_padding,
            right_padding: prompt_padding,
        }
    }

    /// Return the prompt contents as well as an 'offset' which is required in the presence of an
    /// initial grapheme that is too large to fit at the beginning of the screen.
    pub fn view(&self) -> (&str, u16) {
        let mut left_indices = self.contents[..self.offset].grapheme_indices(true).rev();
        let mut total_left_width = 0;
        let (left_offset, extra) = loop {
            match left_indices.next() {
                Some((offset, grapheme)) => {
                    total_left_width += grapheme.width();
                    if total_left_width >= self.screen_offset.into() {
                        let extra = (total_left_width - self.screen_offset as usize) as u16;
                        break (
                            offset
                                + if total_left_width == self.screen_offset.into() {
                                    0
                                } else {
                                    grapheme.len()
                                },
                            extra,
                        );
                    }
                }
                None => break (0, 0),
            }
        };

        let mut right_indices = self.contents[self.offset..].grapheme_indices(true);
        let mut total_right_width = 0;
        let max_right_width = self.width - self.screen_offset;
        let right_offset = loop {
            match right_indices.next() {
                Some((offset, grapheme)) => {
                    total_right_width += grapheme.width();
                    if total_right_width > max_right_width as usize {
                        break self.offset + offset;
                    }
                }
                None => break self.contents.len(),
            }
        };

        (&self.contents[left_offset..right_offset], extra)
    }

    /// Resize the screen, adjusting the padding and the screen width.
    pub fn resize(&mut self, width: u16, padding: u16) {
        let prompt_padding = padding.min(width.saturating_sub(1) / 2);
        self.left_padding = prompt_padding;
        self.right_padding = prompt_padding;
        self.width = width;
        self.screen_offset = self.screen_offset.min(width - prompt_padding);
    }

    /// Get the cursor offset within the screen.
    pub fn screen_offset(&self) -> u16 {
        self.screen_offset
    }

    /// Get the contents of the prompt.
    pub fn contents(&self) -> &str {
        &self.contents
    }

    /// Reset the prompt, moving the cursor to the end.
    pub fn set_prompt<Q: Into<String>>(&mut self, prompt: Q) {
        self.contents = prompt.into();
        self.offset = self.contents.len();
    }

    /// Increase the screen offset by the provided width, without exceeding the maximum offset.
    fn increase_by_width(&mut self, width: usize) {
        self.screen_offset = self
            .screen_offset
            .saturating_add(width.try_into().unwrap_or(u16::MAX))
            .min(self.width - self.right_padding);
    }

    /// Insert a character at the cursor position.
    fn insert_char(&mut self, ch: char, w: usize) -> bool {
        self.contents.insert(self.offset, ch);
        self.increase_by_width(w);
        self.offset += ch.len_utf8();
        true
    }

    /// Insert a string at the cursor position.
    fn insert(&mut self, string: &str) -> bool {
        self.contents.insert_str(self.offset, string);
        self.increase_by_width(string.width());
        self.offset += string.len();
        true
    }

    #[inline]
    fn move_left(&self, width: usize) -> u16 {
        // check if we would hit the beginning of the string
        let mut total_left_width = 0;
        let mut graphemes = self.contents[..self.offset].graphemes(true).rev();
        let left_padding = loop {
            match graphemes.next() {
                Some(g) => {
                    total_left_width += g.width();
                    if total_left_width >= self.left_padding as usize {
                        break self.left_padding;
                    }
                }
                None => {
                    break total_left_width as u16;
                }
            }
        };

        self.screen_offset
            .saturating_sub(width.try_into().unwrap_or(u16::MAX))
            .max(left_padding)
    }

    /// Move the cursor.
    #[inline]
    #[allow(clippy::needless_pass_by_value)]
    fn move_cursor(&mut self, cm: CursorMovement) -> bool {
        match cm {
            CursorMovement::Left => {
                match self.contents[..self.offset]
                    .grapheme_indices(true)
                    .next_back()
                {
                    Some((new_offset, gp)) => {
                        self.offset = new_offset;
                        self.screen_offset = self.move_left(gp.width());
                        true
                    }
                    None => false,
                }
            }
            CursorMovement::WordLeft => {
                match self.contents[..self.offset]
                    .unicode_word_indices()
                    .next_back()
                {
                    Some((new_offset, _)) => {
                        let step_width = self.contents[new_offset..self.offset].width();
                        self.offset = new_offset;
                        self.screen_offset = self.move_left(step_width);
                        true
                    }
                    None => false,
                }
            }
            CursorMovement::Right => match self.contents[self.offset..].graphemes(true).next() {
                Some(gp) => {
                    self.offset += gp.len();
                    self.increase_by_width(gp.width());
                    true
                }
                None => false,
            },
            CursorMovement::WordRight => {
                let mut word_indices = self.contents[self.offset..].unicode_word_indices();
                if word_indices.next().is_some() {
                    let next_offset = word_indices
                        .next()
                        .map(|(s, _)| self.offset + s)
                        .unwrap_or(self.contents.len());
                    let step_width = self.contents[self.offset..next_offset].width();
                    self.offset = next_offset;
                    self.increase_by_width(step_width);
                    true
                } else {
                    false
                }
            }
            CursorMovement::ToStart => {
                if self.offset == 0 {
                    false
                } else {
                    self.offset = 0;
                    self.screen_offset = 0;
                    true
                }
            }
            CursorMovement::ToEnd => {
                if self.offset == self.contents.len() {
                    false
                } else {
                    let max_offset = self.width - self.right_padding;
                    for gp in self.contents[self.offset..].graphemes(true) {
                        self.screen_offset = self
                            .screen_offset
                            .saturating_add(gp.width().try_into().unwrap_or(u16::MAX));
                        if self.screen_offset >= max_offset {
                            self.screen_offset = max_offset;
                            break;
                        }
                    }
                    self.offset = self.contents.len();
                    true
                }
            }
        }
    }

    /// Edit the editable string according to the provided [`Edit`] action.
    pub fn edit(&mut self, e: Edit) -> bool {
        match e {
            Edit::Left => self.move_cursor(CursorMovement::Left),
            Edit::WordLeft => self.move_cursor(CursorMovement::WordLeft),
            Edit::Right => self.move_cursor(CursorMovement::Right),
            Edit::WordRight => self.move_cursor(CursorMovement::WordRight),
            Edit::ToStart => self.move_cursor(CursorMovement::ToStart),
            Edit::ToEnd => self.move_cursor(CursorMovement::ToEnd),
            Edit::Insert(ch) => {
                if let Some((ch, w)) = normalize_char(ch) {
                    self.insert_char(ch, w)
                } else {
                    false
                }
            }
            Edit::Paste(mut s) => {
                normalize_query_string(&mut s);
                self.insert(&s)
            }
            Edit::Backspace => {
                let delete_until = self.offset;
                if self.move_cursor(CursorMovement::Left) {
                    self.contents.replace_range(self.offset..delete_until, "");
                    true
                } else {
                    false
                }
            }
            Edit::ClearBefore => {
                if self.offset == 0 {
                    false
                } else {
                    self.contents.replace_range(..self.offset, "");
                    self.offset = 0;
                    self.screen_offset = 0;
                    true
                }
            }
            Edit::Delete => match self.contents[self.offset..].graphemes(true).next() {
                Some(next) => {
                    self.contents
                        .replace_range(self.offset..self.offset + next.len(), "");
                    true
                }
                None => false,
            },
            Edit::ClearAfter => {
                if self.offset == self.contents.len() {
                    false
                } else {
                    self.contents.truncate(self.offset);
                    true
                }
            }
        }
    }

    /// Check if there is no trailing escape `\`.
    fn no_trailing_escape(&self) -> bool {
        (self
            .contents
            .bytes()
            .rev()
            .take_while(|ch| *ch == b'\\')
            .count()
            % 2)
            == 0
    }

    /// Are we in an "appending" state? This is the case if the cursor is at the end of the string
    /// and the previous character isn't an escaped `\`.
    pub fn is_appending(&self) -> bool {
        self.offset == self.contents.len() && self.no_trailing_escape()
    }
}

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

    #[test]
    fn test_layout() {
        let mut editable = EditableString::new(6, 2);
        editable.edit(Edit::Insert('a'));
        assert_eq!(editable.screen_offset, 1);
        editable.edit(Edit::Insert(''));
        assert_eq!(editable.screen_offset, 3);
        editable.edit(Edit::Insert('B'));
        assert_eq!(editable.screen_offset, 4);

        let mut editable = EditableString::new(6, 2);
        editable.edit(Edit::Paste("AaA".to_owned()));
        assert_eq!(editable.screen_offset, 4);

        let mut editable = EditableString::new(6, 2);
        editable.edit(Edit::Paste("abc".to_owned()));
        assert_eq!(editable.screen_offset, 3);
        editable.edit(Edit::Paste("ab".to_owned()));
        assert_eq!(editable.screen_offset, 4);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 3);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 2);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 2);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 1);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 0);

        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("AAAAA".to_owned()));
        editable.edit(Edit::ToStart);
        assert_eq!(editable.screen_offset, 0);
        editable.edit(Edit::Right);
        assert_eq!(editable.screen_offset, 2);
        editable.edit(Edit::Right);
        assert_eq!(editable.screen_offset, 4);
        editable.edit(Edit::Right);
        assert_eq!(editable.screen_offset, 5);
        editable.edit(Edit::Right);
        assert_eq!(editable.screen_offset, 5);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 3);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 2);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 2);
        editable.edit(Edit::Left);
        assert_eq!(editable.screen_offset, 0);

        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("abc".to_owned()));
        editable.edit(Edit::ToStart);
        editable.edit(Edit::ToEnd);
        assert_eq!(editable.screen_offset, 3);
        editable.edit(Edit::Paste("defghi".to_owned()));
        editable.edit(Edit::ToStart);
        editable.edit(Edit::ToEnd);
        assert_eq!(editable.screen_offset, 5);
    }

    #[test]
    fn test_view() {
        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("abc".to_owned()));
        assert_eq!(editable.view(), ("abc", 0));

        let mut editable = EditableString::new(6, 1);
        editable.edit(Edit::Paste("AAAAAA".to_owned()));
        assert_eq!(editable.view(), ("AA", 1));

        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("AAAA".to_owned()));
        assert_eq!(editable.view(), ("AA", 1));
        editable.edit(Edit::Left);
        assert_eq!(editable.view(), ("AA", 1));
        editable.edit(Edit::Left);
        assert_eq!(editable.view(), ("AAA", 0));

        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("012345678".to_owned()));
        editable.edit(Edit::ToStart);
        assert_eq!(editable.view(), ("0123456", 0));

        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("012345A".to_owned()));
        editable.edit(Edit::ToStart);
        assert_eq!(editable.view(), ("012345", 0));

        let mut editable = EditableString::new(4, 1);
        editable.edit(Edit::Paste("01234567".to_owned()));
        assert_eq!(editable.view(), ("567", 0));
        editable.edit(Edit::Left);
        assert_eq!(editable.view(), ("567", 0));
        editable.edit(Edit::Left);
        assert_eq!(editable.view(), ("567", 0));
        editable.edit(Edit::Left);
        assert_eq!(editable.view(), ("4567", 0));
        editable.edit(Edit::Left);
        assert_eq!(editable.view(), ("3456", 0));
        editable.edit(Edit::Left);
        assert_eq!(editable.view(), ("2345", 0));
        editable.edit(Edit::Right);
        assert_eq!(editable.view(), ("2345", 0));
        editable.edit(Edit::Right);
        assert_eq!(editable.view(), ("2345", 0));
        editable.edit(Edit::Right);
        assert_eq!(editable.view(), ("3456", 0));
    }

    #[test]
    fn test_word_movement() {
        let mut editable = EditableString::new(100, 2);
        editable.edit(Edit::Paste("one two".to_owned()));
        editable.edit(Edit::WordLeft);
        editable.edit(Edit::WordLeft);
        assert_eq!(editable.screen_offset, 0);
        editable.edit(Edit::WordRight);
        assert_eq!(editable.screen_offset, 4);
        editable.edit(Edit::WordRight);
        assert_eq!(editable.screen_offset, 7);
        editable.edit(Edit::WordRight);
        assert_eq!(editable.screen_offset, 7);
    }

    #[test]
    fn test_clear() {
        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("Abcde".to_owned()));
        editable.edit(Edit::ToStart);
        editable.edit(Edit::Right);
        editable.edit(Edit::Right);
        editable.edit(Edit::ClearAfter);
        assert_eq!(editable.contents, "Ab");
        editable.edit(Edit::Insert('c'));
        editable.edit(Edit::Left);
        editable.edit(Edit::ClearBefore);
        assert_eq!(editable.contents, "c");
    }

    #[test]
    fn test_delete() {
        let mut editable = EditableString::new(7, 2);
        editable.edit(Edit::Paste("Ab".to_owned()));
        editable.edit(Edit::Backspace);
        assert_eq!(editable.contents, "");
        assert_eq!(editable.screen_offset, 2);
        editable.edit(Edit::Backspace);
        assert_eq!(editable.contents, "");
        assert_eq!(editable.screen_offset, 0);
    }

    #[test]
    fn test_normalize_query() {
        let mut s = "a\nb".to_owned();
        normalize_query_string(&mut s);
        assert_eq!(s, "a b");

        let mut s = "\n".to_owned();
        normalize_query_string(&mut s);
        assert_eq!(s, "o o");

        let mut s = "a\n\u{07}".to_owned();
        normalize_query_string(&mut s);
        assert_eq!(s, "a o");
    }

    #[test]
    fn test_editable() {
        let mut editable = EditableString::new(3, 1);
        for e in [
            Edit::Insert('a'),
            Edit::Left,
            Edit::Insert('b'),
            Edit::ToEnd,
            Edit::Insert('c'),
            Edit::ToStart,
            Edit::Insert('d'),
            Edit::Left,
            Edit::Left,
            Edit::Right,
            Edit::Insert('e'),
        ] {
            editable.edit(e);
        }
        assert_eq!(editable.contents, "debac");

        let mut editable = EditableString::new(3, 1);
        for e in [
            Edit::Insert('a'),
            Edit::Insert('b'),
            Edit::Insert('c'),
            Edit::Insert('d'),
            Edit::Left,
            Edit::Insert('1'),
            Edit::Insert('2'),
            Edit::Insert('3'),
            Edit::ToStart,
            Edit::Backspace,
            Edit::Insert('4'),
            Edit::ToEnd,
            Edit::Backspace,
            Edit::Left,
            Edit::Delete,
        ] {
            editable.edit(e);
        }

        assert_eq!(editable.contents, "4abc12");
    }

    #[test]
    fn test_editable_unicode() {
        let mut editable = EditableString::new(3, 1);
        for e in [
            Edit::Paste("दे".to_owned()),
            Edit::Left,
            Edit::Insert('a'),
            Edit::ToEnd,
            Edit::Insert(''),
        ] {
            editable.edit(e);
        }
        assert_eq!(editable.contents, "aदेA");

        for e in [
            Edit::ToStart,
            Edit::Right,
            Edit::ToEnd,
            Edit::Left,
            Edit::Backspace,
        ] {
            editable.edit(e);
        }

        assert_eq!(editable.contents, "aA");
    }
}