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
use crate::core::geometry::Rect;
use crate::core::event::{Event, EventType, KB_ENTER, KB_BACKSPACE, KB_LEFT, KB_RIGHT, KB_HOME, KB_END, KB_DEL};
use crate::core::draw::DrawBuffer;
use crate::core::palette::colors;
use crate::core::clipboard;
use crate::terminal::Terminal;
use super::view::{View, write_line_to_terminal};
use std::rc::Rc;
use std::cell::RefCell;
// Control key codes
const KB_CTRL_A: u16 = 0x0001; // Ctrl+A - Select All
const KB_CTRL_C: u16 = 0x0003; // Ctrl+C - Copy
const KB_CTRL_V: u16 = 0x0016; // Ctrl+V - Paste
const KB_CTRL_X: u16 = 0x0018; // Ctrl+X - Cut
pub struct InputLine {
bounds: Rect,
data: Rc<RefCell<String>>,
cursor_pos: usize,
max_length: usize,
focused: bool,
sel_start: usize, // Selection start position
sel_end: usize, // Selection end position
first_pos: usize, // First visible character position for horizontal scrolling
}
impl InputLine {
pub fn new(bounds: Rect, max_length: usize, data: Rc<RefCell<String>>) -> Self {
let cursor_pos = data.borrow().len();
Self {
bounds,
data,
cursor_pos,
max_length,
focused: false,
sel_start: 0,
sel_end: 0,
first_pos: 0,
}
}
pub fn set_text(&mut self, text: String) {
*self.data.borrow_mut() = text;
self.cursor_pos = self.data.borrow().len();
self.sel_start = 0;
self.sel_end = 0;
self.first_pos = 0;
}
pub fn get_text(&self) -> String {
self.data.borrow().clone()
}
pub fn set_focused(&mut self, focused: bool) {
self.focused = focused;
}
/// Select all text
pub fn select_all(&mut self) {
let len = self.data.borrow().len();
self.sel_start = 0;
self.sel_end = len;
self.cursor_pos = len;
}
/// Check if there's an active selection
pub fn has_selection(&self) -> bool {
self.sel_start != self.sel_end
}
/// Get the selected text
pub fn get_selection(&self) -> Option<String> {
if !self.has_selection() {
return None;
}
let text = self.data.borrow();
let start = self.sel_start.min(self.sel_end);
let end = self.sel_start.max(self.sel_end);
Some(text[start..end].to_string())
}
/// Delete the current selection
fn delete_selection(&mut self) {
if !self.has_selection() {
return;
}
let start = self.sel_start.min(self.sel_end);
let end = self.sel_start.max(self.sel_end);
let mut text = self.data.borrow_mut();
text.replace_range(start..end, "");
drop(text);
self.cursor_pos = start;
self.sel_start = 0;
self.sel_end = 0;
}
/// Ensure cursor is visible by adjusting first_pos
fn make_cursor_visible(&mut self) {
let width = self.bounds.width() as usize;
// If cursor is before the visible area
if self.cursor_pos < self.first_pos {
self.first_pos = self.cursor_pos;
}
// If cursor is after the visible area
else if self.cursor_pos >= self.first_pos + width {
self.first_pos = self.cursor_pos - width + 1;
}
}
}
impl View for InputLine {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn draw(&mut self, terminal: &mut Terminal) {
let width = self.bounds.width() as usize;
let mut buf = DrawBuffer::new(width);
let attr = if self.focused {
colors::INPUT_FOCUSED
} else {
colors::INPUT_NORMAL
};
buf.move_char(0, ' ', attr, width);
// Get text and calculate visible portion
let text = self.data.borrow();
let text_len = text.len();
// Calculate visible range
let visible_start = self.first_pos;
let visible_end = (visible_start + width).min(text_len);
// Draw text
if visible_start < text_len {
let visible_text = &text[visible_start..visible_end];
// If there's a selection, draw it with selection color
if self.has_selection() {
let sel_start = self.sel_start.min(self.sel_end);
let sel_end = self.sel_start.max(self.sel_end);
// Draw characters one by one to handle selection highlighting
for (i, ch) in visible_text.chars().enumerate() {
let pos = visible_start + i;
let char_attr = if pos >= sel_start && pos < sel_end {
colors::SELECTED
} else {
attr
};
buf.move_char(i, ch, char_attr, 1);
}
} else {
buf.move_str(0, visible_text, attr);
}
// Show left arrow if text is scrolled
if self.first_pos > 0 {
buf.move_char(0, '<', attr, 1);
}
// Show right arrow if there's more text beyond the visible area
if visible_end < text_len {
buf.move_char(width - 1, '>', attr, 1);
}
}
write_line_to_terminal(terminal, self.bounds.a.x, self.bounds.a.y, &buf);
}
fn handle_event(&mut self, event: &mut Event) {
if !self.focused {
return;
}
if event.what == EventType::Keyboard {
match event.key_code {
KB_BACKSPACE => {
if self.has_selection() {
self.delete_selection();
self.make_cursor_visible();
event.clear();
} else if self.cursor_pos > 0 {
{
let mut text = self.data.borrow_mut();
text.remove(self.cursor_pos - 1);
}
self.cursor_pos -= 1;
self.make_cursor_visible();
event.clear();
}
}
KB_DEL => {
if self.has_selection() {
self.delete_selection();
self.make_cursor_visible();
event.clear();
} else if self.cursor_pos < self.data.borrow().len() {
let mut text = self.data.borrow_mut();
text.remove(self.cursor_pos);
event.clear();
}
}
KB_LEFT => {
if self.cursor_pos > 0 {
self.cursor_pos -= 1;
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
}
KB_RIGHT => {
if self.cursor_pos < self.data.borrow().len() {
self.cursor_pos += 1;
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
}
KB_HOME => {
self.cursor_pos = 0;
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
KB_END => {
self.cursor_pos = self.data.borrow().len();
self.sel_start = 0;
self.sel_end = 0;
self.make_cursor_visible();
event.clear();
}
KB_ENTER => {
// Clear event but don't handle - let parent process it
event.clear();
}
KB_CTRL_A => {
self.select_all();
event.clear();
}
KB_CTRL_C => {
// Copy to clipboard
if let Some(selection) = self.get_selection() {
clipboard::set_clipboard(&selection);
}
event.clear();
}
KB_CTRL_X => {
// Cut to clipboard
if let Some(selection) = self.get_selection() {
clipboard::set_clipboard(&selection);
self.delete_selection();
self.make_cursor_visible();
}
event.clear();
}
KB_CTRL_V => {
// Paste from clipboard
let clipboard_text = clipboard::get_clipboard();
if !clipboard_text.is_empty() {
// Delete selection if any
if self.has_selection() {
self.delete_selection();
}
// Insert clipboard text at cursor position
{
let mut text = self.data.borrow_mut();
let remaining_space = self.max_length.saturating_sub(text.len());
let insert_text = if clipboard_text.len() <= remaining_space {
clipboard_text.as_str()
} else {
&clipboard_text[..remaining_space]
};
text.insert_str(self.cursor_pos, insert_text);
self.cursor_pos += insert_text.len();
}
self.make_cursor_visible();
}
event.clear();
}
// Regular character input
key_code => {
if (32..127).contains(&key_code) {
// Delete selection if any
if self.has_selection() {
self.delete_selection();
}
let text_len = self.data.borrow().len();
if text_len < self.max_length {
let ch = key_code as u8 as char;
{
let mut text = self.data.borrow_mut();
text.insert(self.cursor_pos, ch);
}
self.cursor_pos += 1;
self.make_cursor_visible();
event.clear();
}
}
}
}
}
}
fn can_focus(&self) -> bool {
true
}
fn set_focus(&mut self, focused: bool) {
self.focused = focused;
}
fn update_cursor(&self, terminal: &mut Terminal) {
if self.focused {
// Calculate cursor position on screen
let cursor_x = self.bounds.a.x as usize + (self.cursor_pos - self.first_pos);
let cursor_y = self.bounds.a.y;
// Show cursor at the position
let _ = terminal.show_cursor(cursor_x as u16, cursor_y as u16);
}
// Note: cursor is already hidden by Group if not focused
}
}