a3s-tui 0.1.4

TEA (The Elm Architecture) framework for terminal user interfaces
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use crate::element::{BoxElement, Element, FlexDirection, TextElement};
use crate::event::KeyEvent;
use crate::style::Color;
use crossterm::event::{KeyCode, KeyModifiers};

pub struct Textarea {
    lines: Vec<String>,
    cursor_row: usize,
    cursor_col: usize,
    offset: usize,
    width: u16,
    height: u16,
    placeholder: String,
    focused: bool,
    char_limit: Option<usize>,
    submit_on_enter: bool,
    /// When set, the height auto-fits the line count, clamped to this max.
    auto_grow_max: Option<u16>,
}

#[derive(Debug, Clone)]
pub enum TextareaMsg {
    Changed(String),
    Submit(String),
}

impl Textarea {
    pub fn new() -> Self {
        Self {
            lines: vec![String::new()],
            cursor_row: 0,
            cursor_col: 0,
            offset: 0,
            width: 80,
            height: 5,
            placeholder: String::new(),
            focused: true,
            char_limit: None,
            submit_on_enter: false,
            auto_grow_max: None,
        }
    }

    pub fn with_placeholder(mut self, p: impl Into<String>) -> Self {
        self.placeholder = p.into();
        self
    }

    pub fn with_height(mut self, h: u16) -> Self {
        self.height = h;
        self
    }

    /// Auto-fit the visible height to the number of lines (1..=max), so the box
    /// grows with embedded newlines instead of scrolling a single row.
    pub fn with_auto_grow(mut self, max: u16) -> Self {
        self.auto_grow_max = Some(max.max(1));
        self.fit_height();
        self
    }

    /// Current visible height (rows). Grows with content when auto-grow is on.
    pub fn height(&self) -> u16 {
        self.height
    }

    fn fit_height(&mut self) {
        if let Some(max) = self.auto_grow_max {
            let n = self.lines.len() as u16;
            self.height = n.clamp(1, max);
            // The box grew to fit every line, so the internal scroll offset (set
            // while the height was smaller, to follow the cursor) must reset —
            // otherwise earlier lines stay scrolled out of view.
            if n <= max {
                self.offset = 0;
            }
        }
    }

    pub fn with_width(mut self, w: u16) -> Self {
        self.width = w;
        self
    }

    pub fn with_char_limit(mut self, limit: usize) -> Self {
        self.char_limit = Some(limit);
        self
    }

    pub fn with_submit_on_enter(mut self, submit: bool) -> Self {
        self.submit_on_enter = submit;
        self
    }

    pub fn focus(&mut self) {
        self.focused = true;
    }
    pub fn blur(&mut self) {
        self.focused = false;
    }

    pub fn value(&self) -> String {
        self.lines.join("\n")
    }

    pub fn set_value(&mut self, v: &str) {
        self.lines = v.lines().map(|l| l.to_string()).collect();
        if self.lines.is_empty() {
            self.lines.push(String::new());
        }
        self.cursor_row = self.lines.len() - 1;
        self.cursor_col = Self::char_len(&self.lines[self.cursor_row]);
        self.fit_height();
    }

    pub fn clear(&mut self) {
        self.lines = vec![String::new()];
        self.cursor_row = 0;
        self.cursor_col = 0;
        self.offset = 0;
        self.fit_height();
    }

    pub fn total_chars(&self) -> usize {
        self.lines.iter().map(|l| l.chars().count()).sum::<usize>() + self.lines.len() - 1
    }

