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
use crate::input::event::{Event, KeyCode, KeyEventKind, KeyModifiers};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::widgets::Widget;
/// A reusable Text Input widget.
#[derive(Clone, Debug)]
pub struct TextInput {
pub value: String,
pub cursor_position: usize,
pub style: Style,
pub cursor_style: Style,
pub placeholder: String,
pub placeholder_style: Style,
}
impl Default for TextInput {
fn default() -> Self {
Self {
value: String::new(),
cursor_position: 0,
style: Style::default().fg(Color::White),
cursor_style: Style::default().bg(Color::White).fg(Color::Black),
placeholder: String::new(),
placeholder_style: Style::default().fg(Color::DarkGray),
}
}
}
impl TextInput {
pub fn new() -> Self {
Self::default()
}
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.value = value.into();
self.cursor_position = self.value.len();
self
}
pub fn with_placeholder(mut self, placeholder: impl Into<String>) -> Self {
self.placeholder = placeholder.into();
self
}
pub fn set_value(&mut self, value: String) {
self.value = value;
self.cursor_position = self.value.len();
}
pub fn clear(&mut self) {
self.value.clear();
self.cursor_position = 0;
}
/// Handles an input event. Returns true if the input was modified.
pub fn handle_event(&mut self, event: &Event) -> bool {
if let Event::Key(key) = event {
if key.kind != KeyEventKind::Press {
return false;
}
let has_control = key.modifiers.contains(KeyModifiers::CONTROL);
let has_alt = key.modifiers.contains(KeyModifiers::ALT);
match key.code {
KeyCode::Char(c) if !has_control && !has_alt => {
if c == '\x1b' {
return false;
}
if self.cursor_position >= self.value.len() {
self.value.push(c);
} else {
let mut new_val = String::with_capacity(self.value.len() + 1);
for (i, ch) in self.value.chars().enumerate() {
if i == self.cursor_position {
new_val.push(c);
}
new_val.push(ch);
}
self.value = new_val;
}
self.cursor_position += 1;
return true;
}
// Ctrl+u: Clear to start
KeyCode::Char('u') if has_control => {
if self.cursor_position > 0 {
self.value = self.value.chars().skip(self.cursor_position).collect();
self.cursor_position = 0;
return true;
}
}
// Ctrl+k: Clear to end
KeyCode::Char('k') if has_control => {
if self.cursor_position < self.value.len() {
self.value = self.value.chars().take(self.cursor_position).collect();
return true;
}
}
// Ctrl+w / Ctrl+Backspace / Alt+Backspace: Delete word backwards
KeyCode::Char('w') if has_control => {
return self.delete_word_backwards();
}
KeyCode::Backspace if has_control || has_alt => {
return self.delete_word_backwards();
}
// Ctrl+Delete: Delete word forwards
KeyCode::Delete if has_control || has_alt => {
return self.delete_word_forwards();
}
// Ctrl+a: Start of line
KeyCode::Char('a') if has_control => {
self.cursor_position = 0;
return true;
}
// Ctrl+e: End of line (Note: might be shadowed by global shortcuts)
KeyCode::Char('e') if has_control => {
self.cursor_position = self.value.len();
return true;
}
// Ctrl+f: Move right
KeyCode::Char('f') if has_control => {
if self.cursor_position < self.value.len() {
self.cursor_position += 1;
return true;
}
}
// Ctrl+b: Move left
KeyCode::Char('b') if has_control => {
if self.cursor_position > 0 {
self.cursor_position -= 1;
return true;
}
}
KeyCode::Backspace => {
if self.cursor_position > 0 {
let mut new_val = String::with_capacity(self.value.len());
for (i, ch) in self.value.chars().enumerate() {
if i != self.cursor_position - 1 {
new_val.push(ch);
}
}
self.value = new_val;
self.cursor_position -= 1;
return true;
}
}
KeyCode::Delete => {
if self.cursor_position < self.value.len() {
let mut new_val = String::with_capacity(self.value.len());
for (i, ch) in self.value.chars().enumerate() {
if i != self.cursor_position {
new_val.push(ch);
}
}
self.value = new_val;
return true;
}
}
KeyCode::Left => {
if self.cursor_position > 0 {
self.cursor_position -= 1;
return true;
}
}
KeyCode::Right => {
if self.cursor_position < self.value.len() {
self.cursor_position += 1;
return true;
}
}
KeyCode::Home => {
self.cursor_position = 0;
return true;
}
KeyCode::End => {
self.cursor_position = self.value.len();
return true;
}
_ => {}
}
}
false
}
fn delete_word_backwards(&mut self) -> bool {
if self.cursor_position == 0 {
return false;
}
let mut i = self.cursor_position;
// Skip trailing whitespace
while i > 0 {
let prev = self.value[..i].chars().next_back().unwrap();
if prev.is_whitespace() {
i -= prev.len_utf8();
} else {
break;
}
}
// Skip the word
while i > 0 {
let prev = self.value[..i].chars().next_back().unwrap();
if !prev.is_whitespace() {
i -= prev.len_utf8();
} else {
break;
}
}
let tail = self.value.split_off(self.cursor_position);
self.value.truncate(i);
self.value.push_str(&tail);
self.cursor_position = i;
true
}
fn delete_word_forwards(&mut self) -> bool {
if self.cursor_position >= self.value.len() {
return false;
}
let mut i = self.cursor_position;
// Skip trailing whitespace
while i < self.value.len() {
let next = self.value[i..].chars().next().unwrap();
if next.is_whitespace() {
i += next.len_utf8();
} else {
break;
}
}
// Skip the word
while i < self.value.len() {
let next = self.value[i..].chars().next().unwrap();
if !next.is_whitespace() {
i += next.len_utf8();
} else {
break;
}
}
let tail = self.value.split_off(i);
self.value.truncate(self.cursor_position);
self.value.push_str(&tail);
true
}
}
impl Widget for &TextInput {
fn render(self, area: Rect, buf: &mut Buffer) {
let display_text = if self.value.is_empty() {
&self.placeholder
} else {
&self.value
};
let style = if self.value.is_empty() {
self.placeholder_style
} else {
self.style
};
buf.set_string(area.x, area.y, display_text, style);
// Draw Cursor
// Only if focused/active? Assuming this widget is only rendered when active or we rely on caller.
// We'll draw cursor if it's within bounds.
// If text exceeds width, we should scroll. Implementing basic scrolling:
let cursor_x = area.x + self.cursor_position as u16;
// Simple horizontal scrolling logic (viewport follows cursor)
// This is complex for a stateless render if we don't store "scroll_offset".
// For simplicity, let's assume we render from the start, or we need to add `scroll_offset` to state.
// To keep it simple for now: No scrolling, just clamp cursor.
if cursor_x < area.x + area.width {
if let Some(cell) = buf.cell_mut((cursor_x, area.y)) {
cell.set_style(self.cursor_style);
if self.cursor_position < self.value.len() {
// If cursor is over a character, ensure char is visible
let c = self.value.chars().nth(self.cursor_position).unwrap_or(' ');
cell.set_symbol(&c.to_string());
} else {
cell.set_symbol(" ");
}
}
}
}
}