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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
//! Custom input handler with Claude Code-style @ file picker
//!
//! Provides:
//! - Real-time inline file suggestions when typing @
//! - Arrow key navigation in dropdown
//! - **Enter to SELECT suggestion** (not submit)
//! - Enter to SUBMIT only when no suggestions are active
//! - Support for multiple @ file references
use crate::agent::commands::SLASH_COMMANDS;
use crate::agent::ui::colors::ansi;
use crossterm::{
cursor::{self, MoveUp},
event::{self, DisableBracketedPaste, EnableBracketedPaste, Event, KeyCode, KeyModifiers},
execute,
terminal::{self, Clear, ClearType},
};
use std::io::{self, Write};
use std::path::PathBuf;
/// Result of reading user input
pub enum InputResult {
/// User submitted text (Enter with no picker open)
Submit(String),
/// User cancelled (Ctrl+C or Escape with no picker)
Cancel,
/// User wants to exit
Exit,
/// User toggled planning mode (Shift+Tab)
TogglePlanMode,
}
/// Suggestion item
#[derive(Clone)]
struct Suggestion {
display: String,
value: String,
is_dir: bool,
}
/// Input state
struct InputState {
/// Current input text
text: String,
/// Cursor position in text (character index)
cursor: usize,
/// Current suggestions
suggestions: Vec<Suggestion>,
/// Selected suggestion index (-1 = none selected)
selected: i32,
/// Whether suggestions dropdown is visible
showing_suggestions: bool,
/// Start position of current completion (@ position)
completion_start: Option<usize>,
/// Project path for file searches
project_path: PathBuf,
/// Number of lines rendered for suggestions (for cleanup)
rendered_lines: usize,
/// Number of wrapped lines the input text occupied in last render
prev_wrapped_lines: usize,
/// The line index (0-based) where cursor was positioned after last render
prev_cursor_line: usize,
/// Whether in plan mode (shows ★ indicator)
plan_mode: bool,
}
impl InputState {
fn new(project_path: PathBuf, plan_mode: bool) -> Self {
Self {
text: String::new(),
cursor: 0,
suggestions: Vec::new(),
selected: -1,
showing_suggestions: false,
completion_start: None,
project_path,
rendered_lines: 0,
prev_wrapped_lines: 1,
prev_cursor_line: 0,
plan_mode,
}
}
/// Insert character at cursor
fn insert_char(&mut self, c: char) {
// Skip carriage returns, keep newlines for multi-line support
if c == '\r' {
return;
}
// Insert at cursor position
let byte_pos = self.char_to_byte_pos(self.cursor);
self.text.insert(byte_pos, c);
self.cursor += 1;
// Check if we should trigger completion
if c == '@' {
let valid_trigger = self.cursor == 1
|| self
.text
.chars()
.nth(self.cursor - 2)
.map(|c| c.is_whitespace())
.unwrap_or(false);
if valid_trigger {
self.completion_start = Some(self.cursor - 1);
self.refresh_suggestions();
}
} else if c == '/' && self.cursor == 1 {
// Slash command at start
self.completion_start = Some(0);
self.refresh_suggestions();
} else if c.is_whitespace() {
// Space closes completion
self.close_suggestions();
} else if self.completion_start.is_some() {
// Continue filtering
self.refresh_suggestions();
}
}
/// Delete character before cursor
fn backspace(&mut self) {
if self.cursor > 0 {
let byte_pos = self.char_to_byte_pos(self.cursor - 1);
let next_byte_pos = self.char_to_byte_pos(self.cursor);
self.text.replace_range(byte_pos..next_byte_pos, "");
self.cursor -= 1;
// Check if we deleted the @ trigger
if let Some(start) = self.completion_start {
if self.cursor <= start {
self.close_suggestions();
} else {
self.refresh_suggestions();
}
}
}
}
/// Delete word before cursor (Ctrl+Backspace / Cmd+Delete / Alt+Backspace)
fn delete_word_left(&mut self) {
if self.cursor == 0 {
return;
}
let chars: Vec<char> = self.text.chars().collect();
let mut new_cursor = self.cursor;
// Skip whitespace going backwards
while new_cursor > 0 && chars[new_cursor - 1].is_whitespace() {
new_cursor -= 1;
}
// Skip word characters going backwards
while new_cursor > 0 && !chars[new_cursor - 1].is_whitespace() {
new_cursor -= 1;
}
// Delete from new_cursor to current cursor
let start_byte = self.char_to_byte_pos(new_cursor);
let end_byte = self.char_to_byte_pos(self.cursor);
self.text.replace_range(start_byte..end_byte, "");
self.cursor = new_cursor;
// Update suggestions if active
if let Some(start) = self.completion_start {
if self.cursor <= start {
self.close_suggestions();
} else {
self.refresh_suggestions();
}
}
}
/// Clear entire input (Ctrl+U)
fn clear_all(&mut self) {
self.text.clear();
self.cursor = 0;
self.close_suggestions();
}
/// Delete from cursor to beginning of current line (Cmd+Backspace)
fn delete_to_line_start(&mut self) {
if self.cursor == 0 {
return;
}
let chars: Vec<char> = self.text.chars().collect();
// Find the previous newline or start of text
let mut line_start = self.cursor;
while line_start > 0 && chars[line_start - 1] != '\n' {
line_start -= 1;
}
// If cursor is at line start, delete the newline to join with previous line
if line_start == self.cursor && self.cursor > 0 {
line_start -= 1;
}
// Delete from line_start to cursor
let start_byte = self.char_to_byte_pos(line_start);
let end_byte = self.char_to_byte_pos(self.cursor);
self.text.replace_range(start_byte..end_byte, "");
self.cursor = line_start;
self.close_suggestions();
}
/// Convert character position to byte position
fn char_to_byte_pos(&self, char_pos: usize) -> usize {
self.text
.char_indices()
.nth(char_pos)
.map(|(i, _)| i)
.unwrap_or(self.text.len())
}
/// Get the current filter text (after @ or /)
fn get_filter(&self) -> Option<String> {
self.completion_start.map(|start| {
let filter_start = start + 1; // Skip the @ or /
if filter_start <= self.cursor {
self.text
.chars()
.skip(filter_start)
.take(self.cursor - filter_start)
.collect()
} else {
String::new()
}
})
}
/// Refresh suggestions based on current filter
fn refresh_suggestions(&mut self) {
let filter = self.get_filter().unwrap_or_default();
let trigger = self
.completion_start
.and_then(|pos| self.text.chars().nth(pos));
self.suggestions = match trigger {
Some('@') => self.search_files(&filter),
Some('/') => self.search_commands(&filter),
_ => Vec::new(),
};
self.showing_suggestions = !self.suggestions.is_empty();
self.selected = if self.showing_suggestions { 0 } else { -1 };
}
/// Search for files matching filter
fn search_files(&self, filter: &str) -> Vec<Suggestion> {
let mut results = Vec::new();
let filter_lower = filter.to_lowercase();
self.walk_dir(
&self.project_path.clone(),
&filter_lower,
&mut results,
0,
4,
);
// Sort: directories first, then by path length
results.sort_by(|a, b| match (a.is_dir, b.is_dir) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.value.len().cmp(&b.value.len()),
});
results.truncate(8);
results
}
/// Walk directory tree for matching files
fn walk_dir(
&self,
dir: &PathBuf,
filter: &str,
results: &mut Vec<Suggestion>,
depth: usize,
max_depth: usize,
) {
if depth > max_depth || results.len() >= 20 {
return;
}
let skip_dirs = [
"node_modules",
".git",
"target",
"__pycache__",
".venv",
"venv",
"dist",
"build",
".next",
];
let entries = match std::fs::read_dir(dir) {
Ok(e) => e,
Err(_) => return,
};
for entry in entries.flatten() {
let path = entry.path();
let file_name = entry.file_name().to_string_lossy().to_string();
// Skip hidden files (except some)
if file_name.starts_with('.')
&& !file_name.starts_with(".env")
&& file_name != ".gitignore"
{
continue;
}
let rel_path = path
.strip_prefix(&self.project_path)
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|_| file_name.clone());
let is_dir = path.is_dir();
if filter.is_empty()
|| rel_path.to_lowercase().contains(filter)
|| file_name.to_lowercase().contains(filter)
{
let display = if is_dir {
format!("{}/", rel_path)
} else {
rel_path.clone()
};
results.push(Suggestion {
display: display.clone(),
value: display,
is_dir,
});
}
if is_dir && !skip_dirs.contains(&file_name.as_str()) {
self.walk_dir(&path, filter, results, depth + 1, max_depth);
}
}
}
/// Search for slash commands matching filter
fn search_commands(&self, filter: &str) -> Vec<Suggestion> {
let filter_lower = filter.to_lowercase();
SLASH_COMMANDS
.iter()
.filter(|cmd| {
cmd.name.to_lowercase().starts_with(&filter_lower)
|| cmd
.alias
.map(|a| a.to_lowercase().starts_with(&filter_lower))
.unwrap_or(false)
})
.take(8)
.map(|cmd| Suggestion {
display: format!("/{:<12} {}", cmd.name, cmd.description),
value: format!("/{}", cmd.name),
is_dir: false,
})
.collect()
}
/// Close suggestions dropdown
fn close_suggestions(&mut self) {
self.showing_suggestions = false;
self.suggestions.clear();
self.selected = -1;
self.completion_start = None;
}
/// Move selection up
fn select_up(&mut self) {
if self.showing_suggestions && !self.suggestions.is_empty() && self.selected > 0 {
self.selected -= 1;
}
}
/// Move selection down
fn select_down(&mut self) {
if self.showing_suggestions
&& !self.suggestions.is_empty()
&& self.selected < self.suggestions.len() as i32 - 1
{
self.selected += 1;
}
}
/// Accept the current selection
fn accept_selection(&mut self) -> bool {
if self.showing_suggestions
&& self.selected >= 0
&& let Some(suggestion) = self.suggestions.get(self.selected as usize)
{
if let Some(start) = self.completion_start {
// Replace @filter with @value
let before = self.text.chars().take(start).collect::<String>();
let after = self.text.chars().skip(self.cursor).collect::<String>();
// For files, use @path format; for commands, use /command
let replacement = if suggestion.value.starts_with('/') {
format!("{} ", suggestion.value)
} else {
format!("@{} ", suggestion.value)
};
self.text = format!("{}{}{}", before, replacement, after);
self.cursor = before.len() + replacement.len();
}
self.close_suggestions();
return true;
}
false
}
/// Move cursor left
fn cursor_left(&mut self) {
if self.cursor > 0 {
self.cursor -= 1;
}
}
/// Move cursor right
fn cursor_right(&mut self) {
if self.cursor < self.text.chars().count() {
self.cursor += 1;
}
}
/// Move cursor to start of previous word (Option+Left on Mac, Ctrl+Left elsewhere)
fn cursor_word_left(&mut self) {
if self.cursor == 0 {
return;
}
let chars: Vec<char> = self.text.chars().collect();
let mut pos = self.cursor;
// Skip whitespace going backwards
while pos > 0 && chars[pos - 1].is_whitespace() {
pos -= 1;
}
// Skip word characters going backwards
while pos > 0 && !chars[pos - 1].is_whitespace() {
pos -= 1;
}
self.cursor = pos;
}
/// Move cursor to start of next word (Option+Right on Mac, Ctrl+Right elsewhere)
fn cursor_word_right(&mut self) {
let chars: Vec<char> = self.text.chars().collect();
let text_len = chars.len();
if self.cursor >= text_len {
return;
}
let mut pos = self.cursor;
// Skip current word characters
while pos < text_len && !chars[pos].is_whitespace() {
pos += 1;
}
// Skip whitespace
while pos < text_len && chars[pos].is_whitespace() {
pos += 1;
}
self.cursor = pos;
}
/// Move cursor to start
fn cursor_home(&mut self) {
self.cursor = 0;
}
/// Move cursor to end
fn cursor_end(&mut self) {
self.cursor = self.text.chars().count();
}
/// Move cursor up one line
fn cursor_up(&mut self) {
let chars: Vec<char> = self.text.chars().collect();
if self.cursor == 0 {
return;
}
// Find the start of the current line
let mut current_line_start = self.cursor;
while current_line_start > 0 && chars[current_line_start - 1] != '\n' {
current_line_start -= 1;
}
// If we're on the first line, can't go up
if current_line_start == 0 {
return;
}
// Find the column position on current line
let col = self.cursor - current_line_start;
// Find the start of the previous line
let prev_line_end = current_line_start - 1; // Position of the \n
let mut prev_line_start = prev_line_end;
while prev_line_start > 0 && chars[prev_line_start - 1] != '\n' {
prev_line_start -= 1;
}
// Calculate the length of the previous line
let prev_line_len = prev_line_end - prev_line_start;
// Move cursor to same column on previous line (or end if line is shorter)
self.cursor = prev_line_start + col.min(prev_line_len);
}
/// Move cursor down one line
fn cursor_down(&mut self) {
let chars: Vec<char> = self.text.chars().collect();
let text_len = chars.len();
// Find the start of the current line
let mut current_line_start = self.cursor;
while current_line_start > 0 && chars[current_line_start - 1] != '\n' {
current_line_start -= 1;
}
// Find the column position on current line
let col = self.cursor - current_line_start;
// Find the end of the current line (the \n or end of text)
let mut current_line_end = self.cursor;
while current_line_end < text_len && chars[current_line_end] != '\n' {
current_line_end += 1;
}
// If we're on the last line, can't go down
if current_line_end >= text_len {
return;
}
// Find the start of the next line
let next_line_start = current_line_end + 1;
// Find the end of the next line
let mut next_line_end = next_line_start;
while next_line_end < text_len && chars[next_line_end] != '\n' {
next_line_end += 1;
}
// Calculate the length of the next line
let next_line_len = next_line_end - next_line_start;
// Move cursor to same column on next line (or end if line is shorter)
self.cursor = next_line_start + col.min(next_line_len);
}
}
/// Render the input UI with multi-line support
fn render(state: &mut InputState, prompt: &str, stdout: &mut io::Stdout) -> io::Result<usize> {
// Get terminal width
let (term_width, _) = terminal::size().unwrap_or((80, 24));
let term_width = term_width as usize;
// Calculate prompt length (include ★ prefix if in plan mode)
let mode_prefix_len = if state.plan_mode { 2 } else { 0 }; // "★ " = 2 chars
let prompt_len = prompt.len() + 1 + mode_prefix_len; // +1 for space after prompt
// Move up from the cursor's current line position to the start of input
// We use prev_cursor_line (where we left the cursor last render) not prev_wrapped_lines
if state.prev_cursor_line > 0 {
execute!(stdout, cursor::MoveUp(state.prev_cursor_line as u16))?;
}
execute!(stdout, cursor::MoveToColumn(0))?;
// Clear from cursor to end of screen
execute!(stdout, Clear(ClearType::FromCursorDown))?;
// Print prompt and input text with mode indicator if in plan mode
// In raw mode, \n doesn't return to column 0, so we need \r\n
let display_text = state.text.replace('\n', "\r\n");
if state.plan_mode {
print!(
"{}★{} {}{}{} {}",
ansi::ORANGE,
ansi::RESET,
ansi::SUCCESS,
prompt,
ansi::RESET,
display_text
);
} else {
print!(
"{}{}{} {}",
ansi::SUCCESS,
prompt,
ansi::RESET,
display_text
);
}
stdout.flush()?;
// Calculate how many lines the text spans (counting newlines + wrapping)
let mut total_lines = 1;
let mut current_line_len = prompt_len;
for c in state.text.chars() {
if c == '\n' {
total_lines += 1;
current_line_len = 0;
} else {
current_line_len += 1;
if term_width > 0 && current_line_len > term_width {
total_lines += 1;
current_line_len = 1;
}
}
}
state.prev_wrapped_lines = total_lines;
// Render suggestions below if active
let mut lines_rendered = 0;
if state.showing_suggestions && !state.suggestions.is_empty() {
// Move to next line for suggestions (use \r\n in raw mode)
print!("\r\n");
lines_rendered += 1;
for (i, suggestion) in state.suggestions.iter().enumerate() {
let is_selected = i as i32 == state.selected;
let prefix = if is_selected { "â–¸" } else { " " };
if is_selected {
if suggestion.is_dir {
// Use standard cyan which adapts to terminal theme
print!(
" {}{}{} {}{}\r\n",
ansi::BOLD,
ansi::STD_CYAN,
prefix,
suggestion.display,
ansi::RESET
);
} else {
// Use bold for selected items - works on light AND dark terminals
print!(
" {}{} {}{}\r\n",
ansi::BRIGHT,
prefix,
suggestion.display,
ansi::RESET
);
}
} else {
// Use subdued for non-selected - readable on any terminal
print!(
" {}{} {}{}\r\n",
ansi::SUBDUED,
prefix,
suggestion.display,
ansi::RESET
);
}
lines_rendered += 1;
}
// Print hint - use subdued for secondary text
print!(
" {}[↑↓ navigate, Enter select, Esc cancel]{}\r\n",
ansi::SUBDUED,
ansi::RESET
);
lines_rendered += 1;
}
// Position cursor correctly within input (handling newlines)
// Calculate which line and column the cursor is on
let mut cursor_line = 0;
let mut cursor_col = prompt_len;
for (i, c) in state.text.chars().enumerate() {
if i >= state.cursor {
break;
}
if c == '\n' {
cursor_line += 1;
cursor_col = 0;
} else {
cursor_col += 1;
if term_width > 0 && cursor_col >= term_width {
cursor_line += 1;
cursor_col = 0;
}
}
}
// Move cursor from end of text to correct position
let lines_after_cursor = total_lines.saturating_sub(cursor_line + 1) + lines_rendered;
if lines_after_cursor > 0 {
execute!(stdout, cursor::MoveUp(lines_after_cursor as u16))?;
}
execute!(stdout, cursor::MoveToColumn(cursor_col as u16))?;
// Save the cursor line for next render's initial positioning
state.prev_cursor_line = cursor_line;
stdout.flush()?;
Ok(lines_rendered)
}
/// Clear rendered suggestion lines
fn clear_suggestions(num_lines: usize, stdout: &mut io::Stdout) -> io::Result<()> {
if num_lines > 0 {
// Save position, clear lines below, restore
for _ in 0..num_lines {
execute!(stdout, cursor::MoveDown(1), Clear(ClearType::CurrentLine))?;
}
execute!(stdout, MoveUp(num_lines as u16))?;
}
Ok(())
}
/// Read user input with Claude Code-style @ file picker
/// If `plan_mode` is true, shows the plan mode indicator below the prompt
pub fn read_input_with_file_picker(
prompt: &str,
project_path: &std::path::Path,
plan_mode: bool,
) -> InputResult {
let mut stdout = io::stdout();
// Always ensure cursor is visible at start of input (may have been hidden by progress indicator)
print!("{}", ansi::SHOW_CURSOR);
let _ = stdout.flush();
// Enable raw mode
if terminal::enable_raw_mode().is_err() {
return read_simple_input(prompt);
}
// Enable bracketed paste mode to detect paste vs keypress
let _ = execute!(stdout, EnableBracketedPaste);
// Print prompt with mode indicator inline (no separate line)
if plan_mode {
print!(
"{}★{} {}{}{} ",
ansi::ORANGE,
ansi::RESET,
ansi::SUCCESS,
prompt,
ansi::RESET
);
} else {
print!("{}{}{} ", ansi::SUCCESS, prompt, ansi::RESET);
}
let _ = stdout.flush();
// Create state after printing prompt so start_row is correct
let mut state = InputState::new(project_path.to_path_buf(), plan_mode);
let result = loop {
match event::read() {
// Handle paste events - insert all pasted text at once
Ok(Event::Paste(pasted_text)) => {
// Normalize line endings: \r\n -> \n, lone \r -> \n
let normalized = pasted_text.replace("\r\n", "\n").replace('\r', "\n");
for c in normalized.chars() {
state.insert_char(c);
}
// Render after paste completes
state.rendered_lines = render(&mut state, prompt, &mut stdout).unwrap_or(0);
}
Ok(Event::Key(key_event)) => {
match key_event.code {
KeyCode::Enter => {
// Shift+Enter or Alt+Enter inserts newline instead of submitting
if key_event.modifiers.contains(KeyModifiers::SHIFT)
|| key_event.modifiers.contains(KeyModifiers::ALT)
{
state.insert_char('\n');
} else if state.showing_suggestions && state.selected >= 0 {
// Accept selection, don't submit
state.accept_selection();
} else if !state.text.trim().is_empty() {
// Submit
print!("\r\n");
let _ = stdout.flush();
break InputResult::Submit(state.text.clone());
}
}
KeyCode::Tab => {
// Tab also accepts selection
if state.showing_suggestions && state.selected >= 0 {
state.accept_selection();
}
}
KeyCode::BackTab => {
// Shift+Tab toggles planning mode
print!("\r\n");
let _ = stdout.flush();
break InputResult::TogglePlanMode;
}
KeyCode::Esc => {
if state.showing_suggestions {
state.close_suggestions();
} else {
print!("\r\n");
let _ = stdout.flush();
break InputResult::Cancel;
}
}
KeyCode::Char('c') if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
// Ctrl+C always exits (consistent with standard CLI behavior)
print!("\r\n");
let _ = stdout.flush();
break InputResult::Cancel;
}
KeyCode::Char('d') if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
print!("\r\n");
let _ = stdout.flush();
break InputResult::Exit;
}
KeyCode::Up => {
if state.showing_suggestions {
state.select_up();
} else {
state.cursor_up();
}
}
KeyCode::Down => {
if state.showing_suggestions {
state.select_down();
} else {
state.cursor_down();
}
}
KeyCode::Left => {
state.cursor_left();
// Close suggestions if cursor moves before @
if let Some(start) = state.completion_start
&& state.cursor <= start
{
state.close_suggestions();
}
}
KeyCode::Right => {
state.cursor_right();
}
// Alt+b (Option+Left on Mac) - Move cursor to previous word
KeyCode::Char('b') if key_event.modifiers.contains(KeyModifiers::ALT) => {
state.cursor_word_left();
state.close_suggestions();
}
// Alt+f (Option+Right on Mac) - Move cursor to next word
KeyCode::Char('f') if key_event.modifiers.contains(KeyModifiers::ALT) => {
state.cursor_word_right();
}
KeyCode::Home | KeyCode::Char('a')
if key_event.modifiers.contains(KeyModifiers::CONTROL) =>
{
state.cursor_home();
state.close_suggestions();
}
KeyCode::End | KeyCode::Char('e')
if key_event.modifiers.contains(KeyModifiers::CONTROL) =>
{
state.cursor_end();
}
// Ctrl+U - Clear entire input
KeyCode::Char('u') if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
state.clear_all();
}
// Ctrl+K - Delete to beginning of current line (works on all platforms)
KeyCode::Char('k') if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
state.delete_to_line_start();
}
// Ctrl+Shift+Backspace - Delete to beginning of current line (cross-platform)
KeyCode::Backspace
if key_event.modifiers.contains(KeyModifiers::CONTROL)
&& key_event.modifiers.contains(KeyModifiers::SHIFT) =>
{
state.delete_to_line_start();
}
// Cmd+Backspace (Mac) - Delete to beginning of current line (if terminal passes it)
KeyCode::Backspace if key_event.modifiers.contains(KeyModifiers::SUPER) => {
state.delete_to_line_start();
}
// Ctrl+W or Alt+Backspace - Delete word left
KeyCode::Char('w') if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
state.delete_word_left();
}
KeyCode::Backspace if key_event.modifiers.contains(KeyModifiers::ALT) => {
state.delete_word_left();
}
KeyCode::Backspace if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
state.delete_word_left();
}
// Ctrl+J - Insert newline (multi-line input)
KeyCode::Char('j') if key_event.modifiers.contains(KeyModifiers::CONTROL) => {
state.insert_char('\n');
}
KeyCode::Backspace => {
state.backspace();
}
KeyCode::Char('\n') => {
// Handle newline char that might come through during paste
state.insert_char('\n');
}
KeyCode::Char(c) => {
state.insert_char(c);
}
_ => {}
}
// Only render if no more events are pending (batches rapid input like paste)
// This prevents thousands of renders during paste operations
let should_render =
!event::poll(std::time::Duration::from_millis(0)).unwrap_or(false);
if should_render {
state.rendered_lines = render(&mut state, prompt, &mut stdout).unwrap_or(0);
}
}
Ok(Event::Resize(_, _)) => {
// Redraw on resize
state.rendered_lines = render(&mut state, prompt, &mut stdout).unwrap_or(0);
}
Err(_) => {
break InputResult::Cancel;
}
_ => {}
}
};
// Disable bracketed paste mode
let _ = execute!(stdout, DisableBracketedPaste);
// Disable raw mode
let _ = terminal::disable_raw_mode();
// Clean up any remaining rendered lines
if state.rendered_lines > 0 {
let _ = clear_suggestions(state.rendered_lines, &mut stdout);
}
result
}
/// Simple fallback input without raw mode
fn read_simple_input(prompt: &str) -> InputResult {
print!("{}{}{} ", ansi::SUCCESS, prompt, ansi::RESET);
let _ = io::stdout().flush();
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => {
let trimmed = input.trim();
if trimmed.eq_ignore_ascii_case("exit") || trimmed == "/exit" || trimmed == "/quit" {
InputResult::Exit
} else {
InputResult::Submit(trimmed.to_string())
}
}
Err(_) => InputResult::Cancel,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn new_state() -> InputState {
InputState::new(PathBuf::from("/tmp"), false)
}
#[test]
fn test_insert_char_basic() {
let mut state = new_state();
state.insert_char('h');
state.insert_char('i');
assert_eq!(state.text, "hi");
assert_eq!(state.cursor, 2);
}
#[test]
fn test_insert_char_utf8() {
let mut state = new_state();
state.insert_char('æ—¥');
state.insert_char('本');
assert_eq!(state.text, "日本");
assert_eq!(state.cursor, 2);
}
#[test]
fn test_insert_char_skips_cr() {
let mut state = new_state();
state.insert_char('a');
state.insert_char('\r');
state.insert_char('b');
assert_eq!(state.text, "ab");
}
#[test]
fn test_backspace_basic() {
let mut state = new_state();
state.insert_char('h');
state.insert_char('e');
state.insert_char('l');
state.backspace();
assert_eq!(state.text, "he");
assert_eq!(state.cursor, 2);
}
#[test]
fn test_backspace_utf8() {
let mut state = new_state();
state.insert_char('æ—¥');
state.insert_char('本');
state.backspace();
assert_eq!(state.text, "æ—¥");
assert_eq!(state.cursor, 1);
}
#[test]
fn test_backspace_at_start() {
let mut state = new_state();
state.backspace(); // Should not panic
assert_eq!(state.text, "");
assert_eq!(state.cursor, 0);
}
#[test]
fn test_cursor_movement() {
let mut state = new_state();
state.insert_char('h');
state.insert_char('e');
state.insert_char('l');
state.insert_char('l');
state.insert_char('o');
assert_eq!(state.cursor, 5);
state.cursor_left();
assert_eq!(state.cursor, 4);
state.cursor_home();
assert_eq!(state.cursor, 0);
state.cursor_right();
assert_eq!(state.cursor, 1);
state.cursor_end();
assert_eq!(state.cursor, 5);
}
#[test]
fn test_cursor_bounds() {
let mut state = new_state();
state.insert_char('a');
state.cursor_left();
state.cursor_left(); // Should not go below 0
assert_eq!(state.cursor, 0);
state.cursor_right();
state.cursor_right(); // Should not go beyond text length
assert_eq!(state.cursor, 1);
}
#[test]
fn test_char_to_byte_pos_ascii() {
let mut state = new_state();
state.text = "hello".to_string();
assert_eq!(state.char_to_byte_pos(0), 0);
assert_eq!(state.char_to_byte_pos(2), 2);
assert_eq!(state.char_to_byte_pos(5), 5);
}
#[test]
fn test_char_to_byte_pos_utf8() {
let mut state = new_state();
state.text = "日本語".to_string(); // Each char is 3 bytes
assert_eq!(state.char_to_byte_pos(0), 0);
assert_eq!(state.char_to_byte_pos(1), 3);
assert_eq!(state.char_to_byte_pos(2), 6);
assert_eq!(state.char_to_byte_pos(3), 9);
}
#[test]
fn test_clear_all() {
let mut state = new_state();
state.insert_char('h');
state.insert_char('e');
state.insert_char('l');
state.clear_all();
assert_eq!(state.text, "");
assert_eq!(state.cursor, 0);
}
#[test]
fn test_delete_word_left() {
let mut state = new_state();
for c in "hello world".chars() {
state.insert_char(c);
}
state.delete_word_left();
assert_eq!(state.text, "hello ");
assert_eq!(state.cursor, 6);
}
#[test]
fn test_multiline_cursor_navigation() {
let mut state = new_state();
// "ab\ncd"
for c in "ab".chars() {
state.insert_char(c);
}
state.insert_char('\n');
for c in "cd".chars() {
state.insert_char(c);
}
assert_eq!(state.cursor, 5); // at end
state.cursor_up();
assert_eq!(state.cursor, 2); // end of first line "ab"
state.cursor_down();
assert_eq!(state.cursor, 5); // back to end
}
#[test]
fn test_get_filter_at_symbol() {
let mut state = new_state();
state.text = "@src".to_string();
state.cursor = 4;
state.completion_start = Some(0);
assert_eq!(state.get_filter(), Some("src".to_string()));
}
#[test]
fn test_get_filter_no_completion() {
let mut state = new_state();
state.text = "hello".to_string();
state.cursor = 5;
assert_eq!(state.get_filter(), None);
}
#[test]
fn test_cursor_word_left() {
let mut state = new_state();
state.text = "hello world test".to_string();
state.cursor = 16; // at end
state.cursor_word_left();
assert_eq!(state.cursor, 12); // start of "test"
state.cursor_word_left();
assert_eq!(state.cursor, 6); // start of "world"
state.cursor_word_left();
assert_eq!(state.cursor, 0); // start of "hello"
state.cursor_word_left();
assert_eq!(state.cursor, 0); // still at start
}
#[test]
fn test_cursor_word_right() {
let mut state = new_state();
state.text = "hello world test".to_string();
state.cursor = 0; // at start
state.cursor_word_right();
assert_eq!(state.cursor, 6); // start of "world"
state.cursor_word_right();
assert_eq!(state.cursor, 12); // start of "test"
state.cursor_word_right();
assert_eq!(state.cursor, 16); // end of text
state.cursor_word_right();
assert_eq!(state.cursor, 16); // still at end
}
#[test]
fn test_cursor_word_movement_mid_word() {
let mut state = new_state();
state.text = "hello world".to_string();
state.cursor = 8; // middle of "world"
state.cursor_word_left();
assert_eq!(state.cursor, 6); // start of "world"
state.cursor = 3; // middle of "hello"
state.cursor_word_right();
assert_eq!(state.cursor, 6); // start of "world"
}
}