bat/src/terminal.rs

43 lines
1.2 KiB
Rust
Raw Normal View History

extern crate ansi_colours;
2018-04-23 23:56:47 +02:00
use ansi_term::Colour::{Fixed, RGB};
use ansi_term::{self, Style};
2018-08-22 22:29:12 +02:00
use syntect::highlighting::{self, FontStyle};
2018-04-23 23:56:47 +02:00
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Colour {
if true_color {
RGB(color.r, color.g, color.b)
} else {
Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
}
}
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 {
2018-10-10 06:25:33 +02:00
let mut style = if !colored {
2018-05-12 06:59:26 +02:00
Style::default()
} else {
let color = to_ansi_color(style.foreground, true_color);
2018-04-23 23:56:47 +02:00
2018-05-12 15:40:47 +02:00
if style.font_style.contains(FontStyle::BOLD) {
color.bold()
} else if style.font_style.contains(FontStyle::UNDERLINE) {
color.underline()
} else if italics && style.font_style.contains(FontStyle::ITALIC) {
2018-05-12 15:40:47 +02:00
color.italic()
} else {
color.normal()
}
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));
style.paint(text).to_string()
2018-04-23 23:56:47 +02:00
}