vtcode_ui/tui/core_tui/
style.rs1use 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
8pub 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
31pub(crate) fn ratatui_color_from_ansi(color: AnsiColorEnum) -> Color {
36 crate::design::color::anstyle_to_ratatui_color(color)
37}
38
39pub(crate) use crate::design::color::hex_to_ratatui_color;
42
43pub(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
64pub(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
69pub(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 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 let build = Color::Rgb(0x73, 0xA1, 0x8E);
101 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 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 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 assert_eq!(resolve_agent_color("green", fallback, false), Color::LightGreen);
127 assert_eq!(resolve_agent_color("blue", fallback, true), Color::Blue);
128 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}