modalx 0.1.1

A polished, modal-driven terminal UI framework for Rust featuring dynamic boxed frames, interactive forms, and context-aware shortcuts.
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
use colored::Colorize;
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};

use crate::theme::{is_utf8_supported, strip_ansi, BORDER_COLOR, RESET};

/// Action returned when a key event is processed by `TextInput`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TextInputAction {
    /// User submitted the input (e.g. Enter pressed).
    Submit(String),
    /// Content or cursor changed.
    Changed,
    /// History navigation occurred.
    HistoryNavigated,
    /// Unhandled or ignored key event.
    None,
}

/// A responsive, stateful single-line text input engine with full readline editing,
/// command history navigation, unicode safety, and inside-the-box row rendering.
#[derive(Debug, Clone)]
pub struct TextInput {
    buffer: String,
    cursor: usize,
    history: Vec<String>,
    history_idx: Option<usize>,
    history_stash: String,
}

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

impl TextInput {
    /// Creates a new empty `TextInput`.
    pub fn new() -> Self {
        Self {
            buffer: String::new(),
            cursor: 0,
            history: Vec::new(),
            history_idx: None,
            history_stash: String::new(),
        }
    }

    /// Creates a `TextInput` initialized with an initial value.
    pub fn with_value(val: impl Into<String>) -> Self {
        let buffer = val.into();
        let cursor = buffer.chars().count();
        Self {
            buffer,
            cursor,
            history: Vec::new(),
            history_idx: None,
            history_stash: String::new(),
        }
    }

    /// Attaches pre-populated command history.
    pub fn with_history(mut self, history: Vec<String>) -> Self {
        self.history = history;
        self
    }

    /// Returns the current raw string buffer.
    pub fn buffer(&self) -> &str {
        &self.buffer
    }

    /// Returns the current character cursor position.
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    /// Returns the history entries.
    pub fn history(&self) -> &[String] {
        &self.history
    }

    /// Replaces the current buffer and resets the cursor to the end.
    pub fn set_buffer(&mut self, val: impl Into<String>) {
        self.buffer = val.into();
        self.cursor = self.buffer.chars().count();
    }

    /// Inserts a character at the current cursor position.
    pub fn insert_char(&mut self, c: char) {
        let mut chars: Vec<char> = self.buffer.chars().collect();
        let cur = self.cursor.min(chars.len());
        chars.insert(cur, c);
        self.buffer = chars.into_iter().collect();
        self.cursor = cur + 1;
    }

    /// Inserts a string at the current cursor position.
    pub fn insert_str(&mut self, s: &str) {
        for c in s.chars() {
            self.insert_char(c);
        }
    }

    /// Deletes the character directly preceding the cursor.
    pub fn backspace(&mut self) {
        if self.cursor > 0 && !self.buffer.is_empty() {
            let mut chars: Vec<char> = self.buffer.chars().collect();
            let cur = self.cursor.min(chars.len());
            chars.remove(cur - 1);
            self.buffer = chars.into_iter().collect();
            self.cursor = cur - 1;
        }
    }

    /// Deletes the character directly under the cursor.
    pub fn delete(&mut self) {
        let mut chars: Vec<char> = self.buffer.chars().collect();
        if self.cursor < chars.len() {
            chars.remove(self.cursor);
            self.buffer = chars.into_iter().collect();
        }
    }

    /// Deletes the word preceding the cursor (Ctrl+Backspace / Ctrl+W / Alt+Backspace).
    pub fn delete_word(&mut self) {
        if self.cursor == 0 || self.buffer.is_empty() {
            return;
        }
        let chars: Vec<char> = self.buffer.chars().collect();
        let cur = self.cursor.min(chars.len());
        let before = &chars[..cur];

        let mut idx = cur;
        // Skip whitespace immediately before cursor
        while idx > 0 && before[idx - 1].is_whitespace() {
            idx -= 1;
        }
        // Skip word / non-whitespace characters
        while idx > 0 && !before[idx - 1].is_whitespace() {
            idx -= 1;
        }

        let mut candidate: String = chars[..idx].iter().collect();
        let after: String = chars[cur..].iter().collect();
        candidate.push_str(&after);

        self.buffer = candidate;
        self.cursor = idx;
    }

    /// Clears the entire buffer and resets the cursor.
    pub fn clear(&mut self) {
        self.buffer.clear();
        self.cursor = 0;
    }

    /// Moves cursor left by one character.
    pub fn move_left(&mut self) {
        self.cursor = self.cursor.saturating_sub(1);
    }

    /// Moves cursor right by one character.
    pub fn move_right(&mut self) {
        let len = self.buffer.chars().count();
        if self.cursor < len {
            self.cursor += 1;
        }
    }

