oxi-tui 0.25.7

Terminal UI widgets and theme system for oxi, built on 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
//! Input widget — multi-line text input with cursor, placeholder, and scrolling.
//!
//! Built on `ratatui-textarea` for:
//! - Full Unicode including CJK double-width characters
//! - Emacs-like shortcuts (Ctrl+Left/Right for word movement, Ctrl+A/E)
//! - Undo/Redo support (Ctrl+Z / Ctrl+Shift+Z)
//! - Better IME handling via bracketed paste mode
//!
//! Behavior:
//! - Enter submits text
//! - Shift+Enter inserts newline (multiline mode)
//! - Dynamic height: expands based on content up to max_height

use crate::Theme;
use ratatui::prelude::*;
use ratatui::widgets::Paragraph;
use ratatui_textarea::{Input as TextAreaInput, Key, TextArea};

// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------

/// Input state wrapping ratatui-textarea's TextArea.
///
/// This provides all textarea features:
/// - Insert/delete characters, word-by-word deletion (Ctrl+Backspace)
/// - Cursor movement (Left/Right, Ctrl+Left/Right, Home/End)
/// - Undo/Redo (Ctrl+Z / Ctrl+Shift+Z)
/// - Selection support (Shift+Arrow)
/// - Multi-line text with automatic line wrapping
/// - Shift+Enter inserts newline
#[derive(Debug)]
pub struct InputState {
    /// The textarea holds all state (text, cursor, history, etc.)
    textarea: TextArea<'static>,
}

impl Default for InputState {
    fn default() -> Self {
        let mut textarea = TextArea::default();
        // No visual line numbers for input
        textarea.remove_line_number();
        // Disable cursor line highlight for cleaner look
        textarea.set_cursor_line_style(Style::default());
        // Enable soft-wrap so multi-line input expands vertically
        textarea.set_wrap_mode(ratatui_textarea::WrapMode::Word);
        Self { textarea }
    }
}

impl InputState {
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the current text content
    pub fn text(&self) -> String {
        self.textarea.lines().join("\n")
    }

    /// Set text directly (replaces all content)
    pub fn set_text(&mut self, text: String) {
        self.textarea.clear();
        if !text.is_empty() {
            self.textarea.insert_str(&text);
        }
    }

    /// Get lines as a vector
    pub fn lines(&self) -> Vec<String> {
        self.textarea.lines().to_vec()
    }

    pub fn clear(&mut self) {
        self.textarea.clear();
    }

    /// Set placeholder text
    pub fn set_placeholder(&mut self, placeholder: Option<String>) {
        if let Some(p) = placeholder {
            self.textarea.set_placeholder_text(&p);
        } else {
            self.textarea.set_placeholder_text("");
        }
    }

    // ── Backward-compatible API (used by oxi-cli handlers) ──

    /// Insert a character at cursor position
    pub fn insert_char(&mut self, c: char) {
        self.handle_char(c);
    }

    /// Insert a string at cursor position
    pub fn insert_str(&mut self, s: &str) {
        self.textarea.insert_str(s);
    }

    /// Delete character before cursor (Backspace)
    pub fn backspace(&mut self) {
        self.textarea.input(TextAreaInput {
            key: Key::Backspace,
            ..Default::default()
        });
    }

    /// Delete character after cursor (Delete)
    pub fn delete(&mut self) {
        self.textarea.input(TextAreaInput {
            key: Key::Delete,
            ..Default::default()
        });
    }

    /// Move cursor left
    pub fn move_left(&mut self) {
        self.textarea
            .move_cursor(ratatui_textarea::CursorMove::Back);
    }

    /// Move cursor right
    pub fn move_right(&mut self) {
        self.textarea
            .move_cursor(ratatui_textarea::CursorMove::Forward);
    }

    /// Move cursor to start of line
    pub fn move_home(&mut self) {
        self.textarea
            .move_cursor(ratatui_textarea::CursorMove::Head);
    }

    /// Move cursor to end of line
    pub fn move_end(&mut self) {
        self.textarea.move_cursor(ratatui_textarea::CursorMove::End);
    }

    /// Move cursor by word (Ctrl+Left/Right)
    pub fn move_word_left(&mut self) {
        self.textarea
            .move_cursor(ratatui_textarea::CursorMove::WordBack);
    }

