on 89fcbb2 help sign in
bat/src/terminal.rs
48 lines 1.5 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 color.a == 0 {
10 // Themes can specify one of the user-configurable terminal colors by
11 // encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
12 // to the color palette number. The built-in themes ansi-light,
13 // ansi-dark, and base16 use this.
14 Fixed(color.r)
15 } else if true_color {
16 RGB(color.r, color.g, color.b)
17 } else {
18 Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
19 }
22pub fn as_terminal_escaped(
23 style: highlighting::Style,
24 text: &str,
25 true_color: bool,
26 colored: bool,
27 italics: bool,
28 background_color: Option<highlighting::Color>,
29) -> String {
30 let mut style = if !colored {
31 Style::default()
32 } else {
33 let color = to_ansi_color(style.foreground, true_color);
35 if style.font_style.contains(FontStyle::BOLD) {
36 color.bold()
37 } else if style.font_style.contains(FontStyle::UNDERLINE) {
38 color.underline()
39 } else if italics && style.font_style.contains(FontStyle::ITALIC) {
40 color.italic()
41 } else {
42 color.normal()
43 }
44 };
46 style.background = background_color.map(|c| to_ansi_color(c, true_color));
47 style.paint(text).to_string()