vtcode_tui/core_tui/session/
styling.rs1use anstyle::{AnsiColor, Color as AnsiColorEnum, RgbColor};
2use ratatui::prelude::*;
3
4use crate::config::constants::ui;
5use crate::ui::tui::{
6 style::{ratatui_color_from_ansi, ratatui_style_from_inline},
7 types::{InlineMessageKind, InlineTextStyle, InlineTheme},
8};
9
10use super::message::MessageLine;
11
12fn mix(color: RgbColor, target: RgbColor, ratio: f64) -> RgbColor {
13 let ratio = ratio.clamp(ui::THEME_MIX_RATIO_MIN, ui::THEME_MIX_RATIO_MAX);
14 let blend = |c: u8, t: u8| -> u8 {
15 let c = c as f64;
16 let t = t as f64;
17 ((c + (t - c) * ratio).round()).clamp(ui::THEME_BLEND_CLAMP_MIN, ui::THEME_BLEND_CLAMP_MAX)
18 as u8
19 };
20
21 RgbColor(
22 blend(color.0, target.0),
23 blend(color.1, target.1),
24 blend(color.2, target.2),
25 )
26}
27
28pub fn normalize_tool_name(tool_name: &str) -> &'static str {
29 match tool_name.to_lowercase().as_str() {
30 "grep" | "rg" | "ripgrep" | "grep_file" | "search" | "find" | "ag" => "search",
31 "list" | "ls" | "dir" | "list_files" => "list",
32 "read" | "cat" | "file" | "read_file" => "read",
33 "write" | "edit" | "save" | "insert" | "edit_file" => "write",
34 "run" | "command" | "bash" | "sh" => "run",
35 _ => "other",
36 }
37}
38
39pub fn tool_inline_style_for(tool_name: &str, theme: &InlineTheme) -> InlineTextStyle {
42 let normalized_name = normalize_tool_name(tool_name);
43 let mut style = InlineTextStyle::default().bold();
44
45 style.color = match normalized_name {
46 "read" => Some(AnsiColor::Cyan.into()),
47 "list" => Some(AnsiColor::Green.into()),
48 "search" => Some(AnsiColor::Cyan.into()),
49 "write" => Some(AnsiColor::Magenta.into()),
50 "run" => Some(AnsiColor::Red.into()),
51 "git" | "version_control" => Some(AnsiColor::Cyan.into()),
52 _ => theme.tool_accent.or(theme.primary).or(theme.foreground),
53 };
54
55 style
56}
57
58pub struct SessionStyles {
60 theme: InlineTheme,
61}
62
63impl SessionStyles {
64 pub fn new(theme: InlineTheme) -> Self {
65 Self { theme }
66 }
67
68 #[allow(dead_code)]
69 pub fn theme(&self) -> &InlineTheme {
70 &self.theme
71 }
72
73 pub fn set_theme(&mut self, theme: InlineTheme) {
74 self.theme = theme;
75 }
76
77 pub fn modal_list_highlight_style(&self) -> Style {
79 let mut style = Style::default().add_modifier(Modifier::REVERSED | Modifier::BOLD);
80 if let Some(primary) = self.theme.primary.or(self.theme.foreground) {
81 style = style.fg(ratatui_color_from_ansi(primary));
82 }
83 style
84 }
85
86 #[allow(dead_code)]
88 pub fn tool_inline_style(&self, tool_name: &str) -> InlineTextStyle {
89 tool_inline_style_for(tool_name, &self.theme)
90 }
91
92 pub fn tool_border_style(&self) -> InlineTextStyle {
94 self.border_inline_style()
95 }
96
97 pub fn default_style(&self) -> Style {
99 let mut style = Style::default();
100 if let Some(foreground) = self.theme.foreground.map(ratatui_color_from_ansi) {
101 style = style.fg(foreground);
102 }
103 style
104 }
105
106 #[allow(dead_code)]
108 pub fn default_inline_style(&self) -> InlineTextStyle {
109 InlineTextStyle {
110 color: self.theme.foreground,
111 ..InlineTextStyle::default()
112 }
113 }
114
115 pub fn accent_inline_style(&self) -> InlineTextStyle {
117 InlineTextStyle {
118 color: self.theme.primary.or(self.theme.foreground),
119 ..InlineTextStyle::default()
120 }
121 }
122
123 pub fn accent_style(&self) -> Style {
125 ratatui_style_from_inline(&self.accent_inline_style(), self.theme.foreground)
126 }
127
128 pub fn border_inline_style(&self) -> InlineTextStyle {
130 InlineTextStyle {
131 color: self.theme.secondary.or(self.theme.foreground),
132 ..InlineTextStyle::default()
133 }
134 }
135
136 pub fn border_style(&self) -> Style {
138 self.dimmed_border_style(true)
139 }
140
141 pub fn dimmed_border_style(&self, suppress_bold: bool) -> Style {
145 let mut style =
146 ratatui_style_from_inline(&self.border_inline_style(), self.theme.foreground)
147 .add_modifier(Modifier::DIM);
148 if suppress_bold {
149 style = style.remove_modifier(Modifier::BOLD);
150 }
151 style
152 }
153
154 pub fn input_background_style(&self) -> Style {
155 let mut style = self.default_style();
156 let Some(background) = self.theme.background else {
157 return style;
158 };
159
160 let resolved = match (background, self.theme.foreground) {
161 (AnsiColorEnum::Rgb(bg), Some(AnsiColorEnum::Rgb(fg))) => {
162 AnsiColorEnum::Rgb(mix(bg, fg, ui::THEME_INPUT_BACKGROUND_MIX_RATIO))
163 }
164 (color, _) => color,
165 };
166
167 style = style.bg(ratatui_color_from_ansi(resolved));
168 style
169 }
170
171 pub fn prefix_style(&self, line: &MessageLine) -> InlineTextStyle {
173 let fallback = self.text_fallback(line.kind).or(self.theme.foreground);
174
175 let color = line
176 .segments
177 .iter()
178 .find_map(|segment| segment.style.color)
179 .or(fallback);
180
181 InlineTextStyle {
182 color,
183 ..InlineTextStyle::default()
184 }
185 }
186
187 pub fn text_fallback(&self, kind: InlineMessageKind) -> Option<AnsiColorEnum> {
189 match kind {
190 InlineMessageKind::Agent | InlineMessageKind::Policy => Some(AnsiColor::Magenta.into()),
191 InlineMessageKind::User => self.theme.secondary.or(self.theme.foreground),
192 InlineMessageKind::Tool | InlineMessageKind::Pty | InlineMessageKind::Error => {
193 self.theme.primary.or(self.theme.foreground)
194 }
195 InlineMessageKind::Info => self.theme.foreground,
196 InlineMessageKind::Warning => Some(AnsiColor::Red.into()),
197 }
198 }
199
200 pub fn message_divider_style(&self, kind: InlineMessageKind) -> Style {
202 let mut style = InlineTextStyle::default();
203 if kind == InlineMessageKind::User {
204 style.color = self.theme.primary.or(self.theme.foreground);
205 } else {
206 style.color = self.text_fallback(kind).or(self.theme.foreground);
207 }
208 let resolved = ratatui_style_from_inline(&style, self.theme.foreground);
209 resolved.add_modifier(Modifier::DIM)
210 }
211}