tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
use crate::text::edit::{TextEdit, TextEditEvent, TextEditFields, TextEditKind, TextTarget};
use crate::utils::text::clamp_cursor;
use crate::utils::text::{
    next_char_boundary, prev_char_boundary, word_boundary_left, word_boundary_right,
};
use undo::Record;

const DEFAULT_HISTORY_LIMIT: usize = 1000;

type CoreEdit = TextEdit<TextBufferCore>;

/// Shared text buffer backing both `TextInput` (single-line) and `TextEditor` (multi-line).
///
/// Owns the UTF-8 text, byte-index cursor, optional selection anchor, undo/redo history,
/// and last-edit event. All cursor-movement, selection, editing, and history operations
/// that are identical across single-line and multi-line modes live here.
#[derive(Clone, Debug)]
pub struct TextBufferCore {
    pub(crate) text: String,
    pub(crate) cursor: usize,
    pub(crate) anchor: Option<usize>,
    pub(crate) history: Record<CoreEdit>,
    pub(crate) last_edit: Option<TextEditEvent>,
}

impl Default for TextBufferCore {
    fn default() -> Self {
        Self {
            text: String::new(),
            cursor: 0,
            anchor: None,
            history: Self::fresh_history(),
            last_edit: None,
        }
    }
}

impl PartialEq for TextBufferCore {
    fn eq(&self, other: &Self) -> bool {
        self.text == other.text && self.cursor == other.cursor && self.anchor == other.anchor
    }
}

impl Eq for TextBufferCore {}

impl TextTarget for TextBufferCore {
    fn with_parts_mut<R>(
        &mut self,
        f: impl FnOnce(&mut String, &mut usize, &mut Option<usize>) -> R,
    ) -> R {
        f(&mut self.text, &mut self.cursor, &mut self.anchor)
    }
}

impl TextBufferCore {
    pub(crate) fn fresh_history() -> Record<CoreEdit> {
        Record::builder().limit(DEFAULT_HISTORY_LIMIT).build()
    }

    // ── Accessors ────────────────────────────────────────────────────────

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

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

    /// Return the selection anchor, if any.
    pub fn anchor(&self) -> Option<usize> {
        self.anchor
    }

    /// Return the selection range as (start, end) byte indices, if there is a selection.
    pub fn selection(&self) -> Option<(usize, usize)> {
        self.anchor.map(|anchor| {
            let anchor = clamp_cursor(&self.text, anchor);
            let cursor = clamp_cursor(&self.text, self.cursor);
            if anchor <= cursor {
                (anchor, cursor)
            } else {
                (cursor, anchor)
            }
        })
    }

    /// Return the selected text, if any.
    pub fn selected_text(&self) -> Option<&str> {
        self.selection().map(|(start, end)| &self.text[start..end])
    }

    // ── History ──────────────────────────────────────────────────────────

    /// Returns true if there is something to undo.
    pub fn can_undo(&self) -> bool {
        self.history.can_undo()
    }

    /// Returns true if there is something to redo.
    pub fn can_redo(&self) -> bool {
        self.history.can_redo()
    }

    /// Undo the most recent edit.
    pub fn undo(&mut self) -> bool {
        let mut history = std::mem::take(&mut self.history);
        let changed = history.undo(self).is_some();
        self.history = history;
        changed
    }

    /// Redo the most recently undone edit.
    pub fn redo(&mut self) -> bool {
        let mut history = std::mem::take(&mut self.history);
        let changed = history.redo(self).is_some();
        self.history = history;
        changed
    }

    /// Clear all undo/redo history without changing the text.
    pub fn clear_history(&mut self) {
        self.history.clear();
    }

    pub(crate) fn reset_history(&mut self) {
        self.history = Self::fresh_history();
    }

    // ── Sync / setters ───────────────────────────────────────────────────

    pub(crate) fn sync_from(&mut self, text: &str, cursor: usize, anchor: Option<usize>) {
        let text_changed = self.text != text;
        if text_changed {
            self.text = text.to_string();
        }
        self.cursor = clamp_cursor(&self.text, cursor);
        self.anchor = anchor.map(|a| clamp_cursor(&self.text, a));
        if text_changed {
            self.reset_history();
        }
    }

