Debian Codegraph @

src/color.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use std::collections::HashMap;
2 use std::str::FromStr;
3
4 use ansi_term::Color;
5 use lazy_static::lazy_static;
6 use syntect::highlighting::Color as SyntectColor;
7
8 use crate::fatal;
9 use crate::git_config::GitConfig;
10 use crate::utils;
11 use ColorMode::*;
12
13 pub fn parse_color(s: &str, true_color: bool, git_config: Option<&GitConfig>) -> Option<Color> {
14 if s == "normal" {
15 return None;
16 }
17 let die = || {
18 fatal(format!("Invalid color or style attribute: {s}"));
19 };
20 let syntect_color = if s.starts_with('#') {
21 SyntectColor::from_str(s).unwrap_or_else(|_| die())
22 } else {
23 let syntect_color = s
24 .parse::<u8>()
25 .ok()
26 .and_then(utils::syntect::syntect_color_from_ansi_number)
27 .or_else(|| utils::syntect::syntect_color_from_ansi_name(s))
28 .or_else(|| utils::syntect::syntect_color_from_name(s));
29 if syntect_color.is_none() {
30 if let Some(git_config) = git_config {
31 if let Some(val) = git_config.get::<String>(&format!("delta.{s}")) {
32 return parse_color(&val, true_color, None);
33 }
34 }
35 die();
36 }
37 syntect_color.unwrap()
38 };
39 utils::bat::terminal::to_ansi_color(syntect_color, true_color)
40 }
41
42 pub fn color_to_string(color: Color) -> String {
43 match color {
•44 Color::Default => todo!(),
45 Color::Fixed(n) if n < 16 => ansi_16_color_number_to_name(n).unwrap().to_string(),
46 Color::Fixed(n) => format!("{n}"),
•47 Color::Rgb(r, g, b) => format!("\"#{r:02x?}{g:02x?}{b:02x?}\""),
48 Color::Black => "black".to_string(),
•49 Color::DarkGray => "bright-black".to_string(),
50 Color::Red => "red".to_string(),
•51 Color::LightRed => "bright-red".to_string(),
52 Color::Green => "green".to_string(),
•53 Color::LightGreen => "bright-green".to_string(),
54 Color::Yellow => "yellow".to_string(),
•55 Color::LightYellow => "bright-yellow".to_string(),
56 Color::Blue => "blue".to_string(),
•57 Color::LightBlue => "bright-blue".to_string(),
58 Color::Purple => "purple".to_string(),
•59 Color::LightPurple => "bright-purple".to_string(),
60 Color::Magenta => "magenta".to_string(),
61 Color::LightMagenta => "bright-magenta".to_string(),
62 Color::Cyan => "cyan".to_string(),
•63 Color::LightCyan => "bright-cyan".to_string(),
64 Color::White => "white".to_string(),
•65 Color::LightGray => "bright-white".to_string(),
66 }
67 }
68
69 // See
70 // https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
71 lazy_static! {
72 static ref ANSI_16_COLORS: HashMap<&'static str, u8> = {
73 vec![
74 ("black", 0),
75 ("red", 1),
76 ("green", 2),
77 ("yellow", 3),
78 ("blue", 4),
79 ("magenta", 5),
80 ("purple", 5),
81 ("cyan", 6),
82 ("white", 7),
83 ("bright-black", 8),
84 ("brightblack", 8),
85 ("bright-red", 9),
86 ("brightred", 9),
87 ("bright-green", 10),
88 ("brightgreen", 10),
89 ("bright-yellow", 11),
90 ("brightyellow", 11),
91 ("bright-blue", 12),
92 ("brightblue", 12),
93 ("bright-magenta", 13),
94 ("brightmagenta", 13),
95 ("bright-purple", 13),
96 ("brightpurple", 13),
97 ("bright-cyan", 14),
98 ("brightcyan", 14),
99 ("bright-white", 15),
100 ("brightwhite", 15),
101 ]
102 .into_iter()
103 .collect()
104 };
105 }
106
107 pub fn ansi_16_color_name_to_number(name: &str) -> Option<u8> {
108 ANSI_16_COLORS.get(name).copied()
109 }
110
111 fn ansi_16_color_number_to_name(n: u8) -> Option<&'static str> {
112 for (k, _n) in &*ANSI_16_COLORS {
113 if *_n == n {
114 return Some(*k);
115 }
116 }
117 None
118 }
119
120 /// The color mode determines some default color choices
121 /// such as the diff background color or the palette used for blame.
122 #[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
123 pub enum ColorMode {
124 #[default]
125 /// Dark background with light text.
126 Dark,
127 /// Light background with dark text.
128 Light,
129 }
130
131 pub fn get_minus_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
132 match (mode, is_true_color) {
133 (Light, true) => LIGHT_THEME_MINUS_COLOR,
134 (Light, false) => LIGHT_THEME_MINUS_COLOR_256,
135 (Dark, true) => DARK_THEME_MINUS_COLOR,
136 (Dark, false) => DARK_THEME_MINUS_COLOR_256,
137 }
138 }
139
140 pub fn get_minus_emph_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
141 match (mode, is_true_color) {
142 (Light, true) => LIGHT_THEME_MINUS_EMPH_COLOR,
143 (Light, false) => LIGHT_THEME_MINUS_EMPH_COLOR_256,
144 (Dark, true) => DARK_THEME_MINUS_EMPH_COLOR,
145 (Dark, false) => DARK_THEME_MINUS_EMPH_COLOR_256,
146 }
147 }
148
149 pub fn get_plus_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
150 match (mode, is_true_color) {
151 (Light, true) => LIGHT_THEME_PLUS_COLOR,
152 (Light, false) => LIGHT_THEME_PLUS_COLOR_256,
153 (Dark, true) => DARK_THEME_PLUS_COLOR,
154 (Dark, false) => DARK_THEME_PLUS_COLOR_256,
155 }
156 }
157
158 pub fn get_plus_emph_background_color_default(mode: ColorMode, is_true_color: bool) -> Color {
159 match (mode, is_true_color) {
160 (Light, true) => LIGHT_THEME_PLUS_EMPH_COLOR,
161 (Light, false) => LIGHT_THEME_PLUS_EMPH_COLOR_256,
162 (Dark, true) => DARK_THEME_PLUS_EMPH_COLOR,
163 (Dark, false) => DARK_THEME_PLUS_EMPH_COLOR_256,
164 }
165 }
166
•167 const LIGHT_THEME_MINUS_COLOR: Color = Color::Rgb(0xff, 0xe0, 0xe0);
168
169 const LIGHT_THEME_MINUS_COLOR_256: Color = Color::Fixed(224);
170
•171 const LIGHT_THEME_MINUS_EMPH_COLOR: Color = Color::Rgb(0xff, 0xc0, 0xc0);
172
173 const LIGHT_THEME_MINUS_EMPH_COLOR_256: Color = Color::Fixed(217);
174
•175 const LIGHT_THEME_PLUS_COLOR: Color = Color::Rgb(0xd0, 0xff, 0xd0);
176
177 const LIGHT_THEME_PLUS_COLOR_256: Color = Color::Fixed(194);
178
•179 const LIGHT_THEME_PLUS_EMPH_COLOR: Color = Color::Rgb(0xa0, 0xef, 0xa0);
180
181 const LIGHT_THEME_PLUS_EMPH_COLOR_256: Color = Color::Fixed(157);
182
•183 const DARK_THEME_MINUS_COLOR: Color = Color::Rgb(0x3f, 0x00, 0x01);
184
185 const DARK_THEME_MINUS_COLOR_256: Color = Color::Fixed(52);
186
•187 const DARK_THEME_MINUS_EMPH_COLOR: Color = Color::Rgb(0x90, 0x10, 0x11);
188
189 const DARK_THEME_MINUS_EMPH_COLOR_256: Color = Color::Fixed(124);
190
•191 const DARK_THEME_PLUS_COLOR: Color = Color::Rgb(0x00, 0x28, 0x00);
192
193 const DARK_THEME_PLUS_COLOR_256: Color = Color::Fixed(22);
194
•195 const DARK_THEME_PLUS_EMPH_COLOR: Color = Color::Rgb(0x00, 0x60, 0x00);
196
197 const DARK_THEME_PLUS_EMPH_COLOR_256: Color = Color::Fixed(28);
198
199 // blame
200
201 pub const LIGHT_THEME_BLAME_PALETTE: &[&str] = &["#FFFFFF", "#DDDDDD", "#BBBBBB"];
202
203 pub const DARK_THEME_BLAME_PALETTE: &[&str] = &["#000000", "#222222", "#444444"];