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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::rendering::{Cell, Charset, Theme, VideoBuffer};
use crate::utils::{CommandHistory, CommandIndexer, FuzzyMatch, FuzzyMatcher};
pub struct SlightInput {
pub prompt_text: String,
pub input_text: String,
pub width: u16,
pub height: u16,
pub x: u16,
pub y: u16,
pub cursor_position: usize,
pub input_col_start: u16,
// Autocomplete state
suggestions: Vec<FuzzyMatch>,
selected_suggestion: usize,
command_indexer: Option<CommandIndexer>,
command_history: Option<CommandHistory>,
}
impl SlightInput {
pub fn new(buffer_width: u16, buffer_height: u16) -> Self {
let prompt_text = "Enter a command to launch:".to_string();
// Calculate dimensions (reduced height since no title)
let width = 60;
let height = 6;
// Center on screen
let x = (buffer_width.saturating_sub(width)) / 2;
let y = (buffer_height.saturating_sub(height)) / 2;
// Calculate input field position (absolute coordinates)
let input_col_start = x + 3; // Border (2) + padding (1)
SlightInput {
prompt_text,
input_text: String::new(),
width,
height,
x,
y,
cursor_position: 0,
input_col_start,
suggestions: Vec::new(),
selected_suggestion: 0,
command_indexer: None,
command_history: None,
}
}
/// Sets the command indexer and history for autocomplete
pub fn set_autocomplete(&mut self, indexer: CommandIndexer, history: CommandHistory) {
self.command_indexer = Some(indexer);
self.command_history = Some(history);
self.update_suggestions();
}
/// Updates suggestions based on current input
fn update_suggestions(&mut self) {
if let (Some(indexer), Some(history)) = (&self.command_indexer, &self.command_history) {
self.suggestions = FuzzyMatcher::find_matches(
&self.input_text,
indexer.get_commands(),
history,
5, // Max 5 suggestions
);
self.selected_suggestion = 0;
}
}
/// Gets the currently selected suggestion (for inline display)
fn get_inline_suggestion(&self) -> Option<&str> {
if self.suggestions.is_empty() {
return None;
}
let suggestion = &self.suggestions[self.selected_suggestion].command;
// Only show inline if it starts with current input
if suggestion
.to_lowercase()
.starts_with(&self.input_text.to_lowercase())
{
Some(suggestion)
} else {
None
}
}
pub fn render(&self, buffer: &mut VideoBuffer, charset: &Charset, theme: &Theme) {
let content_width = self.width.saturating_sub(4); // Remove borders (2 on each side)
// Fill entire popup with background
for row in 0..self.height {
for col in 0..self.width {
let x = self.x + col;
let y = self.y + row;
buffer.set(x, y, Cell::new(' ', theme.slight_fg, theme.slight_bg));
}
}
// Draw border
// Top border
buffer.set(
self.x,
self.y,
Cell::new(
charset.border_top_left,
theme.slight_border,
theme.slight_bg,
),
);
for col in 1..self.width - 1 {
buffer.set(
self.x + col,
self.y,
Cell::new(
charset.border_horizontal,
theme.slight_border,
theme.slight_bg,
),
);
}
buffer.set(
self.x + self.width - 1,
self.y,
Cell::new(
charset.border_top_right,
theme.slight_border,
theme.slight_bg,
),
);
// Side borders
for row in 1..self.height - 1 {
buffer.set(
self.x,
self.y + row,
Cell::new(
charset.border_vertical,
theme.slight_border,
theme.slight_bg,
),
);
buffer.set(
self.x + 1,
self.y + row,
Cell::new(' ', theme.slight_fg, theme.slight_bg),
);
buffer.set(
self.x + self.width - 2,
self.y + row,
Cell::new(' ', theme.slight_fg, theme.slight_bg),
);
buffer.set(
self.x + self.width - 1,
self.y + row,
Cell::new(
charset.border_vertical,
theme.slight_border,
theme.slight_bg,
),
);
}
// Bottom border
buffer.set(
self.x,
self.y + self.height - 1,
Cell::new(
charset.border_bottom_left,
theme.slight_border,
theme.slight_bg,
),
);
for col in 1..self.width - 1 {
buffer.set(
self.x + col,
self.y + self.height - 1,
Cell::new(
charset.border_horizontal,
theme.slight_border,
theme.slight_bg,
),
);
}
buffer.set(
self.x + self.width - 1,
self.y + self.height - 1,
Cell::new(
charset.border_bottom_right,
theme.slight_border,
theme.slight_bg,
),
);
// Render prompt text (centered)
let prompt_y = self.y + 1;
let prompt_start =
self.x + 2 + (content_width.saturating_sub(self.prompt_text.len() as u16)) / 2;
for (i, ch) in self.prompt_text.chars().enumerate() {
if i < content_width as usize {
buffer.set(
prompt_start + i as u16,
prompt_y,
Cell::new(ch, theme.slight_fg, theme.slight_bg),
);
}
}
// Render input field background
let input_y = self.y + 3;
let input_field_width = content_width.saturating_sub(2); // Add some padding
for col in 0..input_field_width {
buffer.set(
self.input_col_start + col,
input_y,
Cell::new(' ', theme.slight_input_fg, theme.slight_input_bg),
);
}
// Render input text
for (i, ch) in self.input_text.chars().enumerate() {
if i < input_field_width as usize {
buffer.set(
self.input_col_start + i as u16,
input_y,
Cell::new(ch, theme.slight_input_fg, theme.slight_input_bg),
);
}
}
// Render inline suggestion (gray text after cursor)
if let Some(suggestion) = self.get_inline_suggestion() {
let suggestion_text = &suggestion[self.input_text.len()..];
for (i, ch) in suggestion_text.chars().enumerate() {
let pos = self.input_text.len() + i;
if pos < input_field_width as usize {
buffer.set(
self.input_col_start + pos as u16,
input_y,
Cell::new(ch, theme.slight_suggestion_fg, theme.slight_input_bg),
);
}
}
}
// Render cursor (if within visible area)
if self.cursor_position < input_field_width as usize {
let cursor_x = self.input_col_start + self.cursor_position as u16;
let cursor_char = if self.cursor_position < self.input_text.len() {
self.input_text
.chars()
.nth(self.cursor_position)
.unwrap_or(' ')
} else {
' '
};
buffer.set(
cursor_x,
input_y,
Cell::new(cursor_char, theme.slight_input_bg, theme.slight_input_fg),
);
}
// Render dropdown suggestion list (below input field, centered)
if !self.suggestions.is_empty() {
let dropdown_y = self.y + self.height; // Below the input dialog
let dropdown_width = 42u16.min(content_width); // Increased by 2 for border spacing
let dropdown_height = self.suggestions.len() as u16 + 2; // +2 for top and bottom borders
// Center the dropdown horizontally
let dropdown_x = self.x + (self.width.saturating_sub(dropdown_width)) / 2;
// Draw top border
buffer.set(
dropdown_x,
dropdown_y,
Cell::new(
charset.border_top_left,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
for col in 1..dropdown_width - 1 {
buffer.set(
dropdown_x + col,
dropdown_y,
Cell::new(
charset.border_horizontal,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
}
buffer.set(
dropdown_x + dropdown_width - 1,
dropdown_y,
Cell::new(
charset.border_top_right,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
// Render suggestions with side borders
for (idx, suggestion) in self.suggestions.iter().enumerate() {
let row_y = dropdown_y + 1 + idx as u16;
let is_selected = idx == self.selected_suggestion;
let (fg, bg) = if is_selected {
// Inverted colors for selected item
(
theme.slight_dropdown_selected_fg,
theme.slight_dropdown_selected_bg,
)
} else {
(theme.slight_dropdown_fg, theme.slight_dropdown_bg)
};
// Left border (always uses dropdown background, not selection background)
buffer.set(
dropdown_x,
row_y,
Cell::new(
charset.border_vertical,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
// Render suggestion text (with padding, accounting for borders)
let text_width = dropdown_width.saturating_sub(2); // -2 for left and right borders
let text = format!(
" {:width$} ",
suggestion.command,
width = text_width.saturating_sub(2) as usize
);
for (i, ch) in text.chars().enumerate() {
if i < text_width as usize {
buffer.set(dropdown_x + 1 + i as u16, row_y, Cell::new(ch, fg, bg));
}
}
// Right border (always uses dropdown background, not selection background)
buffer.set(
dropdown_x + dropdown_width - 1,
row_y,
Cell::new(
charset.border_vertical,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
}
// Draw bottom border
let bottom_y = dropdown_y + dropdown_height - 1;
buffer.set(
dropdown_x,
bottom_y,
Cell::new(
charset.border_bottom_left,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
for col in 1..dropdown_width - 1 {
buffer.set(
dropdown_x + col,
bottom_y,
Cell::new(
charset.border_horizontal,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
}
buffer.set(
dropdown_x + dropdown_width - 1,
bottom_y,
Cell::new(
charset.border_bottom_right,
theme.slight_border,
theme.slight_dropdown_bg,
),
);
}
}
pub fn insert_char(&mut self, c: char) {
self.input_text.insert(self.cursor_position, c);
self.cursor_position += 1;
self.update_suggestions();
}
pub fn delete_char(&mut self) {
if self.cursor_position > 0 {
self.input_text.remove(self.cursor_position - 1);
self.cursor_position -= 1;
self.update_suggestions();
}
}
pub fn move_cursor_left(&mut self) {
if self.cursor_position > 0 {
self.cursor_position -= 1;
}
}
pub fn move_cursor_right(&mut self) {
if self.cursor_position < self.input_text.len() {
self.cursor_position += 1;
}
}
pub fn move_cursor_home(&mut self) {
self.cursor_position = 0;
}
pub fn move_cursor_end(&mut self) {
self.cursor_position = self.input_text.len();
}
pub fn get_input(&self) -> String {
self.input_text.clone()
}
/// Moves to the next suggestion in the dropdown
pub fn next_suggestion(&mut self) {
if !self.suggestions.is_empty() {
self.selected_suggestion = (self.selected_suggestion + 1) % self.suggestions.len();
}
}
/// Moves to the previous suggestion in the dropdown
pub fn previous_suggestion(&mut self) {
if !self.suggestions.is_empty() {
if self.selected_suggestion == 0 {
self.selected_suggestion = self.suggestions.len() - 1;
} else {
self.selected_suggestion -= 1;
}
}
}
/// Accepts the current inline suggestion (Right Arrow key)
pub fn accept_inline_suggestion(&mut self) {
if let Some(suggestion) = self.get_inline_suggestion() {
self.input_text = suggestion.to_string();
self.cursor_position = self.input_text.len();
self.update_suggestions();
}
}
/// Accepts the selected suggestion from dropdown (Tab key)
pub fn accept_selected_suggestion(&mut self) {
if !self.suggestions.is_empty() {
self.input_text = self.suggestions[self.selected_suggestion].command.clone();
self.cursor_position = self.input_text.len();
self.update_suggestions();
}
}
/// Clears input and resets state
#[allow(dead_code)]
pub fn clear(&mut self) {
self.input_text.clear();
self.cursor_position = 0;
self.update_suggestions();
}
/// Gets the command history for recording
#[allow(dead_code)]
pub fn get_history_mut(&mut self) -> Option<&mut CommandHistory> {
self.command_history.as_mut()
}
}