    pub fn handle_key(&mut self, key: &KeyEvent) -> Option<TextareaMsg> {
        if !self.focused {
            return None;
        }

        let result = match (key.code, key.modifiers) {
            (KeyCode::Enter, KeyModifiers::NONE) if self.submit_on_enter => {
                Some(TextareaMsg::Submit(self.value()))
            }
            (KeyCode::Enter, KeyModifiers::NONE) => {
                self.insert_newline();
                Some(TextareaMsg::Changed(self.value()))
            }
            // Shift/Alt+Enter inserts a newline instead of submitting (works in
            // terminals that report the modifier; some fold it into plain Enter).
            (KeyCode::Enter, m)
                if m.contains(KeyModifiers::SHIFT) || m.contains(KeyModifiers::ALT) =>
            {
                self.insert_newline();
                Some(TextareaMsg::Changed(self.value()))
            }
            (KeyCode::Char('j'), m) if m.contains(KeyModifiers::CONTROL) => {
                self.insert_newline();
                Some(TextareaMsg::Changed(self.value()))
            }
            (KeyCode::Char('a'), m) if m.contains(KeyModifiers::CONTROL) => {
                self.cursor_col = 0;
                None
            }
            (KeyCode::Char('e'), m) if m.contains(KeyModifiers::CONTROL) => {
                self.cursor_col = Self::char_len(&self.lines[self.cursor_row]);
                None
            }
            (KeyCode::Char('k'), m) if m.contains(KeyModifiers::CONTROL) => {
                let off = Self::byte_off(&self.lines[self.cursor_row], self.cursor_col);
                self.lines[self.cursor_row].truncate(off);
                Some(TextareaMsg::Changed(self.value()))
            }
            (KeyCode::Char(c), _) => {
                if let Some(limit) = self.char_limit {
                    if self.total_chars() >= limit {
                        return None;
                    }
                }
                self.insert_char(c);
                Some(TextareaMsg::Changed(self.value()))
            }
            (KeyCode::Backspace, _) => {
                if self.delete_backward() {
                    Some(TextareaMsg::Changed(self.value()))
                } else {
                    None
                }
            }
            (KeyCode::Delete, _) => {
                if self.delete_forward() {
                    Some(TextareaMsg::Changed(self.value()))
                } else {
                    None
                }
            }
            (KeyCode::Left, _) => {
                self.move_left();
                None
            }
            (KeyCode::Right, _) => {
                self.move_right();
                None
            }
            (KeyCode::Up, _) => {
                self.move_up();
                None
            }
            (KeyCode::Down, _) => {
                self.move_down();
                None
            }
            (KeyCode::Home, _) => {
                self.cursor_col = 0;
                None
            }
            (KeyCode::End, _) => {
                self.cursor_col = Self::char_len(&self.lines[self.cursor_row]);
                None
            }
            _ => None,
        };
        self.fit_height();
        result
    }

    pub fn view(&self) -> String {
        if self.lines == vec![String::new()] && !self.placeholder.is_empty() && !self.focused {
            return format!("\x1b[2m{}\x1b[0m", self.placeholder);
        }

        let h = self.height as usize;
        let end = (self.offset + h).min(self.lines.len());
        let visible = &self.lines[self.offset..end];

        let mut result = Vec::new();
        for (i, line) in visible.iter().enumerate() {
            let row = self.offset + i;
            if row == self.cursor_row && self.focused {
                result.push(self.render_line_with_cursor(line));
            } else {
                result.push(line.clone());
            }
        }

        for _ in result.len()..h {
            result.push("~".to_string());
        }

        result.join("\n")
    }

    fn char_width(c: char) -> usize {
        unicode_width::UnicodeWidthChar::width(c).unwrap_or(0)
    }

    /// First visible display column of the cursor's line (horizontal scroll), so
    /// the insertion point stays on screen. CJK glyphs count as 2 columns.
    fn scroll_start(&self) -> usize {
        let width = (self.width as usize).max(1);
        let cursor_disp = self.cursor_display_col_abs();
        cursor_disp.saturating_sub(width.saturating_sub(1))
    }

    /// Absolute display column of the insertion point within its line.
    fn cursor_display_col_abs(&self) -> usize {
        self.lines
            .get(self.cursor_row)
            .map(|line| {
                line.chars()
                    .take(self.cursor_col)
                    .map(Self::char_width)
                    .sum()
            })
            .unwrap_or(0)
    }

