vtcode_core/utils/ansi/
core.rs1use super::*;
4
5use crate::config::ToolDisplayMode;
6use crate::config::loader::SyntaxHighlightingConfig;
7use crate::ui::tui::{InlineHandle, InlineMessageKind};
8use crate::utils::ansi_capabilities::AnsiCapabilities;
9use anstream::{AutoStream, ColorChoice};
10use std::io;
11use vtcode_commons::color_policy::{self, ColorOutputPolicySource};
12use vtcode_commons::ui_protocol::ToolOutputId;
13
14impl AnsiRenderer {
15 pub fn stdout() -> Self {
17 let mut capabilities = AnsiCapabilities::detect();
18 let policy = color_policy::current_color_output_policy();
19
20 if !policy.enabled {
21 capabilities.no_color = true;
22 capabilities.force_color = false;
23 } else if matches!(
24 policy.source,
25 ColorOutputPolicySource::CliColorAlways | ColorOutputPolicySource::ConfigOverride
26 ) {
27 capabilities.no_color = false;
28 capabilities.force_color = true;
29 }
30
31 let color = capabilities.supports_color();
32 let choice = if !color {
33 ColorChoice::Never
34 } else if matches!(
35 policy.source,
36 ColorOutputPolicySource::CliColorAlways | ColorOutputPolicySource::ConfigOverride
37 ) {
38 ColorChoice::Always
39 } else {
40 ColorChoice::Auto
41 };
42 Self {
43 writer: AutoStream::new(io::stdout(), choice),
44 buffer: String::with_capacity(1024),
45 color,
46 sink: None,
47 last_line_was_empty: false,
48 highlight_config: SyntaxHighlightingConfig::default(),
49 capabilities,
50 reasoning_visible: true,
51 screen_reader_mode: false,
52 show_diagnostics_in_transcript: false,
53 show_message_timestamps: true,
54 pending_response_footer: false,
55 defer_turn_footer: false,
56 tool_display_mode: ToolDisplayMode::Compact,
57 communication_style: vtcode_commons::ui_protocol::CommunicationStyle::Balanced,
58 diff_preview_mode: vtcode_commons::ui_protocol::DiffPreviewMode::Inline,
59 compact_command_group: None,
60 next_compact_group_id: 0,
61 pending_tool_output_anchor: None,
62 session_expand_anchor: None,
63 session_body: false,
64 diagnostic_sink: None,
65 diagnostic_history: std::collections::VecDeque::new(),
66 }
67 }
68
69 pub fn with_inline_ui(handle: InlineHandle, highlight_config: SyntaxHighlightingConfig) -> Self {
71 let mut renderer = Self::stdout();
72 renderer.highlight_config = highlight_config.clone();
73 renderer.sink = Some(InlineSink::new(handle, highlight_config));
74 renderer.last_line_was_empty = false;
75 renderer
76 }
77
78 #[cfg(feature = "tui")]
80 pub fn program_status_wait(
81 &self,
82 kind: vtcode_commons::program_status::InteractionKind,
83 ) -> Option<vtcode_ui::tui::core_tui::types::ProgramStatusWaitGuard> {
84 self.sink.as_ref().map(|sink| sink.handle.program_status_wait(kind))
85 }
86
87 pub fn set_highlight_config(&mut self, config: SyntaxHighlightingConfig) {
89 if let Some(sink) = &mut self.sink {
90 sink.set_highlight_config(config.clone());
91 }
92 self.highlight_config = config;
93 }
94
95 pub fn begin_transcript_batch(&mut self) {
100 if let Some(sink) = &mut self.sink {
101 sink.begin_transcript_batch();
102 }
103 }
104
105 pub fn end_transcript_batch(&mut self) {
107 if let Some(sink) = &mut self.sink {
108 sink.end_transcript_batch();
109 }
110 }
111
112 pub fn transcript_batch<T>(&mut self, render: impl FnOnce(&mut Self) -> anyhow::Result<T>) -> anyhow::Result<T> {
117 self.begin_transcript_batch();
118 let result = render(self);
119 self.end_transcript_batch();
120 result
121 }
122
123 pub fn set_next_tool_output_anchor(&mut self, id: ToolOutputId) {
129 self.pending_tool_output_anchor = Some(id);
130 }
131
132 pub fn set_session_expand_anchor(&mut self, id: ToolOutputId) {
136 self.session_expand_anchor = Some(id);
137 }
138
139 pub fn take_session_expand_anchor(&mut self) -> Option<ToolOutputId> {
141 self.session_expand_anchor.take()
142 }
143
144 pub fn set_session_body(&mut self, session_body: bool) {
147 self.session_body = session_body;
148 }
149
150 pub fn session_body_active(&self) -> bool {
152 self.session_body
153 }
154
155 pub fn was_previous_line_empty(&self) -> bool {
157 self.last_line_was_empty
158 }
159
160 pub(super) fn message_kind(style: MessageStyle) -> InlineMessageKind {
161 style.message_kind()
162 }
163
164 pub fn supports_streaming_markdown(&self) -> bool {
165 self.sink.is_some()
166 }
167
168 pub fn prefers_untruncated_output(&self) -> bool {
173 self.sink.is_some()
174 }
175
176 pub fn supports_inline_ui(&self) -> bool {
177 self.sink.is_some()
178 }
179
180 pub fn set_reasoning_visible(&mut self, visible: bool) {
181 self.reasoning_visible = visible;
182 }
183
184 pub fn reasoning_visible(&self) -> bool {
185 self.reasoning_visible
186 }
187
188 pub fn writes_to_inline_sink(&self) -> bool {
191 self.sink.is_some()
192 }
193
194 pub fn set_screen_reader_mode(&mut self, enabled: bool) {
195 self.screen_reader_mode = enabled;
196 }
197
198 pub fn set_show_diagnostics_in_transcript(&mut self, enabled: bool) {
199 self.show_diagnostics_in_transcript = if cfg!(debug_assertions) { enabled } else { false };
200 }
201
202 pub fn begin_turn_message_timestamp(&mut self) {
205 self.pending_response_footer = false;
206 self.defer_turn_footer = true;
207 }
208
209 pub fn set_show_message_timestamps(&mut self, enabled: bool) {
214 self.show_message_timestamps = enabled;
215 }
216
217 pub fn show_message_timestamps(&self) -> bool {
218 self.show_message_timestamps
219 }
220
221 pub fn set_tool_display_mode(&mut self, mode: ToolDisplayMode) {
222 self.flush_compact_command_group();
223 self.tool_display_mode = match mode {
224 ToolDisplayMode::Compact => ToolDisplayMode::Compact,
225 ToolDisplayMode::Expanded | ToolDisplayMode::Unknown => ToolDisplayMode::Expanded,
226 };
227 }
228
229 pub fn tool_display_mode(&self) -> ToolDisplayMode {
230 self.tool_display_mode
231 }
232
233 pub fn set_communication_style(&mut self, style: vtcode_commons::ui_protocol::CommunicationStyle) {
237 self.communication_style = style.resolve();
238 }
239
240 pub fn communication_style(&self) -> vtcode_commons::ui_protocol::CommunicationStyle {
241 self.communication_style
242 }
243
244 pub fn loop_notices_visible(&self) -> bool {
255 use vtcode_commons::ui_protocol::CommunicationStyle;
256 !self.writes_to_inline_sink() || matches!(self.communication_style(), CommunicationStyle::Verbose)
257 }
258
259 pub fn inline_failure_extras_visible(&self, is_command_tool: bool) -> bool {
260 use vtcode_commons::ui_protocol::CommunicationStyle;
261 match self.communication_style.resolve() {
262 CommunicationStyle::Verbose => true,
263 CommunicationStyle::Balanced => is_command_tool,
264 CommunicationStyle::Quiet | CommunicationStyle::Unknown => false,
267 }
268 }
269
270 pub fn is_compact_display(&self) -> bool {
276 self.tool_display_mode == ToolDisplayMode::Compact
277 }
278
279 pub fn set_diff_preview_mode(&mut self, mode: vtcode_commons::ui_protocol::DiffPreviewMode) {
280 self.diff_preview_mode = mode;
281 }
282
283 pub fn record_diff_review(&self, anchor: vtcode_commons::ui_protocol::DiffReviewAnchor) {
285 if let Some(sink) = &self.sink {
286 sink.handle.record_diff_review(anchor);
287 }
288 }
289
290 pub fn diff_preview_mode(&self) -> vtcode_commons::ui_protocol::DiffPreviewMode {
291 self.diff_preview_mode
292 }
293
294 pub fn toggle_tool_display_mode(&mut self) -> ToolDisplayMode {
295 self.flush_compact_command_group();
296 let next = match self.tool_display_mode {
297 ToolDisplayMode::Expanded | ToolDisplayMode::Unknown => ToolDisplayMode::Compact,
298 ToolDisplayMode::Compact => ToolDisplayMode::Expanded,
299 };
300 self.tool_display_mode = next;
301 next
302 }
303}