yuru-tui 0.2.2

Terminal user interface for Yuru
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
use yuru_core::ScoredCandidate;

/// What the selection points at, independently of where that lands in a result list.
///
/// A result list is replaced wholesale every time a search finishes, so a bare row index
/// stops meaning the same row the moment that happens. Recording *what* is selected
/// rather than *where* keeps the meaning across the replacement.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SelectionTarget {
    /// No row has been chosen since the query last changed, so the selection follows the
    /// top of whatever the live search returns. This is the state right after typing.
    Top,
    /// A specific candidate the user moved to, identified by id and never by position.
    Row(usize),
}

#[derive(Clone, Debug, Eq, PartialEq)]
/// Mutable query, cursor, selection, and marking state.
pub struct TuiState {
    query: String,
    cursor: usize,
    /// Row the cursor is drawn on. This is a cache of where [`Self::target`] currently
    /// sits, maintained by [`Self::reselect`] and by the selection-moving actions; it is
    /// never the authority on what is selected.
    selected: usize,
    target: SelectionTarget,
    /// Marked candidate ids, in the order they were marked. Marks are identities too, so
    /// they survive a query change; the order is the one fzf prints them in. A list
    /// rather than a set because it is only as long as the user has pressed the mark
    /// key, and the order is part of the contract.
    marked: Vec<usize>,
}

impl TuiState {
    /// Creates TUI state with the given initial query.
    pub fn new(query: impl Into<String>) -> Self {
        let query = query.into();
        let cursor = query.len();
        Self {
            query,
            cursor,
            selected: 0,
            target: SelectionTarget::Top,
            marked: Vec::new(),
        }
    }

    /// Returns the current query text.
    pub fn query(&self) -> &str {
        &self.query
    }

    /// Returns the byte index of the query cursor.
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    /// Returns the selected result index, for drawing the cursor.
    pub fn selected(&self) -> usize {
        self.selected
    }

    /// Returns what the selection points at.
    ///
    /// This is what an accept has to be resolved against: it stays meaningful while a
    /// search is outstanding, whereas [`Self::selected`] does not.
    pub fn target(&self) -> SelectionTarget {
        self.target
    }

    /// Returns the marked candidate ids, in the order they were marked.
    pub fn marked(&self) -> &[usize] {
        &self.marked
    }

    /// Returns whether `id` is marked.
    pub fn is_marked(&self, id: usize) -> bool {
        self.marked.contains(&id)
    }

    /// Applies a state action against the result list the user is looking at.
    ///
    /// `results` is needed rather than just its length because every selection move
    /// re-anchors [`Self::target`] to the row it lands on.
    pub fn apply(&mut self, action: TuiAction, results: &[ScoredCandidate], cycle: bool) {
        self.apply_with_results(action, results, cycle, false, None);
    }

    pub(crate) fn apply_with_results(
        &mut self,
        action: TuiAction,
        results: &[ScoredCandidate],
        cycle: bool,
        multi: bool,
        multi_limit: Option<usize>,
    ) {
        let result_len = results.len();
        match action {
            TuiAction::Insert(ch) => self.insert(ch),
            TuiAction::Backspace => self.backspace(),
            TuiAction::Delete => self.delete(),
            TuiAction::DeleteOrExit => self.delete(),
            TuiAction::DeleteToEnd => self.delete_to_end(),
            TuiAction::DeleteWord => self.delete_word(),
            TuiAction::ClearQuery => self.clear_query(),
            TuiAction::MoveCursorLeft => self.move_cursor_left(),
            TuiAction::MoveCursorRight => self.move_cursor_right(),
            TuiAction::MoveCursorStart => self.cursor = 0,
            TuiAction::MoveCursorEnd => self.cursor = self.query.len(),
            TuiAction::MoveCursorWordLeft => self.move_cursor_word_left(),
            TuiAction::MoveCursorWordRight => self.move_cursor_word_right(),
            TuiAction::MoveSelectionUp => {
                self.move_selection_up(result_len, cycle);
                self.anchor_to_selected(results);
            }
            TuiAction::MoveSelectionDown => {
                self.move_selection_down(result_len, cycle);
                self.anchor_to_selected(results);
            }
            TuiAction::MoveSelectionFirst => {
                self.selected = 0;
                self.anchor_to_selected(results);
            }
            TuiAction::MoveSelectionLast => {
                self.selected = result_len.saturating_sub(1);
                self.anchor_to_selected(results);
            }
            TuiAction::PageUp(rows) => {
                self.selected = self.selected.saturating_sub(rows.max(1));
                self.anchor_to_selected(results);
            }
            TuiAction::PageDown(rows) => {
                if result_len > 0 {
                    self.selected = (self.selected + rows.max(1)).min(result_len - 1);
                }
                self.anchor_to_selected(results);
            }
            TuiAction::ToggleMark => {
                self.toggle_selected_mark(results, multi, multi_limit);
            }
            TuiAction::ToggleMarkAndDown => {
                self.toggle_selected_mark(results, multi, multi_limit);
                self.move_selection_down(result_len, cycle);
                self.anchor_to_selected(results);
            }
            TuiAction::ToggleMarkAndUp => {
                self.toggle_selected_mark(results, multi, multi_limit);
                self.move_selection_up(result_len, cycle);
                self.anchor_to_selected(results);
            }
            TuiAction::PreviewUp
            | TuiAction::PreviewDown
            | TuiAction::PreviewPageUp(_)
            | TuiAction::PreviewPageDown(_)
            | TuiAction::PreviewTop
            | TuiAction::PreviewBottom => {}
        }
    }