    /// Display column of the insertion point relative to the visible window —
    /// what a host needs to place the real terminal cursor.
    pub fn cursor_display_col(&self) -> usize {
        self.cursor_display_col_abs() - self.scroll_start()
    }

    /// The cursor's row within the visible window (for multi-line input — the
    /// host places the real terminal cursor on this row).
    pub fn cursor_row(&self) -> usize {
        self.cursor_row.saturating_sub(self.offset)
    }

    /// Render the cursor's line as plain (horizontally scrolled) text; the real
    /// terminal cursor marks the insertion point, so no reverse-video block here.
    fn render_line_with_cursor(&self, line: &str) -> String {
        let width = (self.width as usize).max(1);
        let start = self.scroll_start();

        let mut out = String::new();
        let mut disp = 0usize; // absolute display column scanned
        let mut shown = 0usize; // visible columns emitted
        for ch in line.chars() {
            let w = Self::char_width(ch);
            if disp < start {
                disp += w; // starts before the scroll window — skip wholly
                continue;
            }
            if shown + w > width {
                break; // right edge reached
            }
            out.push(ch);
            disp += w;
            shown += w;
        }
        out
    }

    pub fn set_width(&mut self, w: u16) {
        self.width = w;
    }

    // cursor_col is a CHAR index; convert to a byte offset before String ops.
    fn byte_off(line: &str, col: usize) -> usize {
        line.char_indices().nth(col).map_or(line.len(), |(b, _)| b)
    }

    fn char_len(line: &str) -> usize {
        line.chars().count()
    }

    /// Insert a (possibly multi-line) string at the cursor — used for paste, so
    /// newlines become real line breaks instead of submitting the message.
    pub fn insert_str(&mut self, text: &str) {
        for ch in text.chars() {
            match ch {
                '\n' => self.insert_newline(),
                '\r' => {} // drop CR so CRLF pastes don't double-break
                _ => self.insert_char(ch),
            }
        }
    }

    fn insert_char(&mut self, c: char) {
        let off = Self::byte_off(&self.lines[self.cursor_row], self.cursor_col);
        self.lines[self.cursor_row].insert(off, c);
        self.cursor_col += 1;
    }

    fn insert_newline(&mut self) {
        let off = Self::byte_off(&self.lines[self.cursor_row], self.cursor_col);
        let rest = self.lines[self.cursor_row].split_off(off);
        self.cursor_row += 1;
        self.lines.insert(self.cursor_row, rest);
        self.cursor_col = 0;
        self.ensure_visible();
    }

    fn delete_backward(&mut self) -> bool {
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
            let off = Self::byte_off(&self.lines[self.cursor_row], self.cursor_col);
            self.lines[self.cursor_row].remove(off);
            true
        } else if self.cursor_row > 0 {
            let current_line = self.lines.remove(self.cursor_row);
            self.cursor_row -= 1;
            self.cursor_col = Self::char_len(&self.lines[self.cursor_row]);
            self.lines[self.cursor_row].push_str(&current_line);
            self.ensure_visible();
            true
        } else {
            false
        }
    }

    fn delete_forward(&mut self) -> bool {
        if self.cursor_col < Self::char_len(&self.lines[self.cursor_row]) {
            let off = Self::byte_off(&self.lines[self.cursor_row], self.cursor_col);
            self.lines[self.cursor_row].remove(off);
            true
        } else if self.cursor_row + 1 < self.lines.len() {
            let next_line = self.lines.remove(self.cursor_row + 1);
            self.lines[self.cursor_row].push_str(&next_line);
            true
        } else {
            false
        }
    }

    fn move_left(&mut self) {
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
        } else if self.cursor_row > 0 {
            self.cursor_row -= 1;
            self.cursor_col = Self::char_len(&self.lines[self.cursor_row]);
            self.ensure_visible();
        }
    }

    fn move_right(&mut self) {
        if self.cursor_col < Self::char_len(&self.lines[self.cursor_row]) {
            self.cursor_col += 1;
        } else if self.cursor_row + 1 < self.lines.len() {
            self.cursor_row += 1;
            self.cursor_col = 0;
            self.ensure_visible();
        }
    }

    fn move_up(&mut self) {
        if self.cursor_row > 0 {
            self.cursor_row -= 1;
            self.cursor_col = self
                .cursor_col
                .min(Self::char_len(&self.lines[self.cursor_row]));
            self.ensure_visible();
        }
    }

    fn move_down(&mut self) {
        if self.cursor_row + 1 < self.lines.len() {
            self.cursor_row += 1;
            self.cursor_col = self
                .cursor_col
                .min(Self::char_len(&self.lines[self.cursor_row]));
            self.ensure_visible();
        }
    }

    fn ensure_visible(&mut self) {
        let h = self.height as usize;
        if self.cursor_row < self.offset {
            self.offset = self.cursor_row;
        } else if self.cursor_row >= self.offset + h {
            self.offset = self.cursor_row - h + 1;
        }
    }
}

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

