Skip to main content

vtcode_commons/
styling.rs

1//! Unified message styles and their logical mappings
2
3use crate::ansi_codes::RESET;
4use crate::color_policy;
5use anstyle::{AnsiColor, Color, Effects, Style};
6
7/// Standard color palette with semantic names
8#[derive(Debug, Clone, Copy)]
9pub struct ColorPalette {
10    pub success: Color, // Green
11    pub error: Color,   // Red
12    pub warning: Color, // Yellow
13    pub info: Color,    // Cyan
14    pub accent: Color,  // Magenta
15    pub muted: Color,   // Gray/Dim
16}
17
18impl Default for ColorPalette {
19    fn default() -> Self {
20        Self {
21            success: Color::Ansi(AnsiColor::Green),
22            error: Color::Ansi(AnsiColor::Red),
23            warning: Color::Ansi(AnsiColor::Yellow),
24            info: Color::Ansi(AnsiColor::Cyan),
25            accent: Color::Ansi(AnsiColor::Magenta),
26            muted: Color::Ansi(AnsiColor::BrightBlack),
27        }
28    }
29}
30
31/// Render text with a single color and optional effects
32pub fn render_styled(text: &str, color: Color, effects: Option<String>) -> String {
33    let mut style = Style::new();
34    if color_policy::color_output_enabled() {
35        style = style.fg_color(Some(color));
36    }
37
38    if let Some(effects_str) = effects {
39        let mut ansi_effects = Effects::new();
40
41        for effect in effects_str.split(',') {
42            let effect = effect.trim().to_lowercase();
43            match effect.as_str() {
44                "bold" => ansi_effects |= Effects::BOLD,
45                "dim" | "dimmed" => ansi_effects |= Effects::DIMMED,
46                "italic" => ansi_effects |= Effects::ITALIC,
47                "underline" => ansi_effects |= Effects::UNDERLINE,
48                "blink" => ansi_effects |= Effects::BLINK,
49                "invert" | "reversed" => ansi_effects |= Effects::INVERT,
50                "hidden" => ansi_effects |= Effects::HIDDEN,
51                "strikethrough" => ansi_effects |= Effects::STRIKETHROUGH,
52                _ => {}
53            }
54        }
55
56        style = style.effects(ansi_effects);
57    }
58
59    let prefix = style.to_string();
60    if prefix.is_empty() {
61        text.to_string()
62    } else {
63        format!("{prefix}{text}{RESET}")
64    }
65}
66
67/// Build style from CSS/terminal color name
68pub fn style_from_color_name(name: &str) -> Style {
69    let (color_name, dimmed) = if let Some(idx) = name.find(':') {
70        let (color, modifier) = name.split_at(idx);
71        (color, modifier.strip_prefix(':').unwrap_or(""))
72    } else {
73        (name, "")
74    };
75
76    let color = match color_name.to_lowercase().as_str() {
77        "red" => Color::Ansi(AnsiColor::Red),
78        "green" => Color::Ansi(AnsiColor::Green),
79        "blue" => Color::Ansi(AnsiColor::Blue),
80        "yellow" => Color::Ansi(AnsiColor::Yellow),
81        "cyan" => Color::Ansi(AnsiColor::Cyan),
82        "magenta" | "purple" => Color::Ansi(AnsiColor::Magenta),
83        "white" => Color::Ansi(AnsiColor::White),
84        "black" => Color::Ansi(AnsiColor::Black),
85        _ => return Style::new(),
86    };
87
88    let mut style = Style::new().fg_color(Some(color));
89    if dimmed.eq_ignore_ascii_case("dimmed") {
90        style = style.dimmed();
91    }
92    style
93}
94
95/// Create a bold colored style from AnsiColor
96pub fn bold_color(color: AnsiColor) -> Style {
97    Style::new().bold().fg_color(Some(Color::Ansi(color)))
98}
99
100/// Create a dimmed colored style from AnsiColor
101pub fn dimmed_color(color: AnsiColor) -> Style {
102    Style::new().dimmed().fg_color(Some(Color::Ansi(color)))
103}
104
105/// Diff color palette for consistent git diff styling
106/// Uses standard ANSI colors without bold for accessibility and consistency.
107#[derive(Debug, Clone, Copy)]
108pub struct DiffColorPalette {
109    pub added_fg: Color,
110    pub removed_fg: Color,
111    pub header_fg: Color,
112}
113
114impl Default for DiffColorPalette {
115    fn default() -> Self {
116        Self {
117            added_fg: Color::Ansi(AnsiColor::Green),
118            removed_fg: Color::Ansi(AnsiColor::Red),
119            header_fg: Color::Ansi(AnsiColor::Cyan),
120        }
121    }
122}
123
124impl DiffColorPalette {
125    pub fn added_style(&self) -> Style {
126        Style::new().fg_color(Some(self.added_fg))
127    }
128
129    pub fn removed_style(&self) -> Style {
130        Style::new().fg_color(Some(self.removed_fg))
131    }
132
133    pub fn header_style(&self) -> Style {
134        Style::new().fg_color(Some(self.header_fg))
135    }
136}
137
138// Re-export diff theme configuration
139pub use crate::diff_theme::{
140    DiffColorLevel, DiffTheme, diff_add_bg, diff_add_word_bg, diff_del_bg, diff_del_word_bg, diff_gutter_bg_add_light,
141    diff_gutter_bg_del_light, diff_gutter_fg_light, diff_hunk_bg,
142};
143
144/// Style presets for consistent UI theming.
145///
146/// Provides convenience constructors for common ANSI styles used across
147/// VT Code's terminal output.
148pub struct Styles;
149
150impl Styles {
151    /// Error message style (red)
152    pub fn error() -> Style {
153        Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red)))
154    }
155
156    /// Warning message style (yellow)
157    pub fn warning() -> Style {
158        Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow)))
159    }
160
161    /// Success message style (green)
162    pub fn success() -> Style {
163        Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)))
164    }
165
166    /// Info message style (cyan)
167    pub fn info() -> Style {
168        Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)))
169    }
170
171    /// Debug message style (cyan, dimmed)
172    pub fn debug() -> Style {
173        Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan))).dimmed()
174    }
175
176    /// Bold text style
177    pub fn bold() -> Style {
178        Style::new().effects(Effects::BOLD)
179    }
180
181    /// Bold error style
182    pub fn bold_error() -> Style {
183        Self::error().bold()
184    }
185
186    /// Bold success style
187    pub fn bold_success() -> Style {
188        Self::success().bold()
189    }
190
191    /// Bold warning style
192    pub fn bold_warning() -> Style {
193        Self::warning().bold()
194    }
195
196    /// Header style (bold)
197    pub fn header() -> Style {
198        Style::new().effects(Effects::BOLD)
199    }
200
201    /// Code style (magenta)
202    pub fn code() -> Style {
203        Style::new().fg_color(Some(Color::Ansi(AnsiColor::Magenta)))
204    }
205
206    /// Render style to ANSI string
207    pub fn render(style: &Style) -> String {
208        style.to_string()
209    }
210
211    /// Render reset ANSI string
212    pub fn render_reset() -> String {
213        anstyle::Reset.to_string()
214    }
215}