    /// Moves cursor to the start of the previous word.
    pub fn word_left(&mut self) {
        if self.cursor == 0 {
            return;
        }
        let chars: Vec<char> = self.buffer.chars().collect();
        let cur = self.cursor.min(chars.len());
        let mut idx = cur;
        while idx > 0 && chars[idx - 1].is_whitespace() {
            idx -= 1;
        }
        while idx > 0 && !chars[idx - 1].is_whitespace() {
            idx -= 1;
        }
        self.cursor = idx;
    }

    /// Moves cursor to the start of the next word.
    pub fn word_right(&mut self) {
        let chars: Vec<char> = self.buffer.chars().collect();
        let len = chars.len();
        let mut idx = self.cursor.min(len);
        while idx < len && !chars[idx].is_whitespace() {
            idx += 1;
        }
        while idx < len && chars[idx].is_whitespace() {
            idx += 1;
        }
        self.cursor = idx.min(len);
    }

    /// Moves cursor to the beginning of the line.
    pub fn move_home(&mut self) {
        self.cursor = 0;
    }

    /// Moves cursor to the end of the line.
    pub fn move_end(&mut self) {
        self.cursor = self.buffer.chars().count();
    }

    /// Recalls previous command from history (Up arrow).
    pub fn history_prev(&mut self) -> bool {
        if self.history.is_empty() {
            return false;
        }
        match self.history_idx {
            None => {
                self.history_stash = self.buffer.clone();
                let last = self.history.len() - 1;
                self.history_idx = Some(last);
                self.buffer = self.history[last].clone();
                self.cursor = self.buffer.chars().count();
                true
            }
            Some(idx) if idx > 0 => {
                let prev = idx - 1;
                self.history_idx = Some(prev);
                self.buffer = self.history[prev].clone();
                self.cursor = self.buffer.chars().count();
                true
            }
            _ => false,
        }
    }

    /// Recalls next command from history (Down arrow).
    pub fn history_next(&mut self) -> bool {
        match self.history_idx {
            Some(idx) => {
                if idx + 1 < self.history.len() {
                    let next = idx + 1;
                    self.history_idx = Some(next);
                    self.buffer = self.history[next].clone();
                    self.cursor = self.buffer.chars().count();
                } else {
                    self.history_idx = None;
                    self.buffer = self.history_stash.clone();
                    self.cursor = self.buffer.chars().count();
                }
                true
            }
            None => false,
        }
    }

    /// Commits the current buffer to history and clears the buffer, returning the submitted text.
    pub fn commit(&mut self) -> String {
        let submitted = self.buffer.clone();
        let trimmed = submitted.trim();
        if !trimmed.is_empty() {
            // Avoid duplicate contiguous history entries
            if self.history.last().map(|s| s.as_str()) != Some(trimmed) {
                self.history.push(trimmed.to_string());
            }
        }
        self.buffer.clear();
        self.cursor = 0;
        self.history_idx = None;
        self.history_stash.clear();
        submitted
    }

    /// Processes a crossterm key event, returning the resulting action.
    pub fn handle_key(&mut self, event: &KeyEvent) -> TextInputAction {
        if event.kind != KeyEventKind::Press {
            return TextInputAction::None;
        }

        // 1. Modifiers + Keys
        if event.modifiers.contains(KeyModifiers::CONTROL) {
            match event.code {
                KeyCode::Char('w')
                | KeyCode::Char('W')
                | KeyCode::Backspace
                | KeyCode::Char('\x08')
                | KeyCode::Char('\x7f') => {
                    self.delete_word();
                    return TextInputAction::Changed;
                }
                KeyCode::Char('u') | KeyCode::Char('U') => {
                    self.clear();
                    return TextInputAction::Changed;
                }
                KeyCode::Char('a') | KeyCode::Char('A') => {
                    self.move_home();
                    return TextInputAction::Changed;
                }
                KeyCode::Char('e') | KeyCode::Char('E') => {
                    self.move_end();
                    return TextInputAction::Changed;
                }
                KeyCode::Left => {
                    self.word_left();
                    return TextInputAction::Changed;
                }
                KeyCode::Right => {
                    self.word_right();
                    return TextInputAction::Changed;
                }
                _ => return TextInputAction::None,
            }
        }

        if event.modifiers.contains(KeyModifiers::ALT) {
            match event.code {
                KeyCode::Backspace | KeyCode::Char('\x08') | KeyCode::Char('\x7f') => {
                    self.delete_word();
                    return TextInputAction::Changed;
                }
                KeyCode::Char('b') | KeyCode::Char('B') => {
                    self.word_left();
                    return TextInputAction::Changed;
                }
                KeyCode::Char('f') | KeyCode::Char('F') => {
                    self.word_right();
                    return TextInputAction::Changed;
                }
                _ => return TextInputAction::None,
            }
        }

        // 2. Navigation & Editing Keys
        match event.code {
            KeyCode::Enter => {
                let line = self.commit();
                TextInputAction::Submit(line)
            }
            KeyCode::Backspace => {
                self.backspace();
                TextInputAction::Changed
            }
            KeyCode::Delete => {
                self.delete();
                TextInputAction::Changed
            }
            KeyCode::Left => {
                self.move_left();
                TextInputAction::Changed
            }
            KeyCode::Right => {
                self.move_right();
                TextInputAction::Changed
            }
            KeyCode::Home => {
                self.move_home();
                TextInputAction::Changed
            }
            KeyCode::End => {
                self.move_end();
                TextInputAction::Changed
            }
            KeyCode::Up => {
                if self.history_prev() {
                    TextInputAction::HistoryNavigated
                } else {
                    TextInputAction::None
                }
            }
            KeyCode::Down => {
                if self.history_next() {
                    TextInputAction::HistoryNavigated
                } else {
                    TextInputAction::None
                }
            }
            KeyCode::Char(c) => {
                self.insert_char(c);
                TextInputAction::Changed
            }
            _ => TextInputAction::None,
        }
    }

