Skip to main content

vtcode_ui/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.request_transcript_clear();
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.request_transcript_clear();
64            }
65            InlineCommand::Inline { kind, segment } => {
66                self.clear_thinking_spinner_if_active(kind);
67                self.append_inline(kind, segment);
68                self.request_transcript_clear();
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.request_transcript_clear();
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_transcript_cache();
93                self.invalidate_scroll_metrics();
94            }
95            InlineCommand::SetHeaderContext { context } => {
96                let mut next_context = *context;
97                next_context.reasoning_stage = self.header_context.reasoning_stage.clone();
98                next_context.primary_agent = self.header_context.primary_agent.clone();
99                self.header_context = next_context;
100                self.invalidate_header_cache();
101            }
102            InlineCommand::SetInputStatus { left, right } => {
103                self.input_status_left = left;
104                self.input_status_right = right;
105                if self.thinking_spinner.is_active {
106                    self.thinking_spinner.stop();
107                }
108                self.needs_redraw = true;
109            }
110            InlineCommand::SetTerminalTitleItems { items } => {
111                self.terminal_title_items = items;
112                self.needs_redraw = true;
113            }
114            InlineCommand::SetTerminalTitleThreadLabel { label } => {
115                self.terminal_title_thread_label = label.filter(|value| !value.trim().is_empty());
116                self.needs_redraw = true;
117            }
118            InlineCommand::SetTerminalTitleGitBranch { branch } => {
119                self.terminal_title_git_branch = branch.filter(|value| !value.trim().is_empty());
120                self.needs_redraw = true;
121            }
122            InlineCommand::SetTheme { theme } => {
123                let previous_theme = self.theme.clone();
124                self.theme = theme.clone();
125                self.styles.set_theme(theme);
126                self.retint_lines_for_theme_change(&previous_theme);
127                self.ensure_prompt_style_color();
128                self.invalidate_transcript_cache();
129            }
130            InlineCommand::SetAppearance { appearance } => {
131                self.appearance = appearance;
132                self.invalidate_header_cache();
133                self.invalidate_transcript_cache();
134                self.invalidate_scroll_metrics();
135            }
136            InlineCommand::SetVimModeEnabled(enabled) => {
137                self.vim_state.set_enabled(enabled);
138                self.needs_redraw = true;
139            }
140            InlineCommand::SetQueuedInputs { entries } => {
141                self.set_queued_inputs_entries(entries);
142                self.mark_dirty();
143            }
144            InlineCommand::SetSubprocessEntries { entries } => {
145                self.subprocess_entries = entries;
146                self.invalidate_sidebar_cache();
147            }
148            InlineCommand::SetSubagentPreview { text } => {
149                self.subagent_preview = text.filter(|value| !value.trim().is_empty());
150                self.invalidate_sidebar_cache();
151            }
152            InlineCommand::SetPrimaryAgent { name } => {
153                self.header_context.primary_agent = name.filter(|value| !value.trim().is_empty());
154                self.invalidate_header_cache();
155            }
156            InlineCommand::SetCursorVisible(value) => {
157                self.cursor_visible = value;
158            }
159            InlineCommand::SetInputEnabled(value) => {
160                self.input_enabled = value;
161            }
162            InlineCommand::SetInput(content) => {
163                // Check if the content appears to be an error message
164                // If it looks like an error, redirect to transcript instead
165                if Self::is_error_content(&content) {
166                    // Add error to transcript instead of input field
167                    crate::tui::utils::transcript::display_error(&content);
168                } else {
169                    self.clear_suggested_prompt_state();
170                    self.clear_inline_prompt_suggestion();
171                    self.input_manager.set_content(content);
172                    self.input_compact_mode = self.input_compact_placeholder().is_some();
173                    self.scroll_manager.set_offset(0);
174                }
175            }
176            InlineCommand::ApplySuggestedPrompt(content) => {
177                self.apply_suggested_prompt(content);
178                self.scroll_manager.set_offset(0);
179            }
180            InlineCommand::SetInlinePromptSuggestion {
181                suggestion,
182                llm_generated,
183            } => {
184                self.set_inline_prompt_suggestion(suggestion, llm_generated);
185            }
186            InlineCommand::ClearInlinePromptSuggestion => {
187                self.clear_inline_prompt_suggestion();
188            }
189            InlineCommand::ClearInput => {
190                command::clear_input(self);
191            }
192            InlineCommand::ForceRedraw => {
193                self.mark_dirty();
194            }
195            InlineCommand::ShowOverlay { request } => {
196                self.clear_inline_prompt_suggestion();
197                self.show_overlay(*request);
198            }
199            InlineCommand::CloseOverlay => {
200                self.close_overlay();
201            }
202            InlineCommand::ClearScreen => {
203                self.clear_screen();
204            }
205            InlineCommand::SuspendEventLoop
206            | InlineCommand::ResumeEventLoop
207            | InlineCommand::ClearInputQueue
208            | InlineCommand::StopEventStream
209            | InlineCommand::StartEventStream => {
210                // Handled by drive_terminal
211            }
212            InlineCommand::SetSkipConfirmations(skip) => {
213                self.skip_confirmations = skip;
214                if skip {
215                    self.close_overlay();
216                }
217            }
218            InlineCommand::Shutdown => {
219                self.request_exit();
220            }
221            InlineCommand::SetReasoningStage(stage) => {
222                self.header_context.reasoning_stage = stage;
223                self.invalidate_header_cache();
224            }
225        }
226        self.needs_redraw = true;
227    }
228}