nucleo-picker 0.7.0-alpha.2

A performant and Unicode-aware fuzzy picker tui library
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
#[cfg(test)]
mod tests;

use unicode_segmentation::UnicodeSegmentation;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

use crate::{
    component::{Component, Status},
    util::as_u16,
};

trait Cursor {
    fn right(self, s: &str, steps: usize) -> Self;
    fn right_word(self, s: &str, steps: usize) -> Self;
    fn left(self, s: &str, steps: usize) -> Self;
    fn left_word(self, s: &str, steps: usize) -> Self;
}

impl Cursor for usize {
    fn right(self, s: &str, steps: usize) -> Self {
        match s[self..].grapheme_indices(true).nth(steps) {
            Some((offset, _)) => self + offset,
            None => s.len(),
        }
    }

    fn right_word(self, s: &str, steps: usize) -> Self {
        match s[self..].unicode_word_indices().nth(steps) {
            Some((offset, _)) => self + offset,
            None => s.len(),
        }
    }

    fn left(self, s: &str, steps: usize) -> Self {
        match s[..self].grapheme_indices(true).rev().take(steps).last() {
            Some((offset, _)) => offset,
            None => 0,
        }
    }

    fn left_word(self, s: &str, steps: usize) -> Self {
        match s[..self].unicode_word_indices().rev().take(steps).last() {
            Some((offset, _)) => offset,
            None => 0,
        }
    }
}

/// Mutate a given string in-place, removing ASCII control characters and converting newlines,
/// carriage returns, and TABs to ASCII space.
pub fn normalize_prompt_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 event that modifies the prompt.
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PromptEvent {
    /// Move the cursor `usize` graphemes to the left.
    Left(usize),
    /// Move the cursor `usize` Unicode words to the left.
    WordLeft(usize),
    /// Move the cursor `usize` graphemes to the right.
    Right(usize),
    /// Move the cursor `usize` Unicode words to the right.
    WordRight(usize),
    /// Move the cursor to the start.
    ToStart,
    /// Move the cursor to the start.
    ToEnd,
    /// Delete `usize` graphemes immediately preceding the cursor.
    Backspace(usize),
    /// Delete `usize` graphemes immediately following the cursor.
    Delete(usize),
    /// Delete `usize` Unicode words immediately preceding the cursor.
    BackspaceWord(usize),
    /// Clear everything before the cursor.
    ClearBefore,
    /// Clear everything after the cursor.
    ClearAfter,
    /// Insert a character at the cursor position.
    Insert(char),
    /// Paste a string at the cursor position.
    Paste(String),
    /// Reset the prompt and move the cursor to the end.
    #[allow(unused)]
    Reset(String),
}

impl PromptEvent {
    /// Whether or not the event is a cursor movement that does not edit the prompt string.
    pub fn is_cursor_movement(&self) -> bool {
        matches!(
            &self,
            PromptEvent::Left(_)
                | PromptEvent::WordLeft(_)
                | PromptEvent::Right(_)
                | PromptEvent::WordRight(_)
                | PromptEvent::ToStart
                | PromptEvent::ToEnd
        )
    }
}

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

#[derive(Debug, Clone)]
pub struct PromptConfig {
    pub padding: u16,
}

impl Default for PromptConfig {
    fn default() -> Self {
        Self { padding: 2 }
    }
}

#[derive(Debug)]
pub struct Prompt {
    contents: String,
    offset: usize,
    screen_offset: u16,
    width: u16,
    config: PromptConfig,
}

impl Prompt {
    /// Create a new editable string with initial screen width and maximum padding.
    pub fn new(config: PromptConfig) -> Self {
        Self {
            contents: String::new(),
            offset: 0,
            screen_offset: 0,
            width: u16::MAX,
            config,
        }
    }

    pub fn padding(&self) -> u16 {
        self.config.padding.min(self.width.saturating_sub(1) / 2)
    }

    /// Whether or not the prompt is empty.
    pub fn is_empty(&self) -> bool {
        self.contents.is_empty()
    }

