Skip to main content

vtcode_tui/core_tui/session/
impl_input.rs

1use super::*;
2
3impl Session {
4    pub fn cursor(&self) -> usize {
5        self.input_manager.cursor()
6    }
7
8    pub fn set_input(&mut self, text: impl Into<String>) {
9        self.input_manager.set_content(text.into());
10        self.input_compact_mode = self.input_compact_placeholder().is_some();
11        self.check_file_reference_trigger();
12        self.mark_dirty();
13    }
14
15    pub fn set_cursor(&mut self, pos: usize) {
16        self.input_manager.set_cursor(pos);
17        self.mark_dirty();
18    }
19
20    pub fn process_key(&mut self, key: KeyEvent) -> Option<InlineEvent> {
21        events::process_key(self, key)
22    }
23
24    pub fn handle_command(&mut self, command: InlineCommand) {
25        // Track streaming state: set when agent starts responding
26        if matches!(
27            &command,
28            InlineCommand::AppendLine { kind: InlineMessageKind::Agent, segments }
29                if !segments.is_empty()
30        ) || matches!(
31            &command,
32            InlineCommand::AppendPastedMessage { kind: InlineMessageKind::Agent, text, .. }
33                if !text.is_empty()
34        ) || matches!(
35            &command,
36            InlineCommand::Inline { kind: InlineMessageKind::Agent, segment }
37                if !segment.text.is_empty()
38        ) {
39            self.is_streaming_final_answer = true;
40        }
41
42        // Clear streaming state on turn completion (status cleared)
43        if let InlineCommand::SetInputStatus { left, right } = &command
44            && self.is_streaming_final_answer
45            && left.is_none()
46            && right.is_none()
47        {
48            self.is_streaming_final_answer = false;
49        }
50
51        match command {
52            InlineCommand::AppendLine { kind, segments } => {
53                self.clear_thinking_spinner_if_active(kind);
54                self.push_line(kind, segments);
55                self.transcript_content_changed = true;
56            }
57            InlineCommand::AppendPastedMessage {
58                kind,
59                text,
60                line_count,
61            } => {
62                self.clear_thinking_spinner_if_active(kind);
63                self.append_pasted_message(kind, text, line_count);
64                self.transcript_content_changed = true;
65            }
66            InlineCommand::Inline { kind, segment } => {
67                self.clear_thinking_spinner_if_active(kind);
68                self.append_inline(kind, segment);
69                self.transcript_content_changed = true;
70            }
71            InlineCommand::ReplaceLast {
72                count,
73                kind,
74                lines,
75                link_ranges,
76            } => {
77                self.clear_thinking_spinner_if_active(kind);
78                self.replace_last(count, kind, lines, link_ranges);
79                self.transcript_content_changed = true;
80            }
81            InlineCommand::SetPrompt { prefix, style } => {
82                self.prompt_prefix = prefix;
83                self.prompt_style = style;
84                self.ensure_prompt_style_color();
85            }
86            InlineCommand::SetPlaceholder { hint, style } => {
87                self.placeholder = hint;
88                self.placeholder_style = style;
89            }
90            InlineCommand::SetMessageLabels { agent, user } => {
91                self.labels.agent = agent.filter(|label| !label.is_empty());
92                self.labels.user = user.filter(|label| !label.is_empty());
93                self.invalidate_scroll_metrics();
94            }
95            InlineCommand::SetHeaderContext { context } => {
96                self.header_context = *context;
97                self.needs_redraw = true;
98            }
99            InlineCommand::SetInputStatus { left, right } => {
100                self.input_status_left = left;
101                self.input_status_right = right;
102                if self.thinking_spinner.is_active {
103                    self.thinking_spinner.stop();
104                }
105                self.needs_redraw = true;
106            }
107            InlineCommand::SetTheme { theme } => {
108                let previous_theme = self.theme.clone();
109                self.theme = theme.clone();
110                self.styles.set_theme(theme);
111                self.retint_lines_for_theme_change(&previous_theme);
112                self.ensure_prompt_style_color();
113                self.invalidate_transcript_cache();
114            }
115            InlineCommand::SetAppearance { appearance } => {
116                self.appearance = appearance;
117                self.invalidate_transcript_cache();
118                self.invalidate_scroll_metrics();
119            }
120            InlineCommand::SetVimModeEnabled(enabled) => {
121                self.vim_state.set_enabled(enabled);
122                self.needs_redraw = true;
123            }
124            InlineCommand::SetQueuedInputs { entries } => {
125                self.set_queued_inputs_entries(entries);
126                self.mark_dirty();
127            }
128            InlineCommand::SetCursorVisible(value) => {
129                self.cursor_visible = value;
130            }
131            InlineCommand::SetInputEnabled(value) => {
132                self.input_enabled = value;
133                slash::update_slash_suggestions(self);
134            }
135            InlineCommand::SetInput(content) => {
136                // Check if the content appears to be an error message
137                // If it looks like an error, redirect to transcript instead
138                if Self::is_error_content(&content) {
139                    // Add error to transcript instead of input field
140                    crate::utils::transcript::display_error(&content);
141                } else {
142                    self.clear_suggested_prompt_state();
143                    self.input_manager.set_content(content);
144                    self.input_compact_mode = self.input_compact_placeholder().is_some();
145                    self.scroll_manager.set_offset(0);
146                    slash::update_slash_suggestions(self);
147                    self.check_file_reference_trigger();
148                }
149            }
150            InlineCommand::ApplySuggestedPrompt(content) => {
151                self.apply_suggested_prompt(content);
152                self.scroll_manager.set_offset(0);
153                self.check_file_reference_trigger();
154            }
155            InlineCommand::SetTaskPanelVisible(visible) => {
156                self.set_task_panel_visible(visible);
157            }
158            InlineCommand::SetTaskPanelLines(lines) => {
159                self.set_task_panel_lines(lines);
160            }
161            InlineCommand::ClearInput => {
162                command::clear_input(self);
163            }
164            InlineCommand::ForceRedraw => {
165                self.mark_dirty();
166            }
167            InlineCommand::ShowOverlay { request } => {
168                self.show_overlay(*request);
169            }
170            InlineCommand::CloseOverlay => {
171                self.close_overlay();
172            }
173            InlineCommand::LoadFilePalette { files, workspace } => {
174                self.load_file_palette(files, workspace);
175            }
176            InlineCommand::OpenHistoryPicker => {
177                events::open_history_picker(self);
178            }
179            InlineCommand::ClearScreen => {
180                self.clear_screen();
181            }
182            InlineCommand::SuspendEventLoop
183            | InlineCommand::ResumeEventLoop
184            | InlineCommand::ClearInputQueue => {
185                // Handled by drive_terminal
186            }
187            InlineCommand::SetEditingMode(mode) => {
188                self.header_context.editing_mode = mode;
189                self.needs_redraw = true;
190            }
191            InlineCommand::SetAutonomousMode(enabled) => {
192                self.header_context.autonomous_mode = enabled;
193                self.needs_redraw = true;
194            }
195            InlineCommand::SetSkipConfirmations(skip) => {
196                self.skip_confirmations = skip;
197                if skip {
198                    self.close_overlay();
199                }
200            }
201            InlineCommand::Shutdown => {
202                self.request_exit();
203            }
204            InlineCommand::SetReasoningStage(stage) => {
205                self.header_context.reasoning_stage = stage;
206                self.invalidate_header_cache();
207            }
208        }
209        self.needs_redraw = true;
210    }
211}