tinsel 1.1.0

Clean terminal output with a little sparkle: live step lists, spinners, tables, trees, prompts and full-screen apps.
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
//! Several lines of text to type into, inside an app.

use std::cell::Cell;

use super::field::{caret, edit_line};
use super::{Event, Frame, Key, Rect, Widget};
use crate::style::palette::{BERRY, MUTED, SOFT, TEXT, TRACK};
use crate::style::{italic, mix, paint};
use crate::text::char_width;

/// A place in a [`TextArea`]'s text, from [`TextArea::cursor`]: the line, and the character
/// in it, both counted from 0. (Characters, not columns: a wide one counts once.)
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Position {
    /// The line.
    pub row: usize,
    /// The character in the line.
    pub column: usize,
}

/// A box to type several lines into: a message, notes, a description. Like [`TextInput`](super::TextInput),
/// keep one in your app, pass it every event while it has focus, and read what's typed with
/// [`value`](TextArea::value). Enter starts a new line, pasted text keeps its line breaks,
/// long lines wrap, and the box scrolls to keep the cursor in view.
///
/// ```
/// use tinsel::app::{Event, Frame, Key, Position, TextArea};
///
/// tinsel::set_color_mode(tinsel::ColorMode::None);
/// let mut notes = TextArea::new().placeholder("what changed?");
/// notes.focus(true);
/// notes.handle(&Event::Paste("fix the build\n".into()));
/// for c in "and the docs".chars() {
///     notes.handle(&Event::Key(Key::Char(c)));
/// }
/// assert_eq!(notes.value(), "fix the build\nand the docs");
/// assert_eq!(notes.cursor(), Position { row: 1, column: 12 });
/// ```
#[derive(Clone, Debug)]
pub struct TextArea {
    lines: Vec<Vec<char>>,
    /// The cursor: a line, and a character in it.
    row: usize,
    col: usize,
    /// The column ↑↓ aim for, so passing a short line doesn't lose it.
    goal: Option<usize>,
    placeholder: String,
    focused: bool,
    /// The first visual row showing, kept between drawings so the view only moves when the
    /// cursor would leave it.
    top: Cell<usize>,
}

impl Default for TextArea {
    fn default() -> Self {
        TextArea::new()
    }
}

impl TextArea {
    /// An empty box, without focus.
    pub fn new() -> Self {
        TextArea {
            lines: vec![Vec::new()],
            row: 0,
            col: 0,
            goal: None,
            placeholder: String::new(),
            focused: false,
            top: Cell::new(0),
        }
    }

