on e04bccd help sign in
bat/src/terminal.rs
42 lines 1.2 kB history raw
1extern crate ansi_colours;
3use ansi_term::Colour::{Fixed, RGB};
4use ansi_term::{self, Style};
6use syntect::highlighting::{self, FontStyle};
8pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Colour {
9 if true_color {
10 RGB(color.r, color.g, color.b)
11 } else {
12 Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
13 }
16pub fn as_terminal_escaped(
17 style: highlighting::Style,
18 text: &str,
19 true_color: bool,
20 colored: bool,
21 italics: bool,
22 background_color: Option<highlighting::Color>,
23) -> String {
24 let mut style = if !colored {
25 Style::default()
26 } else {
27 let color = to_ansi_color(style.foreground, true_color);
29 if style.font_style.contains(FontStyle::BOLD) {
30 color.bold()
31 } else if style.font_style.contains(FontStyle::UNDERLINE) {
32 color.underline()
33 } else if italics && style.font_style.contains(FontStyle::ITALIC) {
34 color.italic()
35 } else {
36 color.normal()
37 }
38 };
40 style.background = background_color.map(|c| to_ansi_color(c, true_color));
41 style.paint(text).to_string()