2020-04-22 08:56:07 +02:00
|
|
|
use ansi_term::Color::{self, Fixed, RGB};
|
2018-08-19 12:32:35 +02:00
|
|
|
use ansi_term::{self, Style};
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-05-11 13:52:12 +02:00
|
|
|
use syntect::highlighting::{self, FontStyle};
|
2018-04-23 23:56:47 +02:00
|
|
|
|
2020-04-22 08:56:07 +02:00
|
|
|
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Color {
|
2019-04-27 18:48:56 +02:00
|
|
|
if color.a == 0 {
|
|
|
|
// Themes can specify one of the user-configurable terminal colors by
|
|
|
|
// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
|
|
|
|
// to the color palette number. The built-in themes ansi-light,
|
|
|
|
// ansi-dark, and base16 use this.
|
|
|
|
Fixed(color.r)
|
2020-04-22 08:56:07 +02:00
|
|
|
} else if color.a == 0x0f {
|
|
|
|
match color.r {
|
|
|
|
0x00 => Color::Black,
|
|
|
|
0x01 => Color::Red,
|
|
|
|
0x02 => Color::Green,
|
|
|
|
0x03 => Color::Yellow,
|
|
|
|
0x04 => Color::Blue,
|
|
|
|
0x05 => Color::Purple,
|
|
|
|
0x06 => Color::Cyan,
|
|
|
|
0x07 => Color::White,
|
|
|
|
// TODO: the following should be high-intensity variants of
|
|
|
|
// these colors ("bright black", "bright red", ...).
|
|
|
|
0x08 => Color::Black,
|
|
|
|
0x09 => Color::Red,
|
|
|
|
0x0a => Color::Green,
|
|
|
|
0x0b => Color::Yellow,
|
|
|
|
0x0c => Color::Blue,
|
|
|
|
0x0d => Color::Purple,
|
|
|
|
0x0e => Color::Cyan,
|
|
|
|
0x0f => Color::White,
|
2020-04-23 00:56:35 +02:00
|
|
|
_ => unreachable!("The 0x0f color encoding does not allow for codes higher than 0x0f"),
|
2020-04-22 08:56:07 +02:00
|
|
|
}
|
2019-04-27 18:48:56 +02:00
|
|
|
} else if true_color {
|
2018-08-19 12:32:35 +02:00
|
|
|
RGB(color.r, color.g, color.b)
|
|
|
|
} else {
|
2018-09-21 16:56:09 +02:00
|
|
|
Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
|
2018-08-19 12:32:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 14:53:52 +02:00
|
|
|
pub fn as_terminal_escaped(
|
2018-05-12 15:40:47 +02:00
|
|
|
style: highlighting::Style,
|
2018-05-12 06:59:26 +02:00
|
|
|
text: &str,
|
2018-05-06 14:53:52 +02:00
|
|
|
true_color: bool,
|
|
|
|
colored: bool,
|
2018-11-01 17:32:04 +01:00
|
|
|
italics: bool,
|
2018-10-10 06:25:33 +02:00
|
|
|
background_color: Option<highlighting::Color>,
|
2018-05-06 14:53:52 +02:00
|
|
|
) -> String {
|
2019-12-07 02:01:39 +01:00
|
|
|
if text.is_empty() {
|
|
|
|
return text.to_string();
|
|
|
|
}
|
|
|
|
|
2018-10-10 06:25:33 +02:00
|
|
|
let mut style = if !colored {
|
2018-05-12 06:59:26 +02:00
|
|
|
Style::default()
|
|
|
|
} else {
|
2020-03-04 13:05:10 +01:00
|
|
|
let mut color = Style::from(to_ansi_color(style.foreground, true_color));
|
2018-05-12 15:40:47 +02:00
|
|
|
if style.font_style.contains(FontStyle::BOLD) {
|
2020-03-04 13:05:10 +01:00
|
|
|
color = color.bold();
|
|
|
|
}
|
|
|
|
if style.font_style.contains(FontStyle::UNDERLINE) {
|
|
|
|
color = color.underline();
|
|
|
|
}
|
|
|
|
if italics && style.font_style.contains(FontStyle::ITALIC) {
|
|
|
|
color = color.italic();
|
2018-05-12 15:40:47 +02:00
|
|
|
}
|
2020-03-04 13:05:10 +01:00
|
|
|
color
|
2018-05-12 06:59:26 +02:00
|
|
|
};
|
2018-04-23 23:56:47 +02:00
|
|
|
|
2018-10-10 06:25:33 +02:00
|
|
|
style.background = background_color.map(|c| to_ansi_color(c, true_color));
|
2018-05-13 12:26:23 +02:00
|
|
|
style.paint(text).to_string()
|
2018-04-23 23:56:47 +02:00
|
|
|
}
|