fresh-editor 0.1.96

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! Single-line text input control
//!
//! Renders as: `Label: [text content     ]`
//!
//! This module provides a complete text input component with:
//! - State management (`TextInputState`)
//! - Rendering (`render_text_input`, `render_text_input_aligned`)
//! - Input handling (`TextInputState::handle_mouse`, `handle_key`)
//! - Layout/hit testing (`TextInputLayout`)

mod input;
mod render;

use crate::primitives::grapheme;
use ratatui::layout::Rect;
use ratatui::style::Color;

pub use input::TextInputEvent;
pub use render::{render_text_input, render_text_input_aligned};

use super::FocusState;

/// State for a text input control
#[derive(Debug, Clone)]
pub struct TextInputState {
    /// Current text value
    pub value: String,
    /// Cursor position (character index)
    pub cursor: usize,
    /// Label displayed before the input
    pub label: String,
    /// Placeholder text when empty
    pub placeholder: String,
    /// Focus state
    pub focus: FocusState,
    /// If true, validate that value is valid JSON before allowing exit
    pub validate_json: bool,
}

impl TextInputState {
    /// Create a new text input state
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            value: String::new(),
            cursor: 0,
            label: label.into(),
            placeholder: String::new(),
            focus: FocusState::Normal,
            validate_json: false,
        }
    }

    /// Set JSON validation mode
    pub fn with_json_validation(mut self) -> Self {
        self.validate_json = true;
        self
    }

    /// Check if the current value is valid (valid JSON if validate_json is set)
    pub fn is_valid(&self) -> bool {
        if self.validate_json {
            serde_json::from_str::<serde_json::Value>(&self.value).is_ok()
        } else {
            true
        }
    }

    /// Set the initial value
    pub fn with_value(mut self, value: impl Into<String>) -> Self {
        self.value = value.into();
        self.cursor = self.value.len();
        self
    }

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

    /// Set the focus state
    pub fn with_focus(mut self, focus: FocusState) -> Self {
        self.focus = focus;
        self
    }

    /// Check if the control is enabled
    pub fn is_enabled(&self) -> bool {
        self.focus != FocusState::Disabled
    }

    /// Insert a character at the cursor position
    pub fn insert(&mut self, c: char) {
        if !self.is_enabled() {
            return;
        }
        self.value.insert(self.cursor, c);
        self.cursor += c.len_utf8();
    }

    /// Insert a string at the cursor position
    pub fn insert_str(&mut self, s: &str) {
        if !self.is_enabled() {
            return;
        }
        self.value.insert_str(self.cursor, s);
        self.cursor += s.len();
    }

    /// Delete the character before the cursor (backspace)
    pub fn backspace(&mut self) {
        if !self.is_enabled() || self.cursor == 0 {
            return;
        }
        // Find the previous character boundary
        let prev_boundary = self.value[..self.cursor]
            .char_indices()
            .next_back()
            .map(|(i, _)| i)
            .unwrap_or(0);
        self.value.remove(prev_boundary);
        self.cursor = prev_boundary;
    }

    /// Delete the grapheme cluster at the cursor (delete key)
    ///
    /// Deletes the entire grapheme cluster, handling combining characters properly.
    pub fn delete(&mut self) {
        if !self.is_enabled() || self.cursor >= self.value.len() {
            return;
        }
        let next_boundary = grapheme::next_grapheme_boundary(&self.value, self.cursor);
        self.value.drain(self.cursor..next_boundary);
    }

    /// Move cursor left (to previous grapheme cluster boundary)
    ///
    /// Uses grapheme cluster boundaries for proper handling of combining characters
    /// like Thai diacritics, emoji with modifiers, etc.
    pub fn move_left(&mut self) {
        if self.cursor > 0 {
            self.cursor = grapheme::prev_grapheme_boundary(&self.value, self.cursor);
        }
    }

    /// Move cursor right (to next grapheme cluster boundary)
    ///
    /// Uses grapheme cluster boundaries for proper handling of combining characters
    /// like Thai diacritics, emoji with modifiers, etc.
    pub fn move_right(&mut self) {
        if self.cursor < self.value.len() {
            self.cursor = grapheme::next_grapheme_boundary(&self.value, self.cursor);
        }
    }

    /// Move cursor to start
    pub fn move_home(&mut self) {
        self.cursor = 0;
    }

    /// Move cursor to end
    pub fn move_end(&mut self) {
        self.cursor = self.value.len();
    }

    /// Clear the input
    pub fn clear(&mut self) {
        if self.is_enabled() {
            self.value.clear();
            self.cursor = 0;
        }
    }

    /// Set the value directly
    pub fn set_value(&mut self, value: impl Into<String>) {
        if self.is_enabled() {
            self.value = value.into();
            self.cursor = self.value.len();
        }
    }
}

/// Colors for the text input control
#[derive(Debug, Clone, Copy)]
pub struct TextInputColors {
    /// Label color
    pub label: Color,
    /// Input text color
    pub text: Color,
    /// Border/bracket color
    pub border: Color,
    /// Placeholder text color
    pub placeholder: Color,
    /// Cursor color
    pub cursor: Color,
    /// Focused highlight color
    pub focused: Color,
    /// Disabled color
    pub disabled: Color,
}

impl Default for TextInputColors {
    fn default() -> Self {
        Self {
            label: Color::White,
            text: Color::White,
            border: Color::Gray,
            placeholder: Color::DarkGray,
            cursor: Color::Yellow,
            focused: Color::Cyan,
            disabled: Color::DarkGray,
        }
    }
}