    pub(crate) fn sync_external_edit_from(
        &mut self,
        text: &str,
        cursor_before: usize,
        anchor_before: Option<usize>,
        cursor_after: usize,
        anchor_after: Option<usize>,
    ) {
        let deleted = self.text.clone();
        let cursor_before = clamp_cursor(&deleted, cursor_before);
        let anchor_before = anchor_before.map(|anchor| clamp_cursor(&deleted, anchor));
        let cursor_after = clamp_cursor(text, cursor_after);
        let anchor_after = anchor_after.map(|anchor| clamp_cursor(text, anchor));
        let edit = TextEdit::new(TextEditFields {
            start: 0,
            deleted,
            inserted: text.to_string(),
            cursor_before,
            anchor_before,
            cursor_after,
            anchor_after,
            kind: TextEditKind::Replace,
        });
        self.record_edit(edit);
        self.last_edit = None;
    }

    /// Set the text, clamping the cursor and clearing selection.
    pub fn set_text(&mut self, text: impl Into<String>) {
        self.text = text.into();
        self.cursor = clamp_cursor(&self.text, self.cursor);
        self.anchor = self.anchor.map(|a| clamp_cursor(&self.text, a));
    }

    /// Set the cursor position (byte index), clearing selection.
    pub fn set_cursor(&mut self, cursor: usize) {
        self.cursor = clamp_cursor(&self.text, cursor);
        self.anchor = None;
    }

    /// Set the cursor position without clearing selection.
    pub fn set_cursor_keep_anchor(&mut self, cursor: usize) {
        self.cursor = clamp_cursor(&self.text, cursor);
    }

    /// Set the selection anchor.
    pub fn set_anchor(&mut self, anchor: Option<usize>) {
        self.anchor = anchor.map(|a| clamp_cursor(&self.text, a));
    }

    // ── Selection ────────────────────────────────────────────────────────

    /// Select all text.
    pub fn select_all(&mut self) -> bool {
        if self.text.is_empty() {
            return false;
        }
        self.anchor = Some(0);
        self.cursor = self.text.len();
        true
    }

    /// Clear selection without moving cursor.
    pub fn clear_selection(&mut self) {
        self.anchor = None;
    }

    /// If selection is empty (anchor == cursor), clear the anchor.
    pub(crate) fn collapse_empty_selection(&mut self) {
        if self.anchor == Some(self.cursor) {
            self.anchor = None;
        }
    }

    pub(crate) fn normalize_cursor_and_anchor(&mut self) {
        self.cursor = clamp_cursor(&self.text, self.cursor);
        self.anchor = self.anchor.map(|anchor| clamp_cursor(&self.text, anchor));
    }

    // ── Edit recording ───────────────────────────────────────────────────

    pub(crate) fn record_edit(&mut self, edit: CoreEdit) {
        self.last_edit = Some(TextEditEvent::from(&edit));
        let mut history = std::mem::take(&mut self.history);
        history.edit(self, edit);
        self.history = history;
    }

    pub(crate) fn take_last_edit(&mut self) -> Option<TextEditEvent> {
        self.last_edit.take()
    }

    pub(crate) fn replace_range(
        &mut self,
        start: usize,
        end: usize,
        inserted: &str,
        kind: TextEditKind,
    ) -> bool {
        self.normalize_cursor_and_anchor();
        let start = clamp_cursor(&self.text, start);
        let end = clamp_cursor(&self.text, end);
        let (start, end) = if start <= end {
            (start, end)
        } else {
            (end, start)
        };

        if start == end && inserted.is_empty() {
            return false;
        }

        let deleted = self.text[start..end].to_string();
        let cursor_after = start.saturating_add(inserted.len());
        let edit = TextEdit::new(TextEditFields {
            start,
            deleted,
            inserted: inserted.to_owned(),
            cursor_before: self.cursor,
            anchor_before: self.anchor,
            cursor_after,
            anchor_after: None,
            kind,
        });
        self.record_edit(edit);
        true
    }

    pub(crate) fn clear_preserving_history(&mut self) -> bool {
        if self.text.is_empty() {
            let changed = self.cursor != 0 || self.anchor.is_some();
            self.cursor = 0;
            self.anchor = None;
            return changed;
        }

        let len = self.text.len();
        self.replace_range(0, len, "", TextEditKind::Replace)
    }

    /// Delete the current selection if any, returning true if something was deleted.
    pub(crate) fn delete_selection(&mut self) -> bool {
        if let Some((start, end)) = self.selection() {
            self.replace_range(start, end, "", TextEditKind::Replace)
        } else {
            false
        }
    }

    // ── Character-level editing ──────────────────────────────────────────