    /// Move cursor by word (Ctrl+Left/Right)
    pub fn move_word_right(&mut self) {
        self.textarea
            .move_cursor(ratatui_textarea::CursorMove::WordForward);
    }

    // ── Input handling ──

    /// Handle a key event, returning true if it was consumed (Enter/Tab).
    pub fn handle_key(&mut self, key: Key) -> bool {
        match key {
            Key::Enter => true, // Enter is reserved for submit
            Key::Tab => true,   // Tab is used for slash completion
            _ => {
                self.textarea.input(TextAreaInput {
                    key,
                    ..Default::default()
                });
                false
            }
        }
    }

    /// Handle a char input event.
    pub fn handle_char(&mut self, c: char) {
        self.textarea.input(TextAreaInput {
            key: Key::Char(c),
            ctrl: false,
            alt: false,
            shift: false,
        });
    }

    /// Handle a full Input event.
    /// Returns true if Enter pressed (should submit).
    pub fn handle_input(&mut self, input: TextAreaInput) -> bool {
        if input.key == Key::Enter && !input.shift {
            true // Enter without shift = submit
        } else {
            self.textarea.input(input);
            false
        }
    }

    /// Get mutable access to the underlying textarea
    pub fn textarea_mut(&mut self) -> &mut TextArea<'static> {
        &mut self.textarea
    }

    /// Undo last change
    pub fn undo(&mut self) {
        self.textarea.undo();
    }

    /// Redo last undone change
    pub fn redo(&mut self) {
        self.textarea.redo();
    }

    /// Delete from cursor to the start of the line (Ctrl+U).
    pub fn delete_to_line_start(&mut self) {
        self.textarea.delete_line_by_head();
    }

    /// Delete from cursor to the end of the line (Ctrl+K).
    pub fn delete_to_line_end(&mut self) {
        self.textarea.delete_line_by_end();
    }

    /// Delete word before cursor (Ctrl+W / Ctrl+Backspace).
    pub fn delete_word_backward(&mut self) {
        self.textarea.delete_word();
    }

    /// Delete word after cursor (Ctrl+Delete).
    pub fn delete_word_forward(&mut self) {
        self.textarea.delete_next_word();
    }

    /// Calculate the exact height (in rows) needed to display the current text
    /// with word-wrap at the given width, using `Paragraph::line_count`.
    ///
    /// `width` should be the available content width (excluding padding).
    pub fn required_height(&self, width: u16, max_height: u16) -> u16 {
        if width < 1 {
            return 1;
        }

        let lines = self.textarea.lines();
        if lines.is_empty() || (lines.len() == 1 && lines[0].is_empty()) {
            return 1;
        }

        let text = Text::from(lines.join("\n"));
        let paragraph = Paragraph::new(text).wrap(ratatui::widgets::Wrap { trim: true });
        let count = paragraph.line_count(width) as u16;
        count.clamp(1, max_height)
    }
}

// ---------------------------------------------------------------------------
// Widget
// ---------------------------------------------------------------------------

/// Input widget for the main prompt.
///
/// This widget wraps the textarea and adds a prompt character ("> ").
/// The textarea is rendered as a StatefulWidget using TextArea::widget().
pub struct Input<'a> {
    theme: &'a Theme,
    placeholder: Option<&'a str>,
}

impl<'a> Input<'a> {
    pub fn new(theme: &'a Theme) -> Self {
        Self {
            theme,
            placeholder: None,
        }
    }

    pub fn with_placeholder(mut self, placeholder: &'a str) -> Self {
        self.placeholder = Some(placeholder);
        self
    }
}

