2018-09-21 14:56:09 +00:00
|
|
|
extern crate ansi_colours;
|
|
|
|
|
2018-04-23 21:56:47 +00:00
|
|
|
use ansi_term::Colour::{Fixed, RGB};
|
2018-08-19 10:32:35 +00:00
|
|
|
use ansi_term::{self, Style};
|
2018-08-22 20:29:12 +00:00
|
|
|
|
2018-05-11 11:52:12 +00:00
|
|
|
use syntect::highlighting::{self, FontStyle};
|
2018-04-23 21:56:47 +00:00
|
|
|
|
2018-08-19 10:32:35 +00:00
|
|
|
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Colour {
|
2019-04-27 16:48:56 +00: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)
|
|
|
|
} else if true_color {
|
2018-08-19 10:32:35 +00:00
|
|
|
RGB(color.r, color.g, color.b)
|
|
|
|
} else {
|
2018-09-21 14:56:09 +00:00
|
|
|
Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
|
2018-08-19 10:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-06 12:53:52 +00:00
|
|
|
pub fn as_terminal_escaped(
|
2018-05-12 13:40:47 +00:00
|
|
|
style: highlighting::Style,
|
2018-05-12 04:59:26 +00:00
|
|
|
text: &str,
|
2018-05-06 12:53:52 +00:00
|
|
|
true_color: bool,
|
|
|
|
colored: bool,
|
2018-11-01 16:32:04 +00:00
|
|
|
italics: bool,
|
2018-10-10 04:25:33 +00:00
|
|
|
background_color: Option<highlighting::Color>,
|
2018-05-06 12:53:52 +00:00
|
|
|
) -> String {
|
2018-10-10 04:25:33 +00:00
|
|
|
let mut style = if !colored {
|
2018-05-12 04:59:26 +00:00
|
|
|
Style::default()
|
|
|
|
} else {
|
2018-08-19 10:32:35 +00:00
|
|
|
let color = to_ansi_color(style.foreground, true_color);
|
2018-04-23 21:56:47 +00:00
|
|
|
|
2018-05-12 13:40:47 +00:00
|
|
|
if style.font_style.contains(FontStyle::BOLD) {
|
|
|
|
color.bold()
|
|
|
|
} else if style.font_style.contains(FontStyle::UNDERLINE) {
|
|
|
|
color.underline()
|
2018-11-04 17:08:33 +00:00
|
|
|
} else if italics && style.font_style.contains(FontStyle::ITALIC) {
|
2018-05-12 13:40:47 +00:00
|
|
|
color.italic()
|
|
|
|
} else {
|
|
|
|
color.normal()
|
|
|
|
}
|
2018-05-12 04:59:26 +00:00
|
|
|
};
|
2018-04-23 21:56:47 +00:00
|
|
|
|
2018-10-10 04:25:33 +00:00
|
|
|
style.background = background_color.map(|c| to_ansi_color(c, true_color));
|
2018-05-13 10:26:23 +00:00
|
|
|
style.paint(text).to_string()
|
2018-04-23 21:56:47 +00:00
|
|
|
}
|