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 tool_display_mode: ToolDisplayMode::Compact,
54 diff_preview_mode: vtcode_commons::ui_protocol::DiffPreviewMode::Inline,
55 compact_command_group: None,
56 next_compact_group_id: 0,
57 pending_tool_output_anchor: None,
58 session_expand_anchor: None,
59 session_body: false,
60 diagnostic_sink: None,
61 diagnostic_history: std::collections::VecDeque::new(),
62 }
63 }
64
65 pub fn with_inline_ui(handle: InlineHandle, highlight_config: SyntaxHighlightingConfig) -> Self {
67 let mut renderer = Self::stdout();
68 renderer.highlight_config = highlight_config.clone();
69 renderer.sink = Some(InlineSink::new(handle, highlight_config));
70 renderer.last_line_was_empty = false;
71 renderer
72 }
73
74 #[cfg(feature = "tui")]
76 pub fn program_status_wait(
77 &self,
78 kind: vtcode_commons::program_status::InteractionKind,
79 ) -> Option<vtcode_ui::tui::core_tui::types::ProgramStatusWaitGuard> {
80 self.sink.as_ref().map(|sink| sink.handle.program_status_wait(kind))
81 }
82
83 pub fn set_highlight_config(&mut self, config: SyntaxHighlightingConfig) {
85 if let Some(sink) = &mut self.sink {
86 sink.set_highlight_config(config.clone());
87 }
88 self.highlight_config = config;
89 }
90
91 pub fn begin_transcript_batch(&mut self) {
96 if let Some(sink) = &mut self.sink {
97 sink.begin_transcript_batch();
98 }
99 }
100
101 pub fn end_transcript_batch(&mut self) {
103 if let Some(sink) = &mut self.sink {
104 sink.end_transcript_batch();
105 }
106 }
107
108 pub fn transcript_batch<T>(&mut self, render: impl FnOnce(&mut Self) -> anyhow::Result<T>) -> anyhow::Result<T> {
113 self.begin_transcript_batch();
114 let result = render(self);
115 self.end_transcript_batch();
116 result
117 }
118
119 pub fn set_next_tool_output_anchor(&mut self, id: ToolOutputId) {
125 self.pending_tool_output_anchor = Some(id);
126 }
127
128 pub fn set_session_expand_anchor(&mut self, id: ToolOutputId) {
132 self.session_expand_anchor = Some(id);
133 }
134
135 pub fn take_session_expand_anchor(&mut self) -> Option<ToolOutputId> {
137 self.session_expand_anchor.take()
138 }
139
140 pub fn set_session_body(&mut self, session_body: bool) {
143 self.session_body = session_body;
144 }
145
146 pub fn session_body_active(&self) -> bool {
148 self.session_body
149 }
150
151 pub fn was_previous_line_empty(&self) -> bool {
153 self.last_line_was_empty
154 }
155
156 pub(super) fn message_kind(style: MessageStyle) -> InlineMessageKind {
157 style.message_kind()
158 }
159
160 pub fn supports_streaming_markdown(&self) -> bool {
161 self.sink.is_some()
162 }
163
164 pub fn prefers_untruncated_output(&self) -> bool {
169 self.sink.is_some()
170 }
171
172 pub fn supports_inline_ui(&self) -> bool {
173 self.sink.is_some()
174 }
175
176 pub fn set_reasoning_visible(&mut self, visible: bool) {
177 self.reasoning_visible = visible;
178 }
179
180 pub fn reasoning_visible(&self) -> bool {
181 self.reasoning_visible
182 }
183
184 pub fn writes_to_inline_sink(&self) -> bool {
187 self.sink.is_some()
188 }
189
190 pub fn set_screen_reader_mode(&mut self, enabled: bool) {
191 self.screen_reader_mode = enabled;
192 }
193
194 pub fn set_show_diagnostics_in_transcript(&mut self, enabled: bool) {
195 self.show_diagnostics_in_transcript = if cfg!(debug_assertions) { enabled } else { false };
196 }
197
198 pub fn set_tool_display_mode(&mut self, mode: ToolDisplayMode) {
199 self.flush_compact_command_group();
200 self.tool_display_mode = match mode {
201 ToolDisplayMode::Compact => ToolDisplayMode::Compact,
202 ToolDisplayMode::Expanded | ToolDisplayMode::Unknown => ToolDisplayMode::Expanded,
203 };
204 }
205
206 pub fn tool_display_mode(&self) -> ToolDisplayMode {
207 self.tool_display_mode
208 }
209
210 pub fn is_compact_display(&self) -> bool {
216 self.tool_display_mode == ToolDisplayMode::Compact
217 }
218
219 pub fn set_diff_preview_mode(&mut self, mode: vtcode_commons::ui_protocol::DiffPreviewMode) {
220 self.diff_preview_mode = mode;
221 }
222
223 pub fn record_diff_review(&self, anchor: vtcode_commons::ui_protocol::DiffReviewAnchor) {
225 if let Some(sink) = &self.sink {
226 sink.handle.record_diff_review(anchor);
227 }
228 }
229
230 pub fn diff_preview_mode(&self) -> vtcode_commons::ui_protocol::DiffPreviewMode {
231 self.diff_preview_mode
232 }
233
234 pub fn toggle_tool_display_mode(&mut self) -> ToolDisplayMode {
235 self.flush_compact_command_group();
236 let next = match self.tool_display_mode {
237 ToolDisplayMode::Expanded | ToolDisplayMode::Unknown => ToolDisplayMode::Compact,
238 ToolDisplayMode::Compact => ToolDisplayMode::Expanded,
239 };
240 self.tool_display_mode = next;
241 next
242 }
243}