    /// Insert a character at the cursor, replacing any selection.
    pub fn insert_char(&mut self, c: char) -> bool {
        let (start, end, kind) = if let Some((start, end)) = self.selection() {
            (start, end, TextEditKind::Replace)
        } else {
            (self.cursor, self.cursor, TextEditKind::Insert)
        };
        let mut buf = [0u8; 4];
        let s = c.encode_utf8(&mut buf);
        self.replace_range(start, end, s, kind)
    }

    /// Delete the previous character or selection.
    pub fn backspace(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        if self.delete_selection() {
            return true;
        }
        if self.cursor == 0 {
            return false;
        }
        let start = prev_char_boundary(&self.text, self.cursor);
        self.replace_range(start, self.cursor, "", TextEditKind::DeleteBackspace)
    }

    /// Delete the character at the cursor or selection.
    pub fn delete(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        if self.delete_selection() {
            return true;
        }
        if self.cursor >= self.text.len() {
            return false;
        }
        let end = next_char_boundary(&self.text, self.cursor);
        self.replace_range(self.cursor, end, "", TextEditKind::DeleteForward)
    }

    /// Delete the previous word.
    pub fn delete_word_left(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        if self.delete_selection() {
            return true;
        }
        if self.cursor == 0 {
            return false;
        }
        let target = word_boundary_left(&self.text, self.cursor);
        self.replace_range(target, self.cursor, "", TextEditKind::DeleteBackspace)
    }

    /// Delete the next word.
    pub fn delete_word_right(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        if self.delete_selection() {
            return true;
        }
        if self.cursor >= self.text.len() {
            return false;
        }
        let target = word_boundary_right(&self.text, self.cursor);
        self.replace_range(self.cursor, target, "", TextEditKind::DeleteForward)
    }

    // ── Cursor movement ──────────────────────────────────────────────────

    /// Move one character left, clearing selection.
    pub fn move_left(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        // If there's a selection, move to the start of it
        if let Some((start, _)) = self.selection() {
            self.cursor = start;
            self.anchor = None;
            return true;
        }
        if self.cursor == 0 {
            return false;
        }
        let new = prev_char_boundary(&self.text, self.cursor);
        if new == self.cursor {
            return false;
        }
        self.cursor = new;
        true
    }

    /// Move one character right, clearing selection.
    pub fn move_right(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        // If there's a selection, move to the end of it
        if let Some((_, end)) = self.selection() {
            self.cursor = end;
            self.anchor = None;
            return true;
        }
        if self.cursor >= self.text.len() {
            return false;
        }
        let new = next_char_boundary(&self.text, self.cursor);
        if new == self.cursor {
            return false;
        }
        self.cursor = new;
        true
    }

    /// Extend selection one character left.
    pub fn select_left(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        if self.cursor == 0 {
            return false;
        }
        if self.anchor.is_none() {
            self.anchor = Some(self.cursor);
        }
        let new = prev_char_boundary(&self.text, self.cursor);
        if new == self.cursor {
            return false;
        }
        self.cursor = new;
        self.collapse_empty_selection();
        true
    }

    /// Extend selection one character right.
    pub fn select_right(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        if self.cursor >= self.text.len() {
            return false;
        }
        if self.anchor.is_none() {
            self.anchor = Some(self.cursor);
        }
        let new = next_char_boundary(&self.text, self.cursor);
        if new == self.cursor {
            return false;
        }
        self.cursor = new;
        self.collapse_empty_selection();
        true
    }

    /// Move to the beginning of the previous word, clearing selection.
    pub fn move_word_left(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        self.anchor = None;
        let old = self.cursor;
        self.cursor = word_boundary_left(&self.text, self.cursor);
        self.cursor != old
    }

    /// Move to the end of the next word, clearing selection.
    pub fn move_word_right(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        self.anchor = None;
        let old = self.cursor;
        self.cursor = word_boundary_right(&self.text, self.cursor);
        self.cursor != old
    }

    /// Extend selection to the beginning of the previous word.
    pub fn select_word_left(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        let old = self.cursor;
        if self.anchor.is_none() {
            self.anchor = Some(self.cursor);
        }
        self.cursor = word_boundary_left(&self.text, self.cursor);
        self.collapse_empty_selection();
        self.cursor != old
    }

    /// Extend selection to the end of the next word.
    pub fn select_word_right(&mut self) -> bool {
        self.normalize_cursor_and_anchor();
        let old = self.cursor;
        if self.anchor.is_none() {
            self.anchor = Some(self.cursor);
        }
        self.cursor = word_boundary_right(&self.text, self.cursor);
        self.collapse_empty_selection();
        self.cursor != old
    }
}