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
/// Single-line text input buffer for the top search bar and bottom AI prompt.
/// Stores the buffer plus a byte-cursor; rendering uses `chars()` so multibyte
/// characters (Cyrillic etc.) display correctly.
#[derive(Debug, Default, Clone)]
pub struct TextInput {
buffer: String,
/// Cursor position as a *character* index, not a byte index.
cursor: usize,
}
impl TextInput {
pub fn new() -> Self {
Self::default()
}
pub fn as_str(&self) -> &str {
&self.buffer
}
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
#[allow(dead_code)]
pub fn cursor(&self) -> usize {
self.cursor
}
/// Render the buffer for display with `cursor_char` placed at the actual
/// cursor position. Use this everywhere we draw a single-line text input
/// — otherwise the visual cursor lags behind the real position and edits
/// in the middle of the buffer look like characters are being scrambled.
pub fn render_with_cursor(&self, cursor_char: char) -> String {
let chars: Vec<char> = self.buffer.chars().collect();
let mut out = String::with_capacity(self.buffer.len() + 1);
for (i, c) in chars.iter().enumerate() {
if i == self.cursor {
out.push(cursor_char);
}
out.push(*c);
}
if self.cursor >= chars.len() {
out.push(cursor_char);
}
out
}
pub fn clear(&mut self) {
self.buffer.clear();
self.cursor = 0;
}
/// 1.2.8+ — replace the full buffer and set the cursor
/// in one step (char-index). Used by the shell pane's
/// Tab autocomplete to swap a token for its completion.
/// Clamps cursor to the new buffer's char length.
pub fn set_with_cursor(&mut self, text: String, cursor_chars: usize) {
let len = text.chars().count();
self.buffer = text;
self.cursor = cursor_chars.min(len);
}
pub fn insert_char(&mut self, c: char) {
let byte_idx = self.byte_offset(self.cursor);
self.buffer.insert(byte_idx, c);
self.cursor += 1;
}
pub fn backspace(&mut self) {
if self.cursor == 0 {
return;
}
let prev_byte = self.byte_offset(self.cursor - 1);
let cur_byte = self.byte_offset(self.cursor);
self.buffer.replace_range(prev_byte..cur_byte, "");
self.cursor -= 1;
}
pub fn delete(&mut self) {
let len = self.buffer.chars().count();
if self.cursor >= len {
return;
}
let cur_byte = self.byte_offset(self.cursor);
let next_byte = self.byte_offset(self.cursor + 1);
self.buffer.replace_range(cur_byte..next_byte, "");
}
pub fn move_left(&mut self) {
if self.cursor > 0 {
self.cursor -= 1;
}
}
pub fn move_right(&mut self) {
let len = self.buffer.chars().count();
if self.cursor < len {
self.cursor += 1;
}
}
pub fn move_home(&mut self) {
self.cursor = 0;
}
pub fn move_end(&mut self) {
self.cursor = self.buffer.chars().count();
}
/// 1.2.8+ — kill from the cursor to the start of the
/// buffer (readline Ctrl+U). The deleted text is NOT
/// captured into a yank ring — single-line prompts don't
/// have the multi-stash workflow that justifies one.
pub fn kill_to_start(&mut self) {
let cur_byte = self.byte_offset(self.cursor);
self.buffer.replace_range(0..cur_byte, "");
self.cursor = 0;
}
/// 1.2.8+ — kill from the cursor to the end of the buffer
/// (readline Ctrl+K).
pub fn kill_to_end(&mut self) {
let cur_byte = self.byte_offset(self.cursor);
self.buffer.truncate(cur_byte);
}
/// 1.2.8+ — move the cursor backward to the start of the
/// previous word. Words are defined as runs of
/// non-whitespace, non-punctuation chars (same convention
/// as readline's `\b` / Alt+B).
pub fn move_word_left(&mut self) {
let chars: Vec<char> = self.buffer.chars().collect();
let mut i = self.cursor;
// Skip any whitespace immediately before the cursor.
while i > 0 && is_word_separator(chars[i - 1]) {
i -= 1;
}
// Walk back through word chars.
while i > 0 && !is_word_separator(chars[i - 1]) {
i -= 1;
}
self.cursor = i;
}
/// 1.2.8+ — move the cursor forward to the end of the
/// next word. Mirrors `move_word_left`.
pub fn move_word_right(&mut self) {
let chars: Vec<char> = self.buffer.chars().collect();
let len = chars.len();
let mut i = self.cursor;
// Walk forward through word chars first.
while i < len && !is_word_separator(chars[i]) {
i += 1;
}
// Then skip trailing separator(s) to land at the next
// word's start.
while i < len && is_word_separator(chars[i]) {
i += 1;
}
self.cursor = i;
}
/// 1.2.8+ — kill the word immediately before the cursor
/// (readline Ctrl+W / Alt+Backspace). Uses the same word
/// definition as `move_word_left`.
pub fn kill_word_left(&mut self) {
let start_cursor = self.cursor;
self.move_word_left();
let kill_start_byte = self.byte_offset(self.cursor);
let kill_end_byte = self.byte_offset(start_cursor);
self.buffer.replace_range(kill_start_byte..kill_end_byte, "");
}
fn byte_offset(&self, char_idx: usize) -> usize {
self.buffer
.char_indices()
.nth(char_idx)
.map(|(b, _)| b)
.unwrap_or(self.buffer.len())
}
}
/// 1.2.8+ — predicate used by `move_word_*` / `kill_word_*`.
/// Whitespace and ASCII punctuation chars break word runs;
/// everything else (letters, digits, underscores, non-ASCII
/// letters) is part of a word. Matches readline + most
/// editors so Ctrl+W jumps to the start of the identifier
/// the cursor sits in, regardless of language.
fn is_word_separator(c: char) -> bool {
c.is_whitespace()
|| matches!(
c,
'|' | ';' | '(' | ')' | '[' | ']' | '{' | '}'
| ',' | '.' | ':' | '/' | '\\' | '"' | '\''
| '`' | '<' | '>' | '!' | '?' | '*' | '&'
| '=' | '+' | '~'
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ascii_insert_and_backspace() {
let mut t = TextInput::new();
t.insert_char('h');
t.insert_char('i');
assert_eq!(t.as_str(), "hi");
t.backspace();
assert_eq!(t.as_str(), "h");
assert_eq!(t.cursor(), 1);
}
#[test]
fn unicode_insert_and_navigate() {
let mut t = TextInput::new();
for c in "утро".chars() {
t.insert_char(c);
}
assert_eq!(t.as_str(), "утро");
assert_eq!(t.cursor(), 4);
t.move_left();
t.move_left();
t.backspace();
assert_eq!(t.as_str(), "уро");
}
#[test]
fn middle_insert_round_trip() {
let mut t = TextInput::new();
for c in "Hello".chars() {
t.insert_char(c);
}
// Move cursor between 'e' and 'l'.
t.move_home();
t.move_right();
t.move_right();
t.insert_char('X');
assert_eq!(t.as_str(), "HeXllo");
assert_eq!(t.cursor(), 3);
}
#[test]
fn render_with_cursor_in_middle() {
let mut t = TextInput::new();
for c in "Hi".chars() {
t.insert_char(c);
}
// Cursor at end.
assert_eq!(t.render_with_cursor('│'), "Hi│");
// Cursor at start.
t.move_home();
assert_eq!(t.render_with_cursor('│'), "│Hi");
// Cursor in middle.
t.move_right();
assert_eq!(t.render_with_cursor('│'), "H│i");
}
#[test]
fn home_end_move_cursor() {
let mut t = TextInput::new();
for c in "hello world".chars() {
t.insert_char(c);
}
assert_eq!(t.cursor(), 11);
t.move_home();
assert_eq!(t.cursor(), 0);
assert_eq!(t.render_with_cursor('│'), "│hello world");
t.move_end();
assert_eq!(t.cursor(), 11);
assert_eq!(t.render_with_cursor('│'), "hello world│");
}
#[test]
fn delete_at_cursor() {
let mut t = TextInput::new();
for c in "abcde".chars() {
t.insert_char(c);
}
t.move_home();
t.move_right(); // cursor between a and b
t.delete(); // removes 'b'
assert_eq!(t.as_str(), "acde");
assert_eq!(t.cursor(), 1);
}
#[test]
fn kill_to_start_and_end() {
let mut t = TextInput::new();
for c in "hello world".chars() {
t.insert_char(c);
}
// cursor at end → kill_to_start clears everything.
t.kill_to_start();
assert_eq!(t.as_str(), "");
assert_eq!(t.cursor(), 0);
for c in "hello world".chars() {
t.insert_char(c);
}
// cursor between 'hello ' and 'world' → kill_to_end
// leaves "hello ".
t.move_home();
for _ in 0..6 {
t.move_right();
}
t.kill_to_end();
assert_eq!(t.as_str(), "hello ");
assert_eq!(t.cursor(), 6);
}
#[test]
fn word_navigation_and_kill() {
let mut t = TextInput::new();
for c in "git status --short".chars() {
t.insert_char(c);
}
// Hyphen is intentionally NOT a separator so `--short`
// counts as one logical word (a CLI flag). From the
// end, three move_word_left jumps land on:
// `--short` start, `status` start, `git` start.
t.move_word_left();
assert_eq!(t.cursor(), "git status ".len());
t.move_word_left();
assert_eq!(t.cursor(), "git ".len());
t.move_word_left();
assert_eq!(t.cursor(), 0);
// Forward: from start, jump past `git`, land on `status`.
t.move_word_right();
assert_eq!(t.cursor(), "git ".len());
// Kill word left at end of buffer.
let mut t2 = TextInput::new();
for c in "git status".chars() {
t2.insert_char(c);
}
t2.kill_word_left();
assert_eq!(t2.as_str(), "git ");
}
#[test]
fn render_with_cursor_unicode() {
let mut t = TextInput::new();
for c in "утро".chars() {
t.insert_char(c);
}
t.move_left();
t.move_left();
assert_eq!(t.render_with_cursor('│'), "ут│ро");
}
}