Skip to main content

vtcode_ui/tui/core_tui/
style.rs

1use crate::tui::core_tui::session::text_utils::display_width;
2use anstyle::{Color as AnsiColorEnum, Style as AnsiStyle};
3use ratatui::style::{Color, Modifier, Style};
4use unicode_width::UnicodeWidthStr;
5
6use crate::tui::ui::theme;
7
8// Re-export from commons so existing consumers don't break.
9pub use vtcode_commons::ui_protocol::{convert_style, theme_from_color_fields};
10
11use super::types::{InlineTextStyle, InlineTheme};
12
13pub fn theme_from_styles(styles: &theme::ThemeStyles) -> InlineTheme {
14    theme_from_color_fields(
15        styles.foreground,
16        styles.background,
17        styles.primary,
18        styles.secondary,
19        styles.tool,
20        styles.tool_detail,
21        styles.pty_output,
22        styles.error,
23        styles.warning,
24    )
25}
26
27pub(crate) fn measure_text_width(text: &str) -> u16 {
28    display_width(text) as u16
29}
30
31/// Convert anstyle Color to ratatui Color.
32///
33/// Delegates to `crate::design::color::anstyle_to_ratatui_color` which
34/// provides the correct mapping (fixing the Magenta bug).
35pub(crate) fn ratatui_color_from_ansi(color: AnsiColorEnum) -> Color {
36    crate::design::color::anstyle_to_ratatui_color(color)
37}
38
39/// Parse a hex color string (e.g., "#D99A4E") to a ratatui Color.
40/// Returns None if the string is invalid or cannot be parsed.
41pub(crate) use crate::design::color::hex_to_ratatui_color;
42
43/// Get the agent color style from an optional color token.
44///
45/// The token may be a primary-agent mode name (`"build"`), a standard ANSI hue
46/// name (`"green"`), or a `#rrggbb` hex string. It is resolved theme-aware via
47/// the design system so the badge stays legible on both dark and light
48/// terminals, with `fallback_color` used when the token is empty or unknown.
49pub(crate) fn agent_color_style(color: Option<&str>, fallback_color: Color) -> Style {
50    let light = matches!(
51        vtcode_commons::ansi_capabilities::detect_color_scheme(),
52        vtcode_commons::ansi_capabilities::ColorScheme::Light
53    );
54    let color = color
55        .map(|c| crate::design::color::resolve_agent_color(c, fallback_color, light))
56        .unwrap_or(fallback_color);
57    Style::default().fg(color).add_modifier(Modifier::BOLD)
58}
59
60pub(crate) fn ratatui_style_from_inline(style: &InlineTextStyle, fallback: Option<AnsiColorEnum>) -> Style {
61    crate::design::style::inline_text_style_to_ratatui(style.color, style.bg_color, style.effects, fallback)
62}
63
64/// PTY output style helper: keep configured colors and suppress bold.
65pub(crate) fn ratatui_pty_style_from_inline(style: &InlineTextStyle, fallback: Option<AnsiColorEnum>) -> Style {
66    ratatui_style_from_inline(style, fallback).remove_modifier(Modifier::BOLD)
67}
68
69/// Convert an `anstyle::Style` directly to a `ratatui::style::Style`.
70pub(crate) fn ratatui_style_from_ansi(style: AnsiStyle) -> Style {
71    crate::design::style::anstyle_to_ratatui_style(style)
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use crate::design::color::resolve_agent_color;
78    use vtcode_config::constants::ui::{AGENT_COLOR_AUTO, AGENT_COLOR_BUILD, AGENT_COLOR_DUCK, AGENT_COLOR_PLAN};
79
80    #[test]
81    fn agent_color_style_applies_mode_color_with_bold() {
82        let fallback = Color::LightMagenta;
83        let style = agent_color_style(Some(AGENT_COLOR_BUILD), fallback);
84        // The exact variant depends on the detected terminal scheme, but it must
85        // be a concrete (non-fallback) standard color and always bold.
86        assert_ne!(style.fg, Some(fallback));
87        assert!(style.add_modifier.contains(Modifier::BOLD));
88
89        for hue in [AGENT_COLOR_BUILD, AGENT_COLOR_AUTO, AGENT_COLOR_PLAN, AGENT_COLOR_DUCK] {
90            let s = agent_color_style(Some(hue), fallback);
91            assert!(s.add_modifier.contains(Modifier::BOLD));
92            assert!(s.fg.is_some());
93        }
94    }
95
96    #[test]
97    fn agent_color_style_is_theme_aware_and_distinct_per_mode() {
98        let fallback = Color::LightMagenta;
99        // Build is a fixed hex token: identical on both appearances.
100        let build = Color::Rgb(0x73, 0xA1, 0x8E);
101        // On a dark terminal each hue resolves to its bright variant.
102        let dark = [
103            resolve_agent_color(AGENT_COLOR_BUILD, fallback, false),
104            resolve_agent_color(AGENT_COLOR_AUTO, fallback, false),
105            resolve_agent_color(AGENT_COLOR_PLAN, fallback, false),
106            resolve_agent_color(AGENT_COLOR_DUCK, fallback, false),
107        ];
108        assert_eq!(dark, [build, Color::LightGreen, Color::LightBlue, Color::LightMagenta]);
109        // On a light terminal each hue resolves to its base variant.
110        let light = [
111            resolve_agent_color(AGENT_COLOR_BUILD, fallback, true),
112            resolve_agent_color(AGENT_COLOR_AUTO, fallback, true),
113            resolve_agent_color(AGENT_COLOR_PLAN, fallback, true),
114            resolve_agent_color(AGENT_COLOR_DUCK, fallback, true),
115        ];
116        assert_eq!(light, [build, Color::Green, Color::Blue, Color::Magenta]);
117        // The four modes must remain visually distinct in both appearances.
118        assert_eq!(dark.iter().collect::<std::collections::HashSet<_>>().len(), 4);
119        assert_eq!(light.iter().collect::<std::collections::HashSet<_>>().len(), 4);
120    }
121
122    #[test]
123    fn agent_color_style_accepts_raw_hue_names_and_hex() {
124        let fallback = Color::LightMagenta;
125        // Raw standard ANSI hue name (as emitted by the plan-approval overlay).
126        assert_eq!(resolve_agent_color("green", fallback, false), Color::LightGreen);
127        assert_eq!(resolve_agent_color("blue", fallback, true), Color::Blue);
128        // Legacy hex still resolves.
129        assert_eq!(resolve_agent_color("#FF0000", fallback, false), Color::Rgb(255, 0, 0));
130    }
131
132    #[test]
133    fn agent_color_style_falls_back_when_missing_or_invalid() {
134        let fallback = Color::LightMagenta;
135        let missing = agent_color_style(None, fallback);
136        assert_eq!(missing.fg, Some(fallback));
137        assert!(missing.add_modifier.contains(Modifier::BOLD));
138
139        let invalid = agent_color_style(Some("not-a-color"), fallback);
140        assert_eq!(invalid.fg, Some(fallback));
141    }
142}