    /// Re-resolves the selection against a freshly landed result list.
    ///
    /// This is the only place a result list replacement is allowed to move the cursor.
    /// A [`SelectionTarget::Row`] that is still present keeps the selection on that same
    /// candidate wherever it now sits. A row that is gone resets to the top and to
    /// following the top, which is what fzf does and the least surprising of the
    /// alternatives; an accept that was already committed against the lost row is
    /// resolved from its own captured target and so is never redirected here.
    pub(crate) fn reselect(&mut self, results: &[ScoredCandidate]) {
        match self.target {
            SelectionTarget::Top => self.selected = 0,
            SelectionTarget::Row(id) => match results.iter().position(|row| row.id == id) {
                Some(index) => self.selected = index,
                None => self.reset_selection(),
            },
        }
    }

    /// Resolves `target` and the marks against `results` into accepted candidate ids.
    ///
    /// `target` is passed in rather than read from `self` because an accept made while a
    /// search was outstanding has to resolve the selection as it was when the key was
    /// pressed, not as it is once the replacement rows arrive.
    pub(crate) fn accepted_ids(
        &self,
        target: SelectionTarget,
        results: &[ScoredCandidate],
        multi: bool,
    ) -> Vec<usize> {
        if multi && !self.marked.is_empty() {
            return self.marked.clone();
        }

        match target {
            // The row the user chose has to still be in the live results; if it is not,
            // there is nothing to accept, and picking whatever took its place would
            // return a row the user never selected.
            SelectionTarget::Row(id) => {
                if results.iter().any(|result| result.id == id) {
                    vec![id]
                } else {
                    Vec::new()
                }
            }
            SelectionTarget::Top => results
                .first()
                .map(|result| vec![result.id])
                .unwrap_or_default(),
        }
    }

    /// Points the selection back at the top of the list and at following the top.
    fn reset_selection(&mut self) {
        self.selected = 0;
        self.target = SelectionTarget::Top;
    }

    /// Binds the target to the row the cursor now sits on.
    fn anchor_to_selected(&mut self, results: &[ScoredCandidate]) {
        if self.selected >= results.len() {
            self.selected = results.len().saturating_sub(1);
        }
        self.target = match results.get(self.selected) {
            Some(result) => SelectionTarget::Row(result.id),
            None => SelectionTarget::Top,
        };
    }

    fn toggle_selected_mark(
        &mut self,
        results: &[ScoredCandidate],
        multi: bool,
        multi_limit: Option<usize>,
    ) {
        if !multi {
            return;
        }
        let Some(result) = results.get(self.selected) else {
            return;
        };
        if self.marked.contains(&result.id) {
            self.marked.retain(|marked| *marked != result.id);
        } else if multi_limit.is_none_or(|limit| self.marked.len() < limit) {
            self.marked.push(result.id);
        }
    }

    fn insert(&mut self, ch: char) {
        self.query.insert(self.cursor, ch);
        self.cursor += ch.len_utf8();
        self.reset_selection();
    }

    fn backspace(&mut self) {
        if self.cursor == 0 {
            return;
        }
        let previous = previous_boundary(&self.query, self.cursor);
        self.query.drain(previous..self.cursor);
        self.cursor = previous;
        self.reset_selection();
    }

    fn delete(&mut self) {
        if self.cursor == self.query.len() {
            return;
        }
        let next = next_boundary(&self.query, self.cursor);
        self.query.drain(self.cursor..next);
        self.reset_selection();
    }

    fn delete_to_end(&mut self) {
        self.query.truncate(self.cursor);
        self.reset_selection();
    }

    fn delete_word(&mut self) {
        if self.cursor == 0 {
            return;
        }
        let word_start = previous_word_boundary(&self.query, self.cursor);
        self.query.drain(word_start..self.cursor);
        self.cursor = word_start;
        self.reset_selection();
    }

    fn clear_query(&mut self) {
        self.query.clear();
        self.cursor = 0;
        self.reset_selection();
    }

    fn move_cursor_left(&mut self) {
        self.cursor = previous_boundary(&self.query, self.cursor);
    }