    /// Quiet text shown while the box is empty, saying what goes in it.
    #[must_use]
    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
        self.placeholder = text.into();
        self
    }

    /// What's been typed, its lines joined with `\n`.
    pub fn value(&self) -> String {
        let lines: Vec<String> = self.lines.iter().map(|l| l.iter().collect()).collect();
        lines.join("\n")
    }

    /// Replace what's typed, with the cursor at the end.
    pub fn set_value(&mut self, value: &str) {
        self.lines = vec![Vec::new()];
        (self.row, self.col) = (0, 0);
        self.insert(value);
    }

    /// Empty the box.
    pub fn clear(&mut self) {
        self.set_value("");
    }

    /// Where the cursor is: the line, and the character in it, both counted from 0.
    pub fn cursor(&self) -> Position {
        Position {
            row: self.row,
            column: self.col,
        }
    }

    /// Whether this is where keys go. Only a focused box takes keys or shows its cursor.
    pub fn focus(&mut self, focused: bool) {
        self.focused = focused;
    }

    /// Whether the box has focus.
    pub fn is_focused(&self) -> bool {
        self.focused
    }

    /// Type text at the cursor, starting new lines at its line breaks. Tabs become four
    /// spaces, and other control characters are left out.
    fn insert(&mut self, text: &str) {
        for c in text.chars() {
            match c {
                '\n' => self.split_line(),
                '\t' => (0..4).for_each(|_| self.put(' ')),
                c if c.is_control() => {}
                c => self.put(c),
            }
        }
    }

    fn put(&mut self, c: char) {
        self.lines[self.row].insert(self.col, c);
        self.col += 1;
    }

    fn split_line(&mut self) {
        let rest = self.lines[self.row].split_off(self.col);
        self.row += 1;
        self.col = 0;
        self.lines.insert(self.row, rest);
    }

    /// Move to `row`, as near the goal column as that line allows.
    fn go_to_row(&mut self, row: usize) {
        let goal = *self.goal.get_or_insert(self.col);
        self.row = row;
        self.col = goal.min(self.lines[row].len());
    }

    /// Type, delete or move the cursor, if the box has focus and the event is one it uses:
    /// characters and pastes, Enter for a new line, Backspace and Delete (joining lines at
    /// their ends), ←→↑↓, Page Up/Down ten lines, Home and End (or Ctrl-A and Ctrl-E) for
    /// the line's ends, Ctrl-U and Ctrl-K to clear the line before or after the cursor, and
    /// Ctrl-W or Alt-Backspace to delete a word, and Alt or Ctrl with ←→ to move by words.
    /// Returns whether it used the event, so Esc and Tab are left
    /// for the app, to leave the box with.
    ///
    /// ```
    /// use tinsel::app::{Event, Key, TextArea};
    ///
    /// let mut text = TextArea::new();
    /// text.focus(true);
    /// text.set_value("one\ntwo");
    /// text.handle(&Event::Key(Key::Home));
    /// text.handle(&Event::Key(Key::Backspace));
    /// assert_eq!(text.value(), "onetwo");
    /// assert!(!text.handle(&Event::Key(Key::Esc)));
    /// ```
    pub fn handle(&mut self, event: &Event) -> bool {
        if !self.focused {
            return false;
        }
        let key = match event {
            Event::Key(key) => *key,
            Event::Paste(text) => {
                self.insert(text);
                self.goal = None;
                return true;
            }
            _ => return false,
        };
        let (row, col) = (self.row, self.col);
        let last = self.lines.len() - 1;
        match key {
            Key::Up if row > 0 => return self.moved_to_row(row - 1),
            Key::Down if row < last => return self.moved_to_row(row + 1),
            Key::PageUp => return self.moved_to_row(row.saturating_sub(10)),
            Key::PageDown => return self.moved_to_row((row + 10).min(last)),
            Key::Up => self.col = 0,
            Key::Down => self.col = self.lines[row].len(),
            Key::Char(c) => self.put(c),
            Key::Enter => self.split_line(),
            Key::Backspace if col > 0 => {
                self.lines[row].remove(col - 1);
                self.col -= 1;
            }
            Key::Backspace if row > 0 => {
                let line = self.lines.remove(row);
                self.row -= 1;
                self.col = self.lines[self.row].len();
                self.lines[self.row].extend(line);
            }
            Key::Delete if col < self.lines[row].len() => {
                self.lines[row].remove(col);
            }
            Key::Delete if row < last => {
                let next = self.lines.remove(row + 1);
                self.lines[row].extend(next);
            }
            Key::Left if col > 0 => self.col -= 1,
            Key::Left if row > 0 => {
                self.row -= 1;
                self.col = self.lines[self.row].len();
            }
            Key::Right if col < self.lines[row].len() => self.col += 1,
            Key::Right if row < last => (self.row, self.col) = (row + 1, 0),
            key if edit_line(&mut self.lines[row], &mut self.col, key, |c| !c.is_whitespace()) => {}
            Key::Backspace | Key::Delete | Key::Left | Key::Right => {}
            _ => return false,
        }
        self.goal = None;
        true
    }

    fn moved_to_row(&mut self, row: usize) -> bool {
        self.go_to_row(row);
        true
    }

    /// The lines as drawn `width` wide, broken by characters, and the visual row and column
    /// the cursor is at. A cursor at the end of a full row goes to the start of the next.
    fn layout(&self, width: usize) -> (Vec<String>, (usize, usize)) {
        let width = width.max(1);
        let mut rows = Vec::new();
        let mut cursor = (0, 0);
        for (i, line) in self.lines.iter().enumerate() {
            let (mut row, mut x) = (String::new(), 0);
            for (j, &c) in line.iter().chain([' '].iter()).enumerate() {
                let w = char_width(c);
                if x + w > width && x > 0 {
                    rows.push(std::mem::take(&mut row));
                    x = 0;
                }
                if (i, j) == (self.row, self.col) {
                    cursor = (rows.len(), x);
                }
                if j < line.len() {
                    row.push(c);
                    x += w;
                }
            }
            rows.push(row);
        }
        (rows, cursor)
    }
}

