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
use std::time::Instant;
use crate::input::Action;
use crate::types::{
ChatMessage, ClickTarget, InputMode, MessageRole, Overlay, Panel, Selection, ViewState,
};
use crate::views::file_browser;
use crate::views::fix::FixViewState;
use super::{App, AppCommand};
impl App {
pub fn apply_action(&mut self, action: Action) -> Option<AppCommand> {
// Reset idle suggestion timer on any non-None action
if !matches!(action, Action::None) {
self.idle_suggestions.reset_timer();
}
// Dismiss idle suggestion on any action
if self.idle_suggestions.current.is_some() && !matches!(action, Action::None) {
self.idle_suggestions.dismiss();
}
// Handle overlay-specific input first
if self.overlay != Overlay::None {
return self.handle_overlay_action(action);
}
match action {
Action::Quit => {
self.running = false;
None
}
Action::NextPanel => {
self.next_panel();
None
}
Action::ToggleTerminal => {
self.terminal_visible = !self.terminal_visible;
None
}
Action::ToggleSidebar => {
self.sidebar_visible = !self.sidebar_visible;
None
}
Action::ToggleFilesPanel => {
self.files_panel_visible = !self.files_panel_visible;
None
}
Action::CloseFile => {
self.code_content = None;
self.open_file_path = None;
self.code_scroll = 0;
self.selection = None;
self.active_panel = Panel::FileBrowser;
None
}
Action::InsertChar(c) => {
self.input.insert(self.input_cursor, c);
self.input_cursor += c.len_utf8();
None
}
Action::DeleteChar => {
if self.input_cursor > 0 {
let mut boundary = self.input_cursor - 1;
while !self.input.is_char_boundary(boundary) {
boundary -= 1;
}
self.input.remove(boundary);
self.input_cursor = boundary;
}
None
}
Action::MoveCursorLeft => {
if self.input_cursor > 0 {
let mut boundary = self.input_cursor - 1;
while !self.input.is_char_boundary(boundary) {
boundary -= 1;
}
self.input_cursor = boundary;
}
None
}
Action::MoveCursorRight => {
if self.input_cursor < self.input.len() {
let mut boundary = self.input_cursor + 1;
while boundary < self.input.len()
&& !self.input.is_char_boundary(boundary)
{
boundary += 1;
}
self.input_cursor = boundary;
}
None
}
Action::HistoryUp => {
self.history_up();
None
}
Action::HistoryDown => {
self.history_down();
None
}
Action::TabComplete => {
self.try_tab_complete();
None
}
Action::ScrollUp => {
match self.view_state {
ViewState::Scan => {
let count = self.filtered_findings_count();
self.scan_view.navigate_up();
let _ = count; // used for bounds checking inside navigate_up
}
ViewState::Fix => {
if self.fix_view.is_single_fix() {
self.cycle_single_fix(-1);
} else {
self.fix_view.navigate_up();
}
}
ViewState::Timeline => {
self.timeline_view.scroll_offset =
self.timeline_view.scroll_offset.saturating_sub(1);
}
ViewState::Report => {
self.report_view.scroll_offset =
self.report_view.scroll_offset.saturating_sub(1);
}
ViewState::Passport => {
use crate::views::passport::{PassportDetailMode, PassportViewMode};
if self.passport_view.view_mode == PassportViewMode::AgentList {
if self.passport_view.selected_passport > 0 {
self.passport_view.selected_passport -= 1;
}
} else if self.passport_view.detail_mode == PassportDetailMode::ObligationChecklist {
self.passport_view.obligation_scroll =
self.passport_view.obligation_scroll.saturating_sub(1);
} else if self.passport_view.selected_index > 0 {
self.passport_view.selected_index -= 1;
}
}
ViewState::Obligations => {
let filtered_len = self.obligations_view.filtered_obligations().len();
if filtered_len > 0 && self.obligations_view.selected_index > 0 {
self.obligations_view.selected_index -= 1;
if self.obligations_view.selected_index < self.obligations_view.scroll_offset {
self.obligations_view.scroll_offset = self.obligations_view.selected_index;
}
}
}
_ => match self.active_panel {
Panel::CodeViewer => {
self.code_scroll = self.code_scroll.saturating_sub(1);
}
Panel::FileBrowser => {
self.file_browser_index =
self.file_browser_index.saturating_sub(1);
}
Panel::Terminal => {
self.terminal_scroll =
self.terminal_scroll.saturating_sub(1);
self.terminal_auto_scroll = false;
}
Panel::Chat => {
self.chat_scroll = self.chat_scroll.saturating_sub(1);
self.chat_auto_scroll = false;
}
_ => {}
},
}
None
}
Action::ScrollDown => {
match self.view_state {
ViewState::Scan => {
let count = self.filtered_findings_count();
self.scan_view.navigate_down(count);
}
ViewState::Fix => {
if self.fix_view.is_single_fix() {
self.cycle_single_fix(1);
} else {
self.fix_view.navigate_down();
}
}
ViewState::Timeline => {
self.timeline_view.scroll_offset += 1;
}
ViewState::Report => {
self.report_view.scroll_offset += 1;
}
ViewState::Passport => {
use crate::views::passport::{PassportDetailMode, PassportViewMode};
if self.passport_view.view_mode == PassportViewMode::AgentList {
let max = self.passport_view.loaded_passports.len().saturating_sub(1);
if self.passport_view.selected_passport < max {
self.passport_view.selected_passport += 1;
}
} else if self.passport_view.detail_mode == PassportDetailMode::ObligationChecklist {
self.passport_view.obligation_scroll += 1;
} else {
let max = self.passport_view.fields.len().saturating_sub(1);
if self.passport_view.selected_index < max {
self.passport_view.selected_index += 1;
}
}
}
ViewState::Obligations => {
let filtered_len = self.obligations_view.filtered_obligations().len();
if filtered_len > 0
&& self.obligations_view.selected_index < filtered_len.saturating_sub(1)
{
self.obligations_view.selected_index += 1;
// Keep selected item visible (assume ~30 visible lines)
let visible_lines = 30usize;
if self.obligations_view.selected_index >= self.obligations_view.scroll_offset + visible_lines {
self.obligations_view.scroll_offset = self.obligations_view.selected_index.saturating_sub(visible_lines - 1);
}
}
}
_ => match self.active_panel {
Panel::CodeViewer => {
self.code_scroll += 1;
}
Panel::FileBrowser => {
if self.file_browser_index + 1 < self.file_tree.len() {
self.file_browser_index += 1;
}
}
Panel::Terminal => {
self.terminal_scroll += 1;
if self.terminal_scroll + 1 >= self.terminal_output.len() {
self.terminal_auto_scroll = true;
}
}
Panel::Chat => {
self.chat_scroll += 1;
}
_ => {}
},
}
None
}
Action::ScrollHalfPageUp => {
match self.active_panel {
Panel::Chat => {
self.chat_scroll = self.chat_scroll.saturating_sub(10);
self.chat_auto_scroll = false;
}
_ => {
self.code_scroll = self.code_scroll.saturating_sub(10);
}
}
None
}
Action::ScrollHalfPageDown => {
match self.active_panel {
Panel::Chat => self.chat_scroll += 10,
_ => self.code_scroll += 10,
}
None
}
Action::ScrollToTop => {
self.code_scroll = 0;
None
}
Action::ScrollToBottom => {
self.code_scroll = usize::MAX;
self.chat_auto_scroll = true;
None
}
Action::EnterInsertMode => {
self.input_mode = InputMode::Insert;
if self.view_state == ViewState::Chat {
self.chat_auto_scroll = true;
}
None
}
Action::EnterNormalMode => {
self.input_mode = InputMode::Normal;
self.selection = None;
self.colon_mode = false;
// Esc during streaming on Chat view → cancel LLM response
if self.view_state == ViewState::Chat && self.streaming.active {
return Some(AppCommand::ChatCancel);
}
None
}
Action::EnterVisualMode => {
self.input_mode = InputMode::Visual;
let line = self.code_scroll;
self.selection = Some(Selection {
start_line: line,
end_line: line,
});
None
}
Action::EnterCommandMode => {
self.input_mode = InputMode::Command;
self.input.clear();
self.input_cursor = 0;
None
}
Action::SelectionUp => {
if let Some(sel) = &mut self.selection {
sel.end_line = sel.end_line.saturating_sub(1);
if sel.end_line < sel.start_line {
sel.start_line = sel.end_line;
}
}
None
}
Action::SelectionDown => {
if let Some(sel) = &mut self.selection {
sel.end_line += 1;
}
None
}
Action::SubmitInput => {
let text = std::mem::take(&mut self.input);
self.input_cursor = 0;
if text.trim().is_empty() {
return None;
}
self.push_to_history(&text);
// Handle `!` bash prefix
if let Some(cmd) = text.strip_prefix('!')
&& !cmd.is_empty() {
self.terminal_visible = true;
self.messages.push(ChatMessage::new(
MessageRole::System,
format!("$ {cmd}"),
));
return Some(AppCommand::RunCommand(cmd.to_string()));
}
// Colon-command mode: route to handle_colon_command
if self.colon_mode {
self.colon_mode = false;
self.input_mode = InputMode::Normal;
return self.handle_colon_command(&text);
}
if self.input_mode == InputMode::Command || text.starts_with('/') {
// On Log view, route LLM-specific slash commands to engine chat
if self.view_state == ViewState::Chat && !self.streaming.active {
let cmd_word = text.trim_start_matches('/').split_whitespace().next().unwrap_or("");
if matches!(cmd_word, "cost" | "mode" | "model") {
self.chat_auto_scroll = true;
return Some(AppCommand::ChatSend(text));
}
}
// Code search: if in CodeViewer and text doesn't start with /
if self.active_panel == Panel::CodeViewer && !text.starts_with('/') {
// Treat as code search query
if let Some(content) = &self.code_content {
let matches = crate::views::code_viewer::find_search_matches(content, &text);
self.code_search_current = 0;
if !matches.is_empty() {
self.code_scroll = matches[0];
}
self.code_search_matches = matches;
self.code_search_query = Some(text);
}
self.input_mode = InputMode::Normal;
return None;
}
let cmd = text.trim_start_matches('/');
self.input_mode = InputMode::Insert;
return self.handle_command(cmd);
}
// When on Chat view, send plain text to LLM
if self.view_state == ViewState::Chat && !self.streaming.active {
self.chat_auto_scroll = true;
return Some(AppCommand::ChatSend(text));
}
self.messages.push(ChatMessage::new(
MessageRole::System,
"Unknown input. Use /help or :scan".to_string(),
));
self.chat_auto_scroll = true;
None
}
Action::SendSelectionToAi => {
if let (Some(content), Some(sel)) = (&self.code_content, &self.selection) {
let lines: Vec<&str> = content.lines().collect();
let start = sel.start_line.min(lines.len().saturating_sub(1));
let end = sel.end_line.min(lines.len().saturating_sub(1));
let selected: String = lines[start..=end].join("\n");
let file = self.open_file_path.as_deref().unwrap_or("unknown");
let context = format!(
"[selected {count} lines from {file}:{start_l}-{end_l}]\n```\n{code}\n```",
count = end - start + 1,
start_l = start + 1,
end_l = end + 1,
code = selected
);
self.input_mode = InputMode::Insert;
self.active_panel = Panel::Chat;
self.input = context;
}
None
}
Action::AcceptDiff => {
self.active_panel = Panel::Chat;
self.messages.push(ChatMessage::new(
MessageRole::System,
"Diff applied.".to_string(),
));
None
}
Action::RejectDiff => {
self.active_panel = Panel::Chat;
self.messages.push(ChatMessage::new(
MessageRole::System,
"Diff rejected.".to_string(),
));
None
}
Action::ToggleExpand => {
let idx = self.file_browser_index;
file_browser::toggle_expand(&mut self.file_tree, idx);
None
}
Action::OpenFile => {
if let Some(entry) = self.file_tree.get(self.file_browser_index) {
if entry.is_dir {
file_browser::toggle_expand(
&mut self.file_tree,
self.file_browser_index,
);
None
} else {
let path = entry.path.to_string_lossy().to_string();
Some(AppCommand::OpenFile(path))
}
} else {
None
}
}
Action::ShowCommandPalette => {
self.overlay = Overlay::CommandPalette;
self.overlay_filter.clear();
self.palette_index = 0;
None
}
Action::ShowFilePicker => {
self.overlay = Overlay::FilePicker;
self.overlay_filter.clear();
None
}
Action::ShowHelp => {
self.overlay = Overlay::Help;
self.help_scroll = 0;
None
}
Action::SwitchView(view) => {
self.view_state = view;
// Populate Fix view from latest scan when switching to it
if view == ViewState::Fix
&& let Some(scan) = &self.last_scan {
self.fix_view = FixViewState::from_scan(&scan.findings);
}
// Auto-load obligations when switching to Obligations view
if view == ViewState::Obligations && self.obligations_view.obligations.is_empty() {
return Some(AppCommand::LoadObligations);
}
// Auto-load passports when switching to Passport or Dashboard view
if matches!(view, ViewState::Passport | ViewState::Dashboard)
&& self.passport_view.loaded_passports.is_empty()
&& !self.passport_view.passport_loading
{
return Some(AppCommand::LoadPassports);
}
// Auto-load framework scores when switching to Dashboard
if view == ViewState::Dashboard && self.framework_scores.is_none() {
return Some(AppCommand::LoadFrameworkScores);
}
// Auto-load dashboard metrics when switching to Dashboard
// Reload if any metric is missing (readiness may fail on first load before passports)
if view == ViewState::Dashboard
&& (self.cost_estimate.is_none() || self.readiness_score.is_none())
{
return Some(AppCommand::LoadDashboardMetrics);
}
None
}
Action::ToggleMode => {
self.mode = self.mode.next();
None
}
Action::FocusPanel(panel) => {
self.active_panel = panel;
None
}
Action::WatchToggle => {
Some(AppCommand::ToggleWatch)
}
Action::ShowThemePicker => {
self.theme_picker = Some(crate::theme_picker::ThemePickerState::new());
self.overlay = Overlay::ThemePicker;
None
}
Action::CodeSearch => {
// Enter command mode to type search query
self.input_mode = InputMode::Command;
self.input.clear();
self.input_cursor = 0;
None
}
Action::CodeSearchNext => {
if !self.code_search_matches.is_empty() {
self.code_search_current =
(self.code_search_current + 1) % self.code_search_matches.len();
self.code_scroll = self.code_search_matches[self.code_search_current];
}
None
}
Action::CodeSearchPrev => {
if !self.code_search_matches.is_empty() {
self.code_search_current = if self.code_search_current == 0 {
self.code_search_matches.len() - 1
} else {
self.code_search_current - 1
};
self.code_scroll = self.code_search_matches[self.code_search_current];
}
None
}
Action::StartScan => {
self.messages.push(ChatMessage::new(
MessageRole::System,
"Scanning project...".to_string(),
));
self.operation_start = Some(Instant::now());
self.scan_view.scanning = true;
self.scan_view.scan_error = None;
Some(AppCommand::Scan)
}
Action::ViewKey(c) => {
self.handle_view_key(c)
}
Action::ViewEnter => {
self.handle_view_enter()
}
Action::ViewEscape => {
// Cancel streaming on Esc when on Chat view
if self.view_state == ViewState::Chat && self.streaming.active {
return Some(AppCommand::ChatCancel);
}
self.handle_view_escape();
None
}
Action::GotoLine => {
// Parse `:N` from command input
let text = std::mem::take(&mut self.input);
self.input_cursor = 0;
self.input_mode = InputMode::Normal;
if let Ok(line) = text.parse::<usize>() {
self.code_scroll = line.saturating_sub(1);
}
None
}
Action::Undo => {
Some(AppCommand::Undo(None))
}
Action::ShowUndoHistory => {
self.overlay = Overlay::UndoHistory;
Some(AppCommand::FetchUndoHistory)
}
Action::EnterColonMode => {
self.input_mode = InputMode::Command;
self.colon_mode = true;
self.input.clear();
self.input_cursor = 0;
None
}
Action::ClickAt(target) => {
match target {
ClickTarget::ViewTab(view) => {
self.view_state = view;
if view == ViewState::Fix
&& let Some(scan) = &self.last_scan {
self.fix_view = FixViewState::from_scan(&scan.findings);
}
}
ClickTarget::FindingRow(idx) => {
self.scan_view.selected_finding = Some(idx);
}
ClickTarget::FixCheckbox(idx) => {
self.fix_view.toggle_at(idx);
}
ClickTarget::SidebarToggle => {
self.sidebar_visible = !self.sidebar_visible;
}
}
None
}
Action::ScrollLines(lines) => {
self.scroll_events.push(Instant::now());
// Trim old events (keep last 500ms)
let cutoff = Instant::now().checked_sub(std::time::Duration::from_millis(500)).unwrap();
self.scroll_events.retain(|&t| t > cutoff);
if lines > 0 {
for _ in 0..lines {
self.apply_action(Action::ScrollDown);
}
} else {
for _ in 0..(-lines) {
self.apply_action(Action::ScrollUp);
}
}
None
}
Action::None => None,
}
}
}