impl ratatui::widgets::StatefulWidget for Input<'_> {
    type State = InputState;

    fn render(self, area: Rect, buf: &mut ratatui::buffer::Buffer, state: &mut Self::State) {
        if area.height < 1 || area.width < 4 {
            return;
        }

        let y = area.y;

        // Configure the textarea with oxi styling
        let textarea = state.textarea_mut();
        textarea.set_style(Style::default().fg(self.theme.colors.foreground.to_ratatui()));
        textarea.set_cursor_style(
            Style::default()
                .fg(self.theme.colors.cursor_fg.to_ratatui())
                .bg(self.theme.colors.cursor_bg.to_ratatui()),
        );
        textarea.set_cursor_line_style(Style::default());
        textarea.remove_line_number();

        // Placeholder style — text is managed by InputState::set_placeholder()
        textarea.set_placeholder_style(Style::default().fg(self.theme.colors.muted.to_ratatui()));

        // Render the textarea widget.
        // No prompt symbol is shown (the `> ` prefix was removed),
        // so use minimal 1-char horizontal padding to maximize input width.
        let content_area = Rect {
            x: area.x + 1,
            y,
            width: area.width.saturating_sub(2), // 1 left + 1 right padding
            height: area.height,
        };

        // Clone textarea for rendering (TextArea implements Clone)
        let textarea_clone = textarea.clone();
        textarea_clone.render(content_area, buf);
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn input_state_empty() {
        let state = InputState::default();
        assert!(state.text().is_empty());
    }

    #[test]
    fn input_state_insert() {
        let mut state = InputState::default();
        state.handle_char('a');
        assert_eq!(state.text(), "a");
        state.handle_char('b');
        assert_eq!(state.text(), "ab");
        state.handle_char('\u{0041}'); // 'A'
        assert_eq!(state.text(), "abA");
    }

    #[test]
    fn input_state_insert_str() {
        let mut state = InputState::default();
        state.insert_str("HelloWorld");
        assert_eq!(state.text(), "HelloWorld");
    }

    #[test]
    fn input_state_multiline() {
        let mut state = InputState::default();
        state.handle_char('a');
        state.handle_input(TextAreaInput {
            key: Key::Enter,
            shift: true, // Shift+Enter = newline
            ..Default::default()
        });
        state.handle_char('b');
        assert_eq!(state.text(), "a\nb");
    }

    #[test]
    fn input_state_clear() {
        let mut state = InputState::default();
        state.insert_str("hello");
        state.clear();
        assert!(state.text().is_empty());
    }

    #[test]
    fn input_state_undo_redo() {
        let mut state = InputState::default();
        state.insert_str("hello");
        assert_eq!(state.text(), "hello");
        state.undo();
        assert_eq!(state.text(), "");
        state.redo();
        assert_eq!(state.text(), "hello");
    }

    // ── required_height tests ──

    #[test]
    fn required_height_empty() {
        let state = InputState::default();
        assert_eq!(state.required_height(80, 8), 1);
    }

    #[test]
    fn required_height_short_text() {
        let mut state = InputState::default();
        state.insert_str("hello world");
        // Short text that fits on one line
        assert_eq!(state.required_height(80, 8), 1);
    }

    #[test]
    fn required_height_long_line_wraps() {
        let mut state = InputState::default();
        // Fill with text longer than width
        let long_text = "a".repeat(200);
        state.insert_str(&long_text);
        // With width 80, this should wrap
        let height = state.required_height(80, 8);
        assert!(
            height >= 2,
            "Long text should wrap to multiple lines, got {}",
            height
        );
    }

    #[test]
    fn required_height_explicit_newlines() {
        let mut state = InputState::default();
        state.insert_str("line1\nline2\nline3");
        // 3 explicit lines
        assert_eq!(state.required_height(80, 8), 3);
    }

    #[test]
    #[allow(trivial_casts)]
    fn required_height_mixed_wrapping() {
        let mut state = InputState::default();
        state.insert_str("short\n");
        state.insert_str(&"a".repeat(200)); // Long line that wraps
                                            // 1 short line + wrapped long line
        let height = state.required_height(80, 8);
        assert!(
            height >= 2,
            "Mixed content should need multiple lines, got {}",
            height
        );
    }

    #[test]
    fn required_height_max_height_clamp() {
        let mut state = InputState::default();
        // Add many lines
        for i in 0..20 {
            state.insert_str(&format!("line {}\n", i));
        }
        // Should be clamped to max_height
        assert_eq!(state.required_height(80, 5), 5);
    }

    #[test]
    fn required_height_zero_width() {
        let mut state = InputState::default();
        state.insert_str("hello");
        // Zero width should return 1
        assert_eq!(state.required_height(0, 8), 1);
    }
}