vtcode_commons/
styling.rs1use crate::ansi_codes::RESET;
4use crate::color_policy;
5use anstyle::{AnsiColor, Color, Effects, Style};
6
7#[derive(Debug, Clone, Copy)]
9pub struct ColorPalette {
10 pub success: Color, pub error: Color, pub warning: Color, pub info: Color, pub accent: Color, pub muted: Color, }
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
31pub 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
67pub 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
95pub fn bold_color(color: AnsiColor) -> Style {
97 Style::new().bold().fg_color(Some(Color::Ansi(color)))
98}
99
100pub fn dimmed_color(color: AnsiColor) -> Style {
102 Style::new().dimmed().fg_color(Some(Color::Ansi(color)))
103}
104
105#[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
138pub 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
144pub struct Styles;
149
150impl Styles {
151 pub fn error() -> Style {
153 Style::new().fg_color(Some(Color::Ansi(AnsiColor::Red)))
154 }
155
156 pub fn warning() -> Style {
158 Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow)))
159 }
160
161 pub fn success() -> Style {
163 Style::new().fg_color(Some(Color::Ansi(AnsiColor::Green)))
164 }
165
166 pub fn info() -> Style {
168 Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)))
169 }
170
171 pub fn debug() -> Style {
173 Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan))).dimmed()
174 }
175
176 pub fn bold() -> Style {
178 Style::new().effects(Effects::BOLD)
179 }
180
181 pub fn bold_error() -> Style {
183 Self::error().bold()
184 }
185
186 pub fn bold_success() -> Style {
188 Self::success().bold()
189 }
190
191 pub fn bold_warning() -> Style {
193 Self::warning().bold()
194 }
195
196 pub fn header() -> Style {
198 Style::new().effects(Effects::BOLD)
199 }
200
201 pub fn code() -> Style {
203 Style::new().fg_color(Some(Color::Ansi(AnsiColor::Magenta)))
204 }
205
206 pub fn render(style: &Style) -> String {
208 style.to_string()
209 }
210
211 pub fn render_reset() -> String {
213 anstyle::Reset.to_string()
214 }
215}