impl Textarea {
    pub fn element<Msg>(&self) -> Element<Msg> {
        if self.lines == vec![String::new()] && !self.placeholder.is_empty() && !self.focused {
            return Element::Text(
                TextElement::new(&self.placeholder)
                    .dim()
                    .fg(Color::BrightBlack),
            );
        }

        let h = self.height as usize;
        let end = (self.offset + h).min(self.lines.len());
        let visible = &self.lines[self.offset..end];

        let mut children: Vec<Element<Msg>> = Vec::new();
        for (i, line) in visible.iter().enumerate() {
            let row = self.offset + i;
            if row == self.cursor_row && self.focused {
                children.push(Element::Text(TextElement::new(
                    self.render_line_with_cursor(line),
                )));
            } else {
                children.push(Element::Text(TextElement::new(line.as_str())));
            }
        }

        for _ in children.len()..h {
            children.push(Element::Text(
                TextElement::new("~").dim().fg(Color::BrightBlack),
            ));
        }

        Element::Box(
            BoxElement::new()
                .direction(FlexDirection::Column)
                .children(children),
        )
    }
}

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

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent {
            code,
            modifiers: KeyModifiers::NONE,
        }
    }

    fn ctrl(code: KeyCode) -> KeyEvent {
        KeyEvent {
            code,
            modifiers: KeyModifiers::CONTROL,
        }
    }

    #[test]
    fn typing_text() {
        let mut ta = Textarea::new();
        ta.handle_key(&key(KeyCode::Char('h')));
        ta.handle_key(&key(KeyCode::Char('i')));
        assert_eq!(ta.value(), "hi");
    }

    #[test]
    fn multibyte_input_no_panic() {
        // Regression: cursor_col mixed char/byte indexing → panic on CJK input.
        let mut ta = Textarea::new();
        for c in "你好abc世界".chars() {
            ta.handle_key(&key(KeyCode::Char(c)));
        }
        assert_eq!(ta.value(), "你好abc世界");
        // Move left over multibyte chars and edit — must not panic on a boundary.
        // chars: 你 好 a b c 世 界 ; cursor at end (7), Left x4 -> col 3 (before 'b')
        for _ in 0..4 {
            ta.handle_key(&key(KeyCode::Left));
        }
        ta.handle_key(&key(KeyCode::Backspace)); // col 3->2, deletes 'a'
        ta.handle_key(&key(KeyCode::Delete)); // deletes 'b' at col 2
        assert_eq!(ta.value(), "你好c世界");
        let _ = ta.view();
    }

    #[test]
    fn cjk_cursor_stays_in_view_when_line_overflows() {
        // Narrow box; type a long CJK line. The visible render must never exceed
        // the width and must always include the cursor block at the end.
        let mut ta = Textarea::new().with_width(8).with_height(1);
        for c in "你好世界你好世界".chars() {
            ta.handle_key(&key(KeyCode::Char(c)));
        }
        let line = ta.view();
        let visible = crate::style::visible_len(&line); // ANSI-stripped display width
        assert!(visible <= 8, "rendered width {visible} exceeds box width 8");
        // Insertion point must stay within the visible window for the host to
        // place the real cursor on it.
        assert!(
            ta.cursor_display_col() <= 8,
            "cursor col {} out of view",
            ta.cursor_display_col()
        );
    }

    #[test]
    fn newline_creates_new_line() {
        let mut ta = Textarea::new();
        ta.handle_key(&key(KeyCode::Char('a')));
        ta.handle_key(&key(KeyCode::Enter));
        ta.handle_key(&key(KeyCode::Char('b')));
        assert_eq!(ta.value(), "a\nb");
    }

    #[test]
    fn auto_grow_shows_all_lines_after_newline() {
        // Regression: with auto-grow, adding a newline grew the height but left
        // the scroll offset following the cursor, hiding the first line. The box
        // must show BOTH lines (height grows, offset resets).
        let mut ta = Textarea::new().with_height(1).with_auto_grow(8);
        ta.handle_key(&key(KeyCode::Char('a')));
        ta.handle_key(&key(KeyCode::Enter));
        ta.handle_key(&key(KeyCode::Char('b')));
        assert_eq!(ta.height(), 2, "box grew to two rows");
        let view = ta.view();
        assert!(view.contains('a'), "first line still visible");
        assert!(view.contains('b'), "second line visible");
    }

    #[test]
    fn backspace_joins_lines() {
        let mut ta = Textarea::new();
        ta.set_value("ab\ncd");
        ta.cursor_row = 1;
        ta.cursor_col = 0;
        ta.handle_key(&key(KeyCode::Backspace));
        assert_eq!(ta.value(), "abcd");
    }

    #[test]
    fn cursor_movement() {
        let mut ta = Textarea::new();
        ta.set_value("hello\nworld");
        ta.cursor_row = 1;
        ta.cursor_col = 5;
        ta.handle_key(&key(KeyCode::Up));
        assert_eq!(ta.cursor_row, 0);
        ta.handle_key(&key(KeyCode::Home));
        assert_eq!(ta.cursor_col, 0);
        ta.handle_key(&key(KeyCode::End));
        assert_eq!(ta.cursor_col, 5);
    }

    #[test]
    fn ctrl_a_and_e() {
        let mut ta = Textarea::new();
        ta.set_value("test");
        ta.cursor_col = 2;
        ta.handle_key(&ctrl(KeyCode::Char('a')));
        assert_eq!(ta.cursor_col, 0);
        ta.handle_key(&ctrl(KeyCode::Char('e')));
        assert_eq!(ta.cursor_col, 4);
    }

    #[test]
    fn ctrl_k_kills_line() {
        let mut ta = Textarea::new();
        ta.set_value("hello world");
        ta.cursor_col = 5;
        ta.handle_key(&ctrl(KeyCode::Char('k')));
        assert_eq!(ta.value(), "hello");
    }

    #[test]
    fn char_limit() {
        let mut ta = Textarea::new().with_char_limit(3);
        ta.handle_key(&key(KeyCode::Char('a')));
        ta.handle_key(&key(KeyCode::Char('b')));
        ta.handle_key(&key(KeyCode::Char('c')));
        ta.handle_key(&key(KeyCode::Char('d')));
        assert_eq!(ta.value(), "abc");
    }

    #[test]
    fn clear_resets() {
        let mut ta = Textarea::new();
        ta.set_value("some\ntext");
        ta.clear();
        assert_eq!(ta.value(), "");
        assert_eq!(ta.cursor_row, 0);
        assert_eq!(ta.cursor_col, 0);
    }

    #[test]
    fn submit_on_enter() {
        let mut ta = Textarea::new().with_submit_on_enter(true);
        ta.handle_key(&key(KeyCode::Char('x')));
        let msg = ta.handle_key(&key(KeyCode::Enter));
        assert!(matches!(msg, Some(TextareaMsg::Submit(s)) if s == "x"));
    }
}