Skip to main content

vtcode_tui/core_tui/session/
styling.rs

1use 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
39/// Get the inline style for a tool based on its normalized name.
40/// Shared by both `SessionStyles` and standalone rendering contexts.
41pub 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
58/// Styling utilities for the Session UI
59pub 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    /// Get the modal list highlight style
78    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    /// Get the inline style for a tool based on its name
87    #[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    /// Get the tool border style
93    pub fn tool_border_style(&self) -> InlineTextStyle {
94        self.border_inline_style()
95    }
96
97    /// Get the default style
98    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    /// Get the default inline style (for tests and inline conversions)
107    #[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    /// Get the accent inline style
116    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    /// Get the accent style
124    pub fn accent_style(&self) -> Style {
125        ratatui_style_from_inline(&self.accent_inline_style(), self.theme.foreground)
126    }
127
128    /// Get the border inline style
129    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    /// Get the border style (dimmed)
137    pub fn border_style(&self) -> Style {
138        self.dimmed_border_style(true)
139    }
140
141    /// Get a border style with configurable boldness.
142    /// When `suppress_bold` is true, the BOLD modifier is removed — useful for
143    /// info/error/warning block borders that should appear subtle.
144    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    /// Get the prefix style for a message line
172    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    /// Get the fallback text color for a message kind
188    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    /// Get the message divider style
201    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}