| 1 |
use std::str::FromStr; |
| 2 |
|
| 3 |
use syntect::highlighting::{Color, FontStyle, Style}; |
| 4 |
|
| 5 |
use crate::color; |
| 6 |
use crate::style as delta_style; |
| 7 |
|
| 8 |
pub fn syntect_color_from_ansi_name(name: &str) -> Option<Color> { |
| 9 |
color::ansi_16_color_name_to_number(name).and_then(syntect_color_from_ansi_number) |
| 10 |
} |
| 11 |
|
| 12 |
pub fn syntect_color_from_name(name: &str) -> Option<Color> { |
| 13 |
palette::named::from_str(name).map(|color| Color { |
| 14 |
r: color.red, |
| 15 |
g: color.green, |
| 16 |
b: color.blue, |
| 17 |
a: 0xFF, |
| 18 |
}) |
| 19 |
} |
| 20 |
|
| 21 |
/// Convert 8-bit ANSI code to #RGBA string with ANSI code in red channel and 0 in alpha channel. |
| 22 |
// See https://github.com/sharkdp/bat/pull/543 |
| 23 |
pub fn syntect_color_from_ansi_number(n: u8) -> Option<Color> { |
| 24 |
Color::from_str(&format!("#{n:02x}000000")).ok() |
| 25 |
} |
| 26 |
|
| 27 |
pub trait FromAnsiTermStyle { |
| 28 |
fn from_ansi_term_style(ansi_term_style: ansi_term::Style) -> Self; |
| 29 |
} |
| 30 |
|
| 31 |
impl FromAnsiTermStyle for Style { |
| 32 |
fn from_ansi_term_style(ansi_term_style: ansi_term::Style) -> Self { |
| 33 |
let default = Self::default(); |
| 34 |
Self { |
| 35 |
foreground: if let Some(color) = ansi_term_style.foreground { |
| 36 |
Color::from_ansi_term_color(color) |
| 37 |
} else { |
| 38 |
default.foreground |
| 39 |
}, |
| 40 |
background: if let Some(color) = ansi_term_style.background { |
| 41 |
Color::from_ansi_term_color(color) |
| 42 |
} else { |
| 43 |
default.background |
| 44 |
}, |
| 45 |
font_style: FontStyle::from_ansi_term_style(ansi_term_style), |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl FromAnsiTermStyle for FontStyle { |
| 51 |
fn from_ansi_term_style(ansi_term_style: ansi_term::Style) -> Self { |
| 52 |
let mut font_style = FontStyle::empty(); |
| 53 |
if ansi_term_style.is_bold { |
| 54 |
font_style |= FontStyle::BOLD |
| 55 |
} |
| 56 |
if ansi_term_style.is_italic { |
| 57 |
font_style |= FontStyle::ITALIC |
| 58 |
} |
| 59 |
if ansi_term_style.is_underline { |
| 60 |
font_style |= FontStyle::UNDERLINE |
| 61 |
} |
| 62 |
font_style |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pub trait FromAnsiTermColor { |
| 67 |
fn from_ansi_term_color(ansi_term_color: ansi_term::Color) -> Self; |
| 68 |
} |
| 69 |
|
| 70 |
impl FromAnsiTermColor for Color { |
| 71 |
fn from_ansi_term_color(ansi_term_color: ansi_term::Color) -> Self { |
| 72 |
match ansi_term_color { |
| 73 |
ansi_term::Color::Black => syntect_color_from_ansi_number(0).unwrap(), |
| 74 |
ansi_term::Color::Red => syntect_color_from_ansi_number(1).unwrap(), |
| 75 |
ansi_term::Color::Green => syntect_color_from_ansi_number(2).unwrap(), |
| 76 |
ansi_term::Color::Yellow => syntect_color_from_ansi_number(3).unwrap(), |
| 77 |
ansi_term::Color::Blue => syntect_color_from_ansi_number(4).unwrap(), |
| 78 |
ansi_term::Color::Purple => syntect_color_from_ansi_number(5).unwrap(), |
| 79 |
ansi_term::Color::Cyan => syntect_color_from_ansi_number(6).unwrap(), |
| 80 |
ansi_term::Color::White => syntect_color_from_ansi_number(7).unwrap(), |
| 81 |
ansi_term::Color::Fixed(n) => syntect_color_from_ansi_number(n).unwrap(), |
| 82 |
ansi_term::Color::RGB(r, g, b) => Self { r, g, b, a: 0xFF }, |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
pub trait FromDeltaStyle { |
| 88 |
fn from_delta_style(delta_style: delta_style::Style) -> Self; |
| 89 |
} |
| 90 |
|
| 91 |
impl FromDeltaStyle for Style { |
| 92 |
fn from_delta_style(delta_style: delta_style::Style) -> Self { |
| 93 |
Self::from_ansi_term_style(delta_style.ansi_term_style) |
| 94 |
} |
| 95 |
} |