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::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    style: Style,
16    text: String,
17    link_target: Option<Box<str>>,
18}
19
20/// A single rendered markdown line made up of styled segments.
21#[derive(Clone, Debug, Default)]
22pub struct MarkdownLine {
23    segments: Vec<MarkdownSegment>,
24}
25
26impl MarkdownLine {
27    pub fn is_empty(&self) -> bool {
28        self.segments.iter().all(|segment| segment.text.trim().is_empty())
29    }
30}
31
32/// Visual weight for fenced code blocks.
33///
34/// `Primary` keeps the theme foreground at full intensity. `Muted` recedes the
35/// block toward the theme background so secondary surfaces (evidence bodies
36/// behind a disclosure) stay readable without competing with the summary.
37/// Muted is an explicit theme-derived color, never a faint/dim text attribute:
38/// several terminals attenuate that attribute to near-invisible and ratatui
39/// only ever *inserts* modifiers, so a dimmed area background would mute every
40/// glyph painted on top of it.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
42pub enum CodeBlockWeight {
43    /// Theme foreground at full intensity.
44    #[default]
45    Primary,
46    /// Receded toward the theme background.
47    Muted,
48}
49
50/// Options for the markdown rendering pipeline.
51#[derive(Debug, Clone, Copy, Default)]
52pub struct RenderMarkdownOptions {
53    preserve_code_indentation: bool,
54    disable_code_block_table_reparse: bool,
55    /// Available content width for tables. Headered tables keep a padded,
56    /// width-aware grid while cells remain readable, then fall back to aligned
57    /// labeled records when the grid becomes too cramped. Headerless tables
58    /// retain their grid layout and scale columns to fit. Only effective in
59    /// TUI mode.
60    table_max_width: Option<usize>,
61    /// Visual weight applied to fenced code blocks. Only effective in TUI
62    /// mode.
63    code_block_weight: CodeBlockWeight,
64}
65
66/// A syntax-highlighted text segment.
67#[derive(Clone, Debug)]
68pub struct HighlightedSegment {
69    style: Style,
70    text: String,
71}