impl TextInputColors {
    /// Create colors from theme
    pub fn from_theme(theme: &crate::view::theme::Theme) -> Self {
        Self {
            label: theme.editor_fg,
            text: theme.editor_fg,
            border: theme.line_number_fg,
            placeholder: theme.line_number_fg,
            cursor: theme.cursor,
            focused: theme.selection_bg,
            disabled: theme.line_number_fg,
        }
    }
}

/// Layout information returned after rendering for hit testing
#[derive(Debug, Clone, Copy, Default)]
pub struct TextInputLayout {
    /// The text input field area
    pub input_area: Rect,
    /// The full control area including label
    pub full_area: Rect,
    /// Cursor position in screen coordinates (if focused)
    pub cursor_pos: Option<(u16, u16)>,
}

impl TextInputLayout {
    /// Check if a point is within the input area
    pub fn is_input(&self, x: u16, y: u16) -> bool {
        x >= self.input_area.x
            && x < self.input_area.x + self.input_area.width
            && y >= self.input_area.y
            && y < self.input_area.y + self.input_area.height
    }

    /// Check if a point is within the full control area
    pub fn contains(&self, x: u16, y: u16) -> bool {
        x >= self.full_area.x
            && x < self.full_area.x + self.full_area.width
            && y >= self.full_area.y
            && y < self.full_area.y + self.full_area.height
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::backend::TestBackend;
    use ratatui::Terminal;

    fn test_frame<F>(width: u16, height: u16, f: F)
    where
        F: FnOnce(&mut ratatui::Frame, Rect),
    {
        let backend = TestBackend::new(width, height);
        let mut terminal = Terminal::new(backend).unwrap();
        terminal
            .draw(|frame| {
                let area = Rect::new(0, 0, width, height);
                f(frame, area);
            })
            .unwrap();
    }

    #[test]
    fn test_text_input_renders() {
        test_frame(40, 1, |frame, area| {
            let state = TextInputState::new("Name").with_value("John");
            let colors = TextInputColors::default();
            let layout = render_text_input(frame, area, &state, &colors, 20);

            assert!(layout.input_area.width > 0);
        });
    }

    #[test]
    fn test_text_input_insert() {
        let mut state = TextInputState::new("Test");
        state.insert('a');
        state.insert('b');
        state.insert('c');
        assert_eq!(state.value, "abc");
        assert_eq!(state.cursor, 3);
    }

    #[test]
    fn test_text_input_backspace() {
        let mut state = TextInputState::new("Test").with_value("abc");
        state.backspace();
        assert_eq!(state.value, "ab");
        assert_eq!(state.cursor, 2);
    }

    #[test]
    fn test_text_input_cursor_movement() {
        let mut state = TextInputState::new("Test").with_value("hello");
        assert_eq!(state.cursor, 5);

        state.move_left();
        assert_eq!(state.cursor, 4);

        state.move_home();
        assert_eq!(state.cursor, 0);

        state.move_right();
        assert_eq!(state.cursor, 1);

        state.move_end();
        assert_eq!(state.cursor, 5);
    }

    #[test]
    fn test_text_input_delete() {
        let mut state = TextInputState::new("Test").with_value("abc");
        state.move_home();
        state.delete();
        assert_eq!(state.value, "bc");
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn test_text_input_disabled() {
        let mut state = TextInputState::new("Test").with_focus(FocusState::Disabled);
        state.insert('a');
        assert_eq!(state.value, "");
    }

    #[test]
    fn test_text_input_clear() {
        let mut state = TextInputState::new("Test").with_value("hello");
        state.clear();
        assert_eq!(state.value, "");
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn test_text_input_multibyte_insert_and_backspace() {
        // Regression test for issue #466: panic when backspacing multi-byte chars
        let mut state = TextInputState::new("Test");
        // © is 2 bytes in UTF-8
        state.insert('©');
        assert_eq!(state.value, "©");
        assert_eq!(state.cursor, 2); // byte position, not char position

        // Backspace should delete the whole character, not cause a panic
        state.backspace();
        assert_eq!(state.value, "");
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn test_text_input_multibyte_cursor_movement() {
        let mut state = TextInputState::new("Test").with_value("日本語");
        // Each Japanese character is 3 bytes
        assert_eq!(state.cursor, 9);

        state.move_left();
        assert_eq!(state.cursor, 6); // moved back by one character (3 bytes)

        state.move_left();
        assert_eq!(state.cursor, 3);

        state.move_right();
        assert_eq!(state.cursor, 6);

        state.move_home();
        assert_eq!(state.cursor, 0);

        state.move_right();
        assert_eq!(state.cursor, 3); // moved forward by one character (3 bytes)
    }

    #[test]
    fn test_text_input_multibyte_delete() {
        let mut state = TextInputState::new("Test").with_value("a日b");
        // 'a' is 1 byte, '日' is 3 bytes, 'b' is 1 byte = 5 bytes total
        assert_eq!(state.cursor, 5);

        state.move_home();
        state.move_right(); // cursor now at byte 1 (after 'a', before '日')
        assert_eq!(state.cursor, 1);

        state.delete(); // delete '日'
        assert_eq!(state.value, "ab");
        assert_eq!(state.cursor, 1);
    }

    #[test]
    fn test_text_input_insert_between_multibyte() {
        let mut state = TextInputState::new("Test").with_value("日語");
        state.move_home();
        state.move_right(); // cursor after first character
        assert_eq!(state.cursor, 3);

        state.insert('');
        assert_eq!(state.value, "日本語");
        assert_eq!(state.cursor, 6);
    }
}