| 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::Fixed(n) if n < 16 => ansi_16_color_number_to_name(n).unwrap().to_string(), |
| 45 |
Color::Fixed(n) => format!("{n}"), |
| 46 |
Color::RGB(r, g, b) => format!("\"#{r:02x?}{g:02x?}{b:02x?}\""), |
| 47 |
Color::Black => "black".to_string(), |
| 48 |
Color::Red => "red".to_string(), |
| 49 |
Color::Green => "green".to_string(), |
| 50 |
Color::Yellow => "yellow".to_string(), |
| 51 |
Color::Blue => "blue".to_string(), |
| 52 |
Color::Purple => "purple".to_string(), |
| 53 |
Color::Cyan => "cyan".to_string(), |
| 54 |
Color::White => "white".to_string(), |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// See |
| 59 |
// https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit |
| 60 |
lazy_static! { |
| 61 |
static ref ANSI_16_COLORS: HashMap<&'static str, u8> = { |
| 62 |
vec![ |
| 63 |
("black", 0), |
| 64 |
("red", 1), |
| 65 |
("green", 2), |
| 66 |
("yellow", 3), |
| 67 |
("blue", 4), |
| 68 |
("magenta", 5), |
| 69 |
("purple", 5), |
| 70 |
("cyan", 6), |
| 71 |
("white", 7), |
| 72 |
("bright-black", 8), |
| 73 |
("brightblack", 8), |
| 74 |
("bright-red", 9), |
| 75 |
("brightred", 9), |
| 76 |
("bright-green", 10), |
| 77 |
("brightgreen", 10), |
| 78 |
("bright-yellow", 11), |
| 79 |
("brightyellow", 11), |
| 80 |
("bright-blue", 12), |
| 81 |
("brightblue", 12), |
| 82 |
("bright-magenta", 13), |
| 83 |
("brightmagenta", 13), |
| 84 |
("bright-purple", 13), |
| 85 |
("brightpurple", 13), |
| 86 |
("bright-cyan", 14), |
| 87 |
("brightcyan", 14), |
| 88 |
("bright-white", 15), |
| 89 |
("brightwhite", 15), |
| 90 |
] |
| 91 |
.into_iter() |
| 92 |
.collect() |
| 93 |
}; |
| 94 |
} |
| 95 |
|
| 96 |
pub fn ansi_16_color_name_to_number(name: &str) -> Option<u8> { |
| 97 |
ANSI_16_COLORS.get(name).copied() |
| 98 |
} |
| 99 |
|
| 100 |
fn ansi_16_color_number_to_name(n: u8) -> Option<&'static str> { |
| 101 |
for (k, _n) in &*ANSI_16_COLORS { |
| 102 |
if *_n == n { |
| 103 |
return Some(*k); |
| 104 |
} |
| 105 |
} |
| 106 |
None |
| 107 |
} |
| 108 |
|
| 109 |
/// The color mode determines some default color choices |
| 110 |
/// such as the diff background color or the palette used for blame. |
| 111 |
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)] |
| 112 |
pub enum ColorMode { |
| 113 |
#[default] |
| 114 |
/// Dark background with light text. |
| 115 |
Dark, |
| 116 |
/// Light background with dark text. |
| 117 |
Light, |
| 118 |
} |
| 119 |
|
| 120 |
pub fn get_minus_background_color_default(mode: ColorMode, is_true_color: bool) -> Color { |
| 121 |
match (mode, is_true_color) { |
| 122 |
(Light, true) => LIGHT_THEME_MINUS_COLOR, |
| 123 |
(Light, false) => LIGHT_THEME_MINUS_COLOR_256, |
| 124 |
(Dark, true) => DARK_THEME_MINUS_COLOR, |
| 125 |
(Dark, false) => DARK_THEME_MINUS_COLOR_256, |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
pub fn get_minus_emph_background_color_default(mode: ColorMode, is_true_color: bool) -> Color { |
| 130 |
match (mode, is_true_color) { |
| 131 |
(Light, true) => LIGHT_THEME_MINUS_EMPH_COLOR, |
| 132 |
(Light, false) => LIGHT_THEME_MINUS_EMPH_COLOR_256, |
| 133 |
(Dark, true) => DARK_THEME_MINUS_EMPH_COLOR, |
| 134 |
(Dark, false) => DARK_THEME_MINUS_EMPH_COLOR_256, |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
pub fn get_plus_background_color_default(mode: ColorMode, is_true_color: bool) -> Color { |
| 139 |
match (mode, is_true_color) { |
| 140 |
(Light, true) => LIGHT_THEME_PLUS_COLOR, |
| 141 |
(Light, false) => LIGHT_THEME_PLUS_COLOR_256, |
| 142 |
(Dark, true) => DARK_THEME_PLUS_COLOR, |
| 143 |
(Dark, false) => DARK_THEME_PLUS_COLOR_256, |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
pub fn get_plus_emph_background_color_default(mode: ColorMode, is_true_color: bool) -> Color { |
| 148 |
match (mode, is_true_color) { |
| 149 |
(Light, true) => LIGHT_THEME_PLUS_EMPH_COLOR, |
| 150 |
(Light, false) => LIGHT_THEME_PLUS_EMPH_COLOR_256, |
| 151 |
(Dark, true) => DARK_THEME_PLUS_EMPH_COLOR, |
| 152 |
(Dark, false) => DARK_THEME_PLUS_EMPH_COLOR_256, |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
const LIGHT_THEME_MINUS_COLOR: Color = Color::RGB(0xff, 0xe0, 0xe0); |
| 157 |
|
| 158 |
const LIGHT_THEME_MINUS_COLOR_256: Color = Color::Fixed(224); |
| 159 |
|
| 160 |
const LIGHT_THEME_MINUS_EMPH_COLOR: Color = Color::RGB(0xff, 0xc0, 0xc0); |
| 161 |
|
| 162 |
const LIGHT_THEME_MINUS_EMPH_COLOR_256: Color = Color::Fixed(217); |
| 163 |
|
| 164 |
const LIGHT_THEME_PLUS_COLOR: Color = Color::RGB(0xd0, 0xff, 0xd0); |
| 165 |
|
| 166 |
const LIGHT_THEME_PLUS_COLOR_256: Color = Color::Fixed(194); |
| 167 |
|
| 168 |
const LIGHT_THEME_PLUS_EMPH_COLOR: Color = Color::RGB(0xa0, 0xef, 0xa0); |
| 169 |
|
| 170 |
const LIGHT_THEME_PLUS_EMPH_COLOR_256: Color = Color::Fixed(157); |
| 171 |
|
| 172 |
const DARK_THEME_MINUS_COLOR: Color = Color::RGB(0x3f, 0x00, 0x01); |
| 173 |
|
| 174 |
const DARK_THEME_MINUS_COLOR_256: Color = Color::Fixed(52); |
| 175 |
|
| 176 |
const DARK_THEME_MINUS_EMPH_COLOR: Color = Color::RGB(0x90, 0x10, 0x11); |
| 177 |
|
| 178 |
const DARK_THEME_MINUS_EMPH_COLOR_256: Color = Color::Fixed(124); |
| 179 |
|
| 180 |
const DARK_THEME_PLUS_COLOR: Color = Color::RGB(0x00, 0x28, 0x00); |
| 181 |
|
| 182 |
const DARK_THEME_PLUS_COLOR_256: Color = Color::Fixed(22); |
| 183 |
|
| 184 |
const DARK_THEME_PLUS_EMPH_COLOR: Color = Color::RGB(0x00, 0x60, 0x00); |
| 185 |
|
| 186 |
const DARK_THEME_PLUS_EMPH_COLOR_256: Color = Color::Fixed(28); |
| 187 |
|
| 188 |
// blame |
| 189 |
|
| 190 |
pub const LIGHT_THEME_BLAME_PALETTE: &[&str] = &["#FFFFFF", "#DDDDDD", "#BBBBBB"]; |
| 191 |
|
| 192 |
pub const DARK_THEME_BLAME_PALETTE: &[&str] = &["#000000", "#222222", "#444444"]; |