vtcode_ui/tui/core_tui/session/
impl_input.rs1use super::*;
2#[cfg_attr(not(vendored_crossterm), allow(unused_imports))]
3use vtcode_commons::ansi_capabilities::ColorScheme;
4
5impl Session {
6 pub(crate) fn cursor(&self) -> usize {
7 self.input_manager.cursor()
8 }
9
10 pub(crate) fn set_input(&mut self, text: impl Into<String>) {
11 self.input_manager.set_content(text.into());
12 self.input_compact_mode = self.input_compact_placeholder().is_some();
13 self.mark_dirty();
14 }
15
16 pub(crate) fn set_cursor(&mut self, pos: usize) {
17 self.input_manager.set_cursor(pos);
18 self.mark_dirty();
19 }
20
21 #[cfg_attr(feature = "profiling", hotpath::measure)]
22 pub(crate) fn process_key(&mut self, key: KeyEvent) -> Option<InlineEvent> {
23 events::process_key(self, key)
24 }
25
26 pub fn handle_command(&mut self, command: InlineCommand) {
27 let mut command_needs_redraw = true;
28
29 if matches!(
31 &command,
32 InlineCommand::AppendLine { kind: InlineMessageKind::Agent, segments }
33 if !segments.is_empty()
34 ) || matches!(
35 &command,
36 InlineCommand::AppendLines { lines }
37 if lines
38 .iter()
39 .any(|line| line.kind == InlineMessageKind::Agent && !line.segments.is_empty())
40 ) || matches!(
41 &command,
42 InlineCommand::AppendPastedMessage { kind: InlineMessageKind::Agent, text, .. }
43 if !text.is_empty()
44 ) || matches!(
45 &command,
46 InlineCommand::Inline { kind: InlineMessageKind::Agent, segment }
47 if !segment.text.is_empty()
48 ) {
49 self.is_streaming_final_answer = true;
50 }
51
52 if let InlineCommand::SetInputStatus { left, right } | InlineCommand::SetConfiguredInputStatus { left, right } =
54 &command
55 && self.is_streaming_final_answer
56 && left.is_none()
57 && right.is_none()
58 {
59 self.is_streaming_final_answer = false;
60 }
61
62 match command {
63 InlineCommand::AppendLine { kind, segments } => {
64 let previous_max_offset = self.current_max_scroll_offset();
65 self.clear_thinking_spinner_if_active(kind);
66 self.push_line(kind, segments);
67 self.adjust_scroll_after_change(previous_max_offset);
68 self.request_transcript_clear();
69 }
70 InlineCommand::AppendLines { lines } => {
71 let previous_max_offset = self.current_max_scroll_offset();
72 for line in lines {
73 self.clear_thinking_spinner_if_active(line.kind);
74 self.push_line(line.kind, line.segments);
75 }
76 self.adjust_scroll_after_change(previous_max_offset);
77 self.request_transcript_clear();
78 }
79 InlineCommand::AppendPastedMessage { kind, text, line_count } => {
80 self.clear_thinking_spinner_if_active(kind);
81 self.append_pasted_message(kind, text, line_count);
82 self.request_transcript_clear();
83 }
84 InlineCommand::Inline { kind, segment } => {
85 self.clear_thinking_spinner_if_active(kind);
86 self.append_inline(kind, segment);
87 self.request_transcript_clear();
88 }
89 InlineCommand::ReplaceLast { count, kind, lines, link_ranges } => {
90 self.clear_thinking_spinner_if_active(kind);
91 self.replace_last(count, kind, lines, link_ranges);
92 self.request_transcript_clear();
93 }
94 InlineCommand::SetPrompt { prefix, style } => {
95 self.prompt_prefix = prefix;
96 self.prompt_style = style;
97 self.ensure_prompt_style_color();
98 self.input_render_cache = None;
99 }
100 InlineCommand::SetPlaceholder { hint, style } => {
101 self.placeholder = hint;
102 self.placeholder_style = style;
103 self.input_render_cache = None;
104 }
105 InlineCommand::SetMessageLabels { agent, user } => {
106 self.labels.agent = agent.filter(|label| !label.is_empty());
107 self.labels.user = user.filter(|label| !label.is_empty());
108 self.invalidate_transcript_cache();
109 self.invalidate_scroll_metrics();
110 }
111 InlineCommand::SetHeaderContext { context } => {
112 let mut next_context = *context;
113 next_context.reasoning_stage = self.header_context.reasoning_stage.clone();
114 next_context.primary_agent = self.header_context.primary_agent.clone();
115 next_context.primary_agent_color = self.header_context.primary_agent_color.clone();
116 self.header_context = next_context;
117 self.invalidate_header_cache();
118 }
119 InlineCommand::SetInputStatus { left, right } => {
120 self.apply_input_status(left, right);
121 }
122 InlineCommand::SetConfiguredInputStatus { left, right } => {
123 self.footer_context_configured = true;
124 self.footer_context_right = right.clone();
125 self.footer_context_status = left
126 .as_deref()
127 .map(str::trim)
128 .filter(|value| !value.is_empty())
129 .map(str::to_owned);
130 self.apply_input_status(left, right);
131 }
132 InlineCommand::ProgramStatus(update) => {
133 self.program_status.apply(update);
134 if matches!(update, vtcode_commons::program_status::ProgramStatusUpdate::Configure { enabled: true }) {
135 self.program_status.children(&self.local_agents);
136 }
137 command_needs_redraw = false;
138 }
139 InlineCommand::UpdateProgress(update) => {
140 self.program_status.progress(update);
141 if self.progress.apply(update) {
142 self.request_transcript_clear();
143 } else {
144 command_needs_redraw = false;
145 }
146 }
147 InlineCommand::SetActivityState(state) => {
148 self.program_status.activity(state);
149 self.activity_state = state;
150 self.input_status_left = state.status().map(ToOwned::to_owned);
151 let enabled = !state.is_busy();
152 self.set_input_enabled(enabled);
153 if let Some(ActiveOverlay::Modal(overlay)) = self.active_overlay.as_mut() {
154 overlay.restore_cursor = enabled;
155 }
156 self.cursor_visible = enabled && !self.has_active_overlay();
157 self.invalidate_header_cache();
160 }
161 InlineCommand::SetTerminalTitleItems { items } => {
162 if self.terminal_title_items != items {
163 self.terminal_title_items = items;
164 self.mark_visual_dirty();
165 } else {
166 command_needs_redraw = false;
167 }
168 }
169 InlineCommand::SetTerminalTitleThreadLabel { label } => {
170 let label = label.filter(|value| !value.trim().is_empty());
171 if self.terminal_title_thread_label != label {
172 self.terminal_title_thread_label = label;
173 self.mark_visual_dirty();
174 } else {
175 command_needs_redraw = false;
176 }
177 }
178 InlineCommand::SetTerminalTitleGitBranch { branch } => {
179 let branch = branch.filter(|value| !value.trim().is_empty());
180 if self.terminal_title_git_branch != branch {
181 self.terminal_title_git_branch = branch;
182 self.mark_visual_dirty();
183 } else {
184 command_needs_redraw = false;
185 }
186 }
187 InlineCommand::SetTheme { theme } => {
188 let previous_theme = self.theme.clone();
189 self.theme = theme.clone();
190 self.styles.set_theme(theme);
191 self.retint_lines_for_theme_change(&previous_theme);
192 self.ensure_prompt_style_color();
193 self.invalidate_header_cache();
194 self.invalidate_transcript_cache();
195 }
196 InlineCommand::SetColorSchemeAuto { enabled } => {
197 self.auto_color_scheme = enabled;
198 command_needs_redraw = false;
199 }
200 InlineCommand::SetAppearance { appearance } => {
201 self.appearance = appearance;
202 self.invalidate_header_cache();
203 self.invalidate_transcript_cache();
204 self.invalidate_scroll_metrics();
205 }
206 InlineCommand::SetFullscreenInteraction { interaction } => {
207 self.fullscreen.interaction = interaction;
208 self.mark_visual_dirty();
209 }
210 InlineCommand::SetVimModeEnabled(enabled) => {
211 self.vim_state.set_enabled(enabled);
212 self.mark_visual_dirty();
213 }
214 InlineCommand::SetQueuedInputs { entries } => {
215 self.set_queued_inputs_entries(entries);
216 self.mark_visual_dirty();
217 }
218 InlineCommand::SetSubprocessEntries { entries } => {
219 self.subprocess_entries = entries;
220 self.invalidate_sidebar_cache();
221 }
222 InlineCommand::SetSubagentPreview { text } => {
223 self.subagent_preview = text.filter(|value| !value.trim().is_empty());
224 self.invalidate_sidebar_cache();
225 }
226 InlineCommand::SetPrimaryAgent { name, color } => {
227 self.header_context.primary_agent = name.filter(|value| !value.trim().is_empty());
228 self.header_context.primary_agent_color = color.filter(|value| !value.trim().is_empty());
229 self.invalidate_header_cache();
230 }
231 InlineCommand::SetCursorVisible(value) => {
232 self.cursor_visible = value;
233 }
234 InlineCommand::SetInputEnabled(value) => {
235 self.set_input_enabled(value);
236 }
237 InlineCommand::SetImageInputEnabled(value) => {
238 self.image_input_enabled = value;
239 }
240 InlineCommand::SetInput(content) => {
241 if Self::is_error_content(&content) {
244 crate::tui::utils::transcript::display_error(&content);
246 } else {
247 self.clear_suggested_prompt_state();
248 self.clear_inline_prompt_suggestion();
249 self.input_manager.set_content(content);
250 self.input_compact_mode = self.input_compact_placeholder().is_some();
251 self.scroll_manager.set_offset(0);
252 }
253 }
254 InlineCommand::RestoreInputDraft(input) => {
255 self.clear_suggested_prompt_state();
256 self.clear_inline_prompt_suggestion();
257 self.input_manager.set_content(input.text);
258 self.input_manager.set_attachments(input.attachments);
259 self.input_compact_mode = self.input_compact_placeholder().is_some();
260 self.scroll_manager.set_offset(0);
261 }
262 InlineCommand::ApplySuggestedPrompt(content) => {
263 self.apply_suggested_prompt(content);
264 self.scroll_manager.set_offset(0);
265 }
266 InlineCommand::SetInlinePromptSuggestion { suggestion, llm_generated } => {
267 self.set_inline_prompt_suggestion(suggestion, llm_generated);
268 }
269 InlineCommand::ClearInlinePromptSuggestion => {
270 self.clear_inline_prompt_suggestion();
271 }
272 InlineCommand::ClearInput => {
273 command::clear_input(self);
274 }
275 InlineCommand::ForceRedraw => {
276 self.mark_dirty();
277 }
278 InlineCommand::ShowOverlay { request } => {
279 self.clear_inline_prompt_suggestion();
280 self.show_overlay(*request);
281 }
282 InlineCommand::CloseOverlay => {
283 self.close_overlay();
284 }
285 InlineCommand::ClearScreen => {
286 self.clear_screen();
287 }
288 InlineCommand::SuspendEventLoop
289 | InlineCommand::ResumeEventLoop
290 | InlineCommand::ClearInputQueue
291 | InlineCommand::StopEventStream
292 | InlineCommand::StartEventStream => {
293 }
295 InlineCommand::SetSkipConfirmations(skip) => {
296 if skip {
297 self.close_overlay();
301 }
302 }
303 InlineCommand::Shutdown => {
304 self.request_exit();
305 }
306 InlineCommand::SetReasoningStage(stage) => {
307 self.header_context.reasoning_stage = stage;
308 self.invalidate_header_cache();
309 }
310 }
311 if command_needs_redraw {
312 self.mark_visual_dirty();
313 }
314 }
315
316 fn apply_input_status(&mut self, left: Option<String>, right: Option<String>) {
317 self.input_status_left = left;
318 self.input_status_right = right;
319 if self.thinking_spinner.is_active {
320 self.thinking_spinner.stop();
321 }
322 self.invalidate_header_cache();
324 }
325
326 #[cfg_attr(not(vendored_crossterm), allow(dead_code))]
336 pub(crate) fn apply_terminal_color_scheme_report(&mut self, dark: bool) {
337 if !self.auto_color_scheme {
338 return;
339 }
340 let scheme = if dark { ColorScheme::Dark } else { ColorScheme::Light };
341 vtcode_commons::ansi_capabilities::set_color_scheme_override(Some(scheme));
342 let active = crate::theme::active_theme_id();
343 if crate::theme::theme_matches_terminal_scheme(&active) {
344 return;
345 }
346 let next = crate::theme::theme_for_terminal_scheme_change(&active, dark);
347 if crate::theme::set_active_theme(next).is_err() {
348 return;
349 }
350 let theme = crate::tui::core_tui::style::theme_from_styles(&crate::theme::active_styles());
351 self.handle_command(InlineCommand::SetTheme { theme });
352 }
353}