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.input_manager.set_content(content);
142                    self.input_compact_mode = self.input_compact_placeholder().is_some();
143                    self.scroll_manager.set_offset(0);
144                }
145            }
146            InlineCommand::ApplySuggestedPrompt(content) => {
147                self.apply_suggested_prompt(content);
148                self.scroll_manager.set_offset(0);
149            }
150            InlineCommand::ClearInput => {
151                command::clear_input(self);
152            }
153            InlineCommand::ForceRedraw => {
154                self.mark_dirty();
155            }
156            InlineCommand::ShowOverlay { request } => {
157                self.show_overlay(*request);
158            }
159            InlineCommand::CloseOverlay => {
160                self.close_overlay();
161            }
162            InlineCommand::ClearScreen => {
163                self.clear_screen();
164            }
165            InlineCommand::SuspendEventLoop
166            | InlineCommand::ResumeEventLoop
167            | InlineCommand::ClearInputQueue => {
168                // Handled by drive_terminal
169            }
170            InlineCommand::SetEditingMode(mode) => {
171                self.header_context.editing_mode = mode;
172                self.needs_redraw = true;
173            }
174            InlineCommand::SetAutonomousMode(enabled) => {
175                self.header_context.autonomous_mode = enabled;
176                self.needs_redraw = true;
177            }
178            InlineCommand::SetSkipConfirmations(skip) => {
179                self.skip_confirmations = skip;
180                if skip {
181                    self.close_overlay();
182                }
183            }
184            InlineCommand::Shutdown => {
185                self.request_exit();
186            }
187            InlineCommand::SetReasoningStage(stage) => {
188                self.header_context.reasoning_stage = stage;
189                self.invalidate_header_cache();
190            }
191        }
192        self.needs_redraw = true;
193    }
194}