    /// Renders the input line enclosed inside box borders `│ <prefix><text> │`
    /// and returns `(rendered_row, visual_cursor_x)`.
    ///
    /// The row adapts to available `inner_width` (the space between left and right `│`).
    /// If the text exceeds the available space, it horizontally scrolls so the cursor
    /// is always visible.
    pub fn render_box_row(&self, inner_width: usize, prefix: &str) -> (String, u16) {
        let border_char = if is_utf8_supported() { "│" } else { "|" };
        let prefix_clean = strip_ansi(prefix);
        let prefix_len = prefix_clean.chars().count();

        // 1 column left border + 1 padding space
        let base_x = 2; // X index where content starts after "│ "
        let available_text_len = inner_width.saturating_sub(prefix_len + 2).max(1);

        let chars: Vec<char> = self.buffer.chars().collect();
        let total_chars = chars.len();
        let cur = self.cursor.min(total_chars);

        let (display_text, visual_offset) = if total_chars > available_text_len {
            let start = cur.saturating_sub(available_text_len.saturating_sub(4));
            let end = (start + available_text_len).min(total_chars);
            let slice: String = chars[start..end].iter().collect();
            let offset = cur.saturating_sub(start);
            (slice, offset)
        } else {
            (self.buffer.clone(), cur)
        };

        let displayed_len = display_text.chars().count();
        let pad_len = inner_width.saturating_sub(1 + prefix_len + displayed_len + 1);

        let row = format!(
            "{}{}{} {}{}{}{}{}{}{}\x1B[K\r\n",
            BORDER_COLOR,
            border_char,
            RESET,
            prefix.cyan().bold(),
            display_text,
            " ".repeat(pad_len),
            " ",
            BORDER_COLOR,
            border_char,
            RESET,
        );

        let visual_cursor_x = (base_x + prefix_len + visual_offset) as u16;
        (row, visual_cursor_x)
    }
}

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

    #[test]
    fn test_text_input_insert_and_backspace() {
        let mut input = TextInput::new();
        input.insert_str("hello world");
        assert_eq!(input.buffer(), "hello world");
        assert_eq!(input.cursor(), 11);

        input.backspace();
        assert_eq!(input.buffer(), "hello worl");
        assert_eq!(input.cursor(), 10);
    }

    #[test]
    fn test_text_input_delete_word() {
        let mut input = TextInput::new();
        input.insert_str("server stop now");
        input.delete_word();
        assert_eq!(input.buffer(), "server stop ");
        assert_eq!(input.cursor(), 12);

        input.delete_word();
        assert_eq!(input.buffer(), "server ");
        assert_eq!(input.cursor(), 7);

        input.delete_word();
        assert_eq!(input.buffer(), "");
        assert_eq!(input.cursor(), 0);
    }

    #[test]
    fn test_text_input_word_navigation() {
        let mut input = TextInput::new();
        input.insert_str("one two three");
        assert_eq!(input.cursor(), 13);

        input.word_left();
        assert_eq!(input.cursor(), 8);

        input.word_left();
        assert_eq!(input.cursor(), 4);

        input.word_right();
        assert_eq!(input.cursor(), 8);
    }

    #[test]
    fn test_text_input_history() {
        let mut input = TextInput::new();
        input.insert_str("cmd1");
        assert_eq!(input.commit(), "cmd1");

        input.insert_str("cmd2");
        assert_eq!(input.commit(), "cmd2");

        assert!(input.history_prev());
        assert_eq!(input.buffer(), "cmd2");

        assert!(input.history_prev());
        assert_eq!(input.buffer(), "cmd1");

        assert!(input.history_next());
        assert_eq!(input.buffer(), "cmd2");

        assert!(input.history_next());
        assert_eq!(input.buffer(), "");
    }

    #[test]
    fn test_render_box_row() {
        let mut input = TextInput::new();
        input.insert_str("status");
        let (row, cursor_x) = input.render_box_row(40, "> ");
        assert!(row.contains("│"));
        assert!(row.contains("status"));
        // base_x (2) + prefix_len (2) + 6 = 10
        assert_eq!(cursor_x, 10);
    }
}