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 diff_preview_mode: vtcode_commons::ui_protocol::DiffPreviewMode::Inline,
58 compact_command_group: None,
59 next_compact_group_id: 0,
60 pending_tool_output_anchor: None,
61 session_expand_anchor: None,
62 session_body: false,
63 diagnostic_sink: None,
64 diagnostic_history: std::collections::VecDeque::new(),
65 }
66 }
67
68 pub fn with_inline_ui(handle: InlineHandle, highlight_config: SyntaxHighlightingConfig) -> Self {
70 let mut renderer = Self::stdout();
71 renderer.highlight_config = highlight_config.clone();
72 renderer.sink = Some(InlineSink::new(handle, highlight_config));
73 renderer.last_line_was_empty = false;
74 renderer
75 }
76
77 #[cfg(feature = "tui")]
79 pub fn program_status_wait(
80 &self,
81 kind: vtcode_commons::program_status::InteractionKind,
82 ) -> Option<vtcode_ui::tui::core_tui::types::ProgramStatusWaitGuard> {
83 self.sink.as_ref().map(|sink| sink.handle.program_status_wait(kind))
84 }
85
86 pub fn set_highlight_config(&mut self, config: SyntaxHighlightingConfig) {
88 if let Some(sink) = &mut self.sink {
89 sink.set_highlight_config(config.clone());
90 }
91 self.highlight_config = config;
92 }
93
94 pub fn begin_transcript_batch(&mut self) {
99 if let Some(sink) = &mut self.sink {
100 sink.begin_transcript_batch();
101 }
102 }
103
104 pub fn end_transcript_batch(&mut self) {
106 if let Some(sink) = &mut self.sink {
107 sink.end_transcript_batch();
108 }
109 }
110
111 pub fn transcript_batch<T>(&mut self, render: impl FnOnce(&mut Self) -> anyhow::Result<T>) -> anyhow::Result<T> {
116 self.begin_transcript_batch();
117 let result = render(self);
118 self.end_transcript_batch();
119 result
120 }
121
122 pub fn set_next_tool_output_anchor(&mut self, id: ToolOutputId) {
128 self.pending_tool_output_anchor = Some(id);
129 }
130
131 pub fn set_session_expand_anchor(&mut self, id: ToolOutputId) {
135 self.session_expand_anchor = Some(id);
136 }
137
138 pub fn take_session_expand_anchor(&mut self) -> Option<ToolOutputId> {
140 self.session_expand_anchor.take()
141 }
142
143 pub fn set_session_body(&mut self, session_body: bool) {
146 self.session_body = session_body;
147 }
148
149 pub fn session_body_active(&self) -> bool {
151 self.session_body
152 }
153
154 pub fn was_previous_line_empty(&self) -> bool {
156 self.last_line_was_empty
157 }
158
159 pub(super) fn message_kind(style: MessageStyle) -> InlineMessageKind {
160 style.message_kind()
161 }
162
163 pub fn supports_streaming_markdown(&self) -> bool {
164 self.sink.is_some()
165 }
166
167 pub fn prefers_untruncated_output(&self) -> bool {
172 self.sink.is_some()
173 }
174
175 pub fn supports_inline_ui(&self) -> bool {
176 self.sink.is_some()
177 }
178
179 pub fn set_reasoning_visible(&mut self, visible: bool) {
180 self.reasoning_visible = visible;
181 }
182
183 pub fn reasoning_visible(&self) -> bool {
184 self.reasoning_visible
185 }
186
187 pub fn writes_to_inline_sink(&self) -> bool {
190 self.sink.is_some()
191 }
192
193 pub fn set_screen_reader_mode(&mut self, enabled: bool) {
194 self.screen_reader_mode = enabled;
195 }
196
197 pub fn set_show_diagnostics_in_transcript(&mut self, enabled: bool) {
198 self.show_diagnostics_in_transcript = if cfg!(debug_assertions) { enabled } else { false };
199 }
200
201 pub fn begin_turn_message_timestamp(&mut self) {
204 self.pending_response_footer = false;
205 self.defer_turn_footer = true;
206 }
207
208 pub fn set_show_message_timestamps(&mut self, enabled: bool) {
213 self.show_message_timestamps = enabled;
214 }
215
216 pub fn show_message_timestamps(&self) -> bool {
217 self.show_message_timestamps
218 }
219
220 pub fn set_tool_display_mode(&mut self, mode: ToolDisplayMode) {
221 self.flush_compact_command_group();
222 self.tool_display_mode = match mode {
223 ToolDisplayMode::Compact => ToolDisplayMode::Compact,
224 ToolDisplayMode::Expanded | ToolDisplayMode::Unknown => ToolDisplayMode::Expanded,
225 };
226 }
227
228 pub fn tool_display_mode(&self) -> ToolDisplayMode {
229 self.tool_display_mode
230 }
231
232 pub fn is_compact_display(&self) -> bool {
238 self.tool_display_mode == ToolDisplayMode::Compact
239 }
240
241 pub fn set_diff_preview_mode(&mut self, mode: vtcode_commons::ui_protocol::DiffPreviewMode) {
242 self.diff_preview_mode = mode;
243 }
244
245 pub fn record_diff_review(&self, anchor: vtcode_commons::ui_protocol::DiffReviewAnchor) {
247 if let Some(sink) = &self.sink {
248 sink.handle.record_diff_review(anchor);
249 }
250 }
251
252 pub fn diff_preview_mode(&self) -> vtcode_commons::ui_protocol::DiffPreviewMode {
253 self.diff_preview_mode
254 }
255
256 pub fn toggle_tool_display_mode(&mut self) -> ToolDisplayMode {
257 self.flush_compact_command_group();
258 let next = match self.tool_display_mode {
259 ToolDisplayMode::Expanded | ToolDisplayMode::Unknown => ToolDisplayMode::Compact,
260 ToolDisplayMode::Compact => ToolDisplayMode::Expanded,
261 };
262 self.tool_display_mode = next;
263 next
264 }
265}