2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-15 00:12:57 +00:00
bat/src/terminal.rs

40 lines
1023 B
Rust
Raw Normal View History

extern crate ansi_colours;
2018-04-23 21:56:47 +00:00
use ansi_term::Colour::{Fixed, RGB};
use ansi_term::{self, Style};
2018-08-22 20:29:12 +00:00
use syntect::highlighting::{self, FontStyle};
2018-04-23 21:56:47 +00: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 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,
) -> String {
2018-05-12 04:59:26 +00:00
let style = if !colored {
Style::default()
} else {
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()
} else if style.font_style.contains(FontStyle::ITALIC) {
color.italic()
} else {
color.normal()
}
2018-05-12 04:59:26 +00:00
};
2018-04-23 21:56:47 +00:00
style.paint(text).to_string()
2018-04-23 21:56:47 +00:00
}