impl Widget for TextArea {
    fn render(&self, area: Rect, frame: &mut Frame) {
        if area.is_empty() {
            return;
        }
        if self.focused {
            frame.tint(area, mix(TRACK, BERRY, 0.18));
        }
        if self.lines.len() == 1 && self.lines[0].is_empty() {
            frame.print(area, 0, &paint(MUTED, &italic(&self.placeholder)));
            if self.focused {
                frame.print(area, 0, &caret(self.placeholder.chars().next().unwrap_or(' ')));
            }
            return;
        }
        let (rows, (cursor_row, cursor_x)) = self.layout(area.width);
        let h = area.height;
        let top = self.top.get().min(cursor_row).max((cursor_row + 1).saturating_sub(h));
        self.top.set(top);
        let colour = if self.focused { TEXT } else { SOFT };
        for (n, line) in rows.iter().skip(top).take(h).enumerate() {
            frame.print(area, n, &paint(colour, line));
        }
        if self.focused {
            let under = self.lines[self.row].get(self.col).copied().unwrap_or(' ');
            let line = area.row(cursor_row - top);
            let at = Rect::new(line.x + cursor_x, line.y, line.width.saturating_sub(cursor_x), 1);
            frame.print(at, 0, &caret(under));
        }
    }

    fn size(&self) -> Option<(usize, usize)> {
        let widest = self
            .lines
            .iter()
            .map(|l| l.iter().map(|&c| char_width(c)).sum::<usize>());
        Some((widest.max().unwrap_or(0).max(20), self.lines.len().max(3)))
    }
}

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

    fn at(text: &TextArea) -> (usize, usize) {
        let p = text.cursor();
        (p.row, p.column)
    }

    fn typed(text: &mut TextArea, keys: &[Key]) {
        for k in keys {
            text.handle(&Event::Key(*k));
        }
    }

    fn drawn(text: &TextArea, w: usize, h: usize) -> String {
        crate::set_color_mode(crate::ColorMode::None);
        let mut f = Frame::new(w, h);
        f.render(f.area(), text);
        f.snapshot().plain()
    }

    fn focused(value: &str) -> TextArea {
        let mut text = TextArea::new();
        text.focus(true);
        text.set_value(value);
        text
    }

    #[test]
    fn types_breaks_and_joins_lines() {
        let mut text = TextArea::new();
        typed(&mut text, &[Key::Char('x')]);
        assert_eq!(text.value(), "", "no focus, no typing");
        assert!(!text.handle(&Event::Paste("x".into())));
        text.focus(true);
        typed(
            &mut text,
            &[Key::Char('a'), Key::Enter, Key::Char('b'), Key::Left, Key::Backspace],
        );
        assert_eq!((text.value().as_str(), at(&text)), ("ab", (0, 1)));
        typed(&mut text, &[Key::Enter, Key::Up, Key::End, Key::Delete]);
        assert_eq!((text.value().as_str(), at(&text)), ("ab", (0, 1)));
        typed(&mut text, &[Key::Delete, Key::Delete, Key::Home, Key::Backspace]);
        assert_eq!(text.value(), "a", "nothing to delete at either end");
        typed(&mut text, &[Key::End]);
        assert!(text.handle(&Event::Paste("\tb\r\x07\nc".into())));
        assert_eq!(text.value(), "a    b\nc");
        assert!(!text.handle(&Event::Tick) && !text.handle(&Event::Key(Key::Tab)));
        text.clear();
        assert_eq!((text.value().as_str(), at(&text)), ("", (0, 0)));
    }

    #[test]
    fn moves_across_lines_keeping_its_column() {
        let mut text = focused("long line\nab\nanother line");
        assert_eq!(at(&text), (2, 12));
        typed(&mut text, &[Key::Up]);
        assert_eq!(at(&text), (1, 2), "as far as the short line goes");
        typed(&mut text, &[Key::Up]);
        assert_eq!(at(&text), (0, 9), "and back out to where it was aiming");
        typed(&mut text, &[Key::Up]);
        assert_eq!(at(&text), (0, 0), "up from the top goes to the start");
        typed(&mut text, &[Key::PageDown]);
        assert_eq!(at(&text), (2, 0));
        typed(&mut text, &[Key::Down]);
        assert_eq!(at(&text), (2, 12), "down from the bottom goes to the end");
        typed(&mut text, &[Key::PageUp]);
        assert_eq!(at(&text), (0, 9));
        typed(&mut text, &[Key::Right]);
        assert_eq!(at(&text), (1, 0), "right from a line's end goes to the next");
        typed(&mut text, &[Key::Left]);
        assert_eq!(at(&text), (0, 9), "and left comes back");
        typed(&mut text, &[Key::Ctrl('a'), Key::Left]);
        assert_eq!(at(&text), (0, 0), "nowhere left of the start");
        typed(&mut text, &[Key::Ctrl('e')]);
        assert_eq!(at(&text), (0, 9));
    }

    #[test]
    fn clears_parts_of_a_line() {
        let mut text = focused("keep\ndeploy my app");
        typed(&mut text, &[Key::Ctrl('w')]);
        assert_eq!(text.value(), "keep\ndeploy my ");
        typed(&mut text, &[Key::Ctrl('w'), Key::Left, Key::Ctrl('k')]);
        assert_eq!(text.value(), "keep\ndeploy");
        typed(&mut text, &[Key::Left, Key::Ctrl('u')]);
        assert_eq!((text.value().as_str(), at(&text)), ("keep\ny", (1, 0)));
    }

    #[test]
    fn wraps_and_scrolls_to_keep_the_cursor_in_view() {
        let mut text = focused("abcdefgh\nij");
        assert_eq!(drawn(&text, 5, 4), "abcde\nfgh\nij \n");
        typed(&mut text, &[Key::Up, Key::End]);
        assert_eq!(text.layout(4).1, (2, 0), "past a full row, the cursor starts the next");
        // three rows, two showing: the view follows the cursor, and stays put when it can
        assert_eq!(drawn(&text, 5, 2), "abcde\nfgh ");
        typed(&mut text, &[Key::Down]);
        assert_eq!(drawn(&text, 5, 2), "fgh\nij ");
        typed(&mut text, &[Key::Up]);
        assert_eq!(drawn(&text, 5, 2), "fgh \nij");
        typed(&mut text, &[Key::Ctrl('a')]);
        assert_eq!(drawn(&text, 5, 2), "abcde\nfgh");
        text.focus(false);
        assert_eq!(drawn(&text, 5, 2), "abcde\nfgh");
        assert_eq!(drawn(&text, 0, 0), "");
    }

    #[test]
    fn shows_its_placeholder_and_wants_a_little_room() {
        let empty = TextArea::new().placeholder("notes");
        assert_eq!(drawn(&empty, 10, 1), "notes");
        let mut lit = empty.clone();
        lit.focus(true);
        assert_eq!(drawn(&lit, 10, 1), "notes");
        assert_eq!(TextArea::default().size(), Some((20, 3)));
        assert_eq!(focused(&"x".repeat(30)).size(), Some((30, 3)));
    }
}