Skip to main content

vtcode_commons/ui_protocol/
markdown.rs

1//! Shared markdown rendering types.
2//!
3//! The actual rendering implementation lives in `vtcode-ui`.  These structs
4//! define the output format so that `vtcode-core` can work with rendered
5//! markdown lines regardless of whether the TUI crate is compiled in.
6
7use anstyle::{Color, Style};
8
9/// A styled segment inside a rendered markdown line.
10///
11/// Keep field-compatible with the TUI variant `vtcode_ui::tui::ui::markdown::MarkdownSegment`;
12/// `vtcode-core` picks one of the two by feature and streams both through the same shapes.
13#[derive(Clone, Debug)]
14pub struct MarkdownSegment {
15    pub style: Style,
16    pub text: String,
17    pub link_target: Option<Box<str>>,
18}
19
20/// A single rendered markdown line made up of styled segments.
21///
22/// Keep field-compatible with the TUI variant `vtcode_ui::tui::ui::markdown::MarkdownLine`.
23#[derive(Clone, Debug, Default)]
24pub struct MarkdownLine {
25    pub segments: Vec<MarkdownSegment>,
26    pub line_background: Option<Color>,
27}
28
29impl MarkdownLine {
30    pub fn is_empty(&self) -> bool {
31        self.segments.iter().all(|segment| segment.text.trim().is_empty())
32    }
33}
34
35/// Visual weight for fenced code blocks.
36///
37/// `Primary` keeps the theme foreground at full intensity. `Muted` recedes the
38/// block toward the theme background so secondary surfaces (evidence bodies
39/// behind a disclosure) stay readable without competing with the summary.
40/// Muted is an explicit theme-derived color, never a faint/dim text attribute:
41/// several terminals attenuate that attribute to near-invisible and ratatui
42/// only ever *inserts* modifiers, so a dimmed area background would mute every
43/// glyph painted on top of it.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
45pub enum CodeBlockWeight {
46    /// Theme foreground at full intensity.
47    #[default]
48    Primary,
49    /// Receded toward the theme background.
50    Muted,
51}
52
53/// Options for the markdown rendering pipeline.
54///
55/// Keep field-compatible with the TUI variant `vtcode_ui::tui::ui::markdown::RenderMarkdownOptions`.
56#[derive(Debug, Clone, Copy, Default)]
57pub struct RenderMarkdownOptions {
58    pub preserve_code_indentation: bool,
59    pub disable_code_block_table_reparse: bool,
60    /// Available content width for tables. Headered tables keep a padded,
61    /// width-aware grid while cells remain readable, then fall back to aligned
62    /// labeled records when the grid becomes too cramped. Headerless tables
63    /// retain their grid layout and scale columns to fit. Only effective in
64    /// TUI mode.
65    pub table_max_width: Option<usize>,
66    /// Visual weight applied to fenced code blocks. Only effective in TUI
67    /// mode.
68    pub code_block_weight: CodeBlockWeight,
69}
70
71/// A syntax-highlighted text segment.
72///
73/// Keep field-compatible with the TUI variant `vtcode_ui::tui::ui::markdown::HighlightedSegment`.
74#[derive(Clone, Debug)]
75pub struct HighlightedSegment {
76    pub style: Style,
77    pub text: String,
78}