    /// 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) {
        if self.width == 0 {
            return ("", 0);
        }

        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 == usize::from(self.screen_offset) {
                                    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) {
        // TODO: this is not really correct, since it does not handle width 0 correctly.
        // but in practice, for the prompt this is quite rare; but should fix it at some point
        //
        // to witness in tests, set the width to 0 and then to some large value and the screen
        // offset will be incorrect
        //
        // this is also the reason that the prompt defaults to `u16::MAX`; and this should be fixed
        // as well when this is fixed.
        self.width = width;
        self.screen_offset = self.screen_offset.min(width - self.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();
        normalize_prompt_string(&mut self.contents);
        self.offset = self.contents.len();
        self.screen_offset = as_u16(self.contents.width()).min(self.width - self.padding());
    }

    /// Increase the screen offset by the provided width, without exceeding the maximum offset.
    fn right_by(&mut self, width: usize) {
        self.screen_offset = self
            .screen_offset
            .saturating_add(as_u16(width))
            .min(self.width - self.padding());
    }

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

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

    #[inline]
    fn left_by(&mut self, width: usize) {
        // 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();
                    let left_padding = self.padding();
                    if total_left_width >= left_padding as usize {
                        break left_padding;
                    }
                }
                None => {
                    break total_left_width as u16;
                }
            }
        };

        self.screen_offset = self
            .screen_offset
            .saturating_sub(as_u16(width))
            .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(n) => {
                let new_offset = self.offset.left(&self.contents, n);
                if new_offset != self.offset {
                    let step_width = self.contents[new_offset..self.offset].width();
                    self.offset = new_offset;
                    self.left_by(step_width);
                    true
                } else {
                    false
                }
            }
            CursorMovement::WordLeft(n) => {
                let new_offset = self.offset.left_word(&self.contents, n);
                if new_offset != self.offset {
                    let step_width = self.contents[new_offset..self.offset].width();
                    self.offset = new_offset;
                    self.left_by(step_width);
                    true
                } else {
                    false
                }
            }
            CursorMovement::Right(n) => {
                let new_offset = self.offset.right(&self.contents, n);
                if new_offset != self.offset {
                    let step_width = self.contents[self.offset..new_offset].width();
                    self.offset = new_offset;
                    self.right_by(step_width);
                    true
                } else {
                    false
                }
            }
            CursorMovement::WordRight(n) => {
                let new_offset = self.offset.right_word(&self.contents, n);
                if new_offset != self.offset {
                    let step_width = self.contents[self.offset..new_offset].width();
                    self.offset = new_offset;
                    self.right_by(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.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
                }
            }
        }
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct PromptStatus {
    pub needs_redraw: bool,
    pub contents_changed: bool,
}

impl Status for PromptStatus {
    fn needs_redraw(&self) -> bool {
        self.needs_redraw
    }
}

impl std::ops::BitOrAssign for PromptStatus {
    fn bitor_assign(&mut self, rhs: Self) {
        self.needs_redraw |= rhs.needs_redraw;
        self.contents_changed |= rhs.contents_changed;
    }
}

impl Component for Prompt {
    type Event = PromptEvent;

    type Status = PromptStatus;

    fn handle(&mut self, e: Self::Event) -> Self::Status {
        let mut contents_changed = false;

        let needs_redraw = match e {
            PromptEvent::Reset(s) => {
                self.set_prompt(s);
                true
            }
            PromptEvent::Left(n) => self.move_cursor(CursorMovement::Left(n)),
            PromptEvent::WordLeft(n) => self.move_cursor(CursorMovement::WordLeft(n)),
            PromptEvent::Right(n) => self.move_cursor(CursorMovement::Right(n)),
            PromptEvent::WordRight(n) => self.move_cursor(CursorMovement::WordRight(n)),
            PromptEvent::ToStart => self.move_cursor(CursorMovement::ToStart),
            PromptEvent::ToEnd => self.move_cursor(CursorMovement::ToEnd),
            PromptEvent::Insert(ch) => {
                if let Some((ch, w)) = normalize_char(ch) {
                    contents_changed = true;
                    self.insert_char(ch, w);
                    true
                } else {
                    false
                }
            }
            PromptEvent::Paste(mut s) => {
                normalize_prompt_string(&mut s);
                if !s.is_empty() {
                    contents_changed = true;
                    self.insert(&s);
                    true
                } else {
                    false
                }
            }
            PromptEvent::Backspace(n) => {
                let delete_until = self.offset;
                if self.move_cursor(CursorMovement::Left(n)) {
                    self.contents.replace_range(self.offset..delete_until, "");
                    contents_changed = true;
                    true
                } else {
                    false
                }
            }
            PromptEvent::BackspaceWord(n) => {
                let delete_until = self.offset;
                if self.move_cursor(CursorMovement::WordLeft(n)) {
                    self.contents.replace_range(self.offset..delete_until, "");
                    contents_changed = true;
                    true
                } else {
                    false
                }
            }
            PromptEvent::ClearBefore => {
                if self.offset == 0 {
                    false
                } else {
                    self.contents.replace_range(..self.offset, "");
                    self.offset = 0;
                    self.screen_offset = 0;
                    contents_changed = true;
                    true
                }
            }
            PromptEvent::Delete(n) => {
                let new_offset = self.offset.right(&self.contents, n);
                if new_offset != self.offset {
                    self.contents.replace_range(self.offset..new_offset, "");
                    contents_changed = true;
                    true
                } else {
                    false
                }
            }
            PromptEvent::ClearAfter => {
                if self.offset == self.contents.len() {
                    false
                } else {
                    self.contents.truncate(self.offset);
                    contents_changed = true;
                    true
                }
            }
        };

        Self::Status {
            needs_redraw,
            contents_changed,
        }
    }

    fn draw<W: std::io::Write + ?Sized>(
        &mut self,
        width: u16,
        _height: u16,
        writer: &mut W,
    ) -> std::io::Result<()> {
        use crossterm::{
            cursor::MoveRight,
            style::Print,
            terminal::{Clear, ClearType},
            QueueableCommand,
        };

        writer.queue(Print("> "))?;

        if let Some(width) = width.checked_sub(2) {
            if width != self.width {
                self.resize(width);
            }

            let (contents, shift) = self.view();

            if shift != 0 {
                writer.queue(MoveRight(shift))?;
            }

            writer
                .queue(Print(contents))?
                .queue(Clear(ClearType::UntilNewLine))?;
        }

        Ok(())
    }
}