    fn move_cursor_right(&mut self) {
        self.cursor = next_boundary(&self.query, self.cursor);
    }

    fn move_cursor_word_left(&mut self) {
        self.cursor = previous_word_boundary(&self.query, self.cursor);
    }

    fn move_cursor_word_right(&mut self) {
        self.cursor = next_word_boundary(&self.query, self.cursor);
    }

    fn move_selection_up(&mut self, result_len: usize, cycle: bool) {
        if result_len == 0 {
            self.selected = 0;
        } else if self.selected == 0 {
            self.selected = if cycle { result_len - 1 } else { 0 };
        } else {
            self.selected -= 1;
        }
    }

    fn move_selection_down(&mut self, result_len: usize, cycle: bool) {
        if result_len == 0 {
            self.selected = 0;
        } else if self.selected + 1 >= result_len {
            self.selected = if cycle { 0 } else { result_len - 1 };
        } else {
            self.selected += 1;
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// State transition used by the TUI event loop.
pub enum TuiAction {
    /// Insert a character at the query cursor.
    Insert(char),
    /// Delete the character before the cursor.
    Backspace,
    /// Delete the character at the cursor.
    Delete,
    /// Delete from cursor to end of line.
    DeleteToEnd,
    /// Delete word before cursor.
    DeleteWord,
    /// Clear the query text.
    ClearQuery,
    /// Move the query cursor left.
    MoveCursorLeft,
    /// Move the query cursor right.
    MoveCursorRight,
    /// Move the query cursor to the start.
    MoveCursorStart,
    /// Move the query cursor to the end.
    MoveCursorEnd,
    /// Move the query cursor to the start of the previous word.
    MoveCursorWordLeft,
    /// Move the query cursor to the end of the next word.
    MoveCursorWordRight,
    /// Move the selected row up.
    MoveSelectionUp,
    /// Move the selected row down.
    MoveSelectionDown,
    /// Move to the first row.
    MoveSelectionFirst,
    /// Move to the last row.
    MoveSelectionLast,
    /// Move selection up by the given number of rows.
    PageUp(usize),
    /// Move selection down by the given number of rows.
    PageDown(usize),
    /// Toggle the selected row mark.
    ToggleMark,
    /// Toggle the selected row mark and move down.
    ToggleMarkAndDown,
    /// Toggle the selected row mark and move up.
    ToggleMarkAndUp,
    /// Scroll preview up.
    PreviewUp,
    /// Scroll preview down.
    PreviewDown,
    /// Scroll preview up by the given number of rows.
    PreviewPageUp(usize),
    /// Scroll preview down by the given number of rows.
    PreviewPageDown(usize),
    /// Scroll preview to the top.
    PreviewTop,
    /// Scroll preview to the bottom.
    PreviewBottom,
    /// Delete character and exit if query becomes empty (Ctrl+D).
    DeleteOrExit,
}

fn previous_boundary(text: &str, cursor: usize) -> usize {
    text[..cursor]
        .char_indices()
        .next_back()
        .map(|(index, _)| index)
        .unwrap_or(0)
}

fn next_boundary(text: &str, cursor: usize) -> usize {
    text[cursor..]
        .char_indices()
        .nth(1)
        .map(|(index, _)| cursor + index)
        .unwrap_or(text.len())
}

fn previous_word_boundary(text: &str, cursor: usize) -> usize {
    let mut iter = text[..cursor].char_indices().rev().peekable();

    // Skip any trailing boundary characters (cursor may sit right after whitespace).
    while let Some(&(_, ch)) = iter.peek() {
        if !is_word_boundary(ch) {
            break;
        }
        iter.next();
    }

    // Skip word characters; the first boundary we peek at marks the word start.
    while let Some(&(index, ch)) = iter.peek() {
        if is_word_boundary(ch) {
            return index + ch.len_utf8();
        }
        iter.next();
    }

    0
}

fn next_word_boundary(text: &str, cursor: usize) -> usize {
    let mut iter = text[cursor..].char_indices().peekable();

    let first_is_word = iter
        .peek()
        .map(|(_, ch)| !is_word_boundary(*ch))
        .unwrap_or(false);

    if first_is_word {
        return iter
            .find(|(_, ch)| is_word_boundary(*ch))
            .map(|(index, _)| cursor + index)
            .unwrap_or(text.len());
    }

    for (_, ch) in iter.by_ref() {
        if !is_word_boundary(ch) {
            return iter
                .find(|(_, next_ch)| is_word_boundary(*next_ch))
                .map(|(index, _)| cursor + index)
                .unwrap_or(text.len());
        }
    }

    text.len()
}

fn is_word_boundary(ch: char) -> bool {
    ch.is_whitespace() || ch == '/' || ch == '-' || ch == '_' || ch == '.'
}