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