on 3446fdf help sign in
bat/src/terminal.rs
39 lines 1.0 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) -> String {
22 let style = if !colored {
23 Style::default()
24 } else {
25 let color = to_ansi_color(style.foreground, true_color);
27 if style.font_style.contains(FontStyle::BOLD) {
28 color.bold()
29 } else if style.font_style.contains(FontStyle::UNDERLINE) {
30 color.underline()
31 } else if style.font_style.contains(FontStyle::ITALIC) {
32 color.italic()
33 } else {
34 color.normal()
35 }
36 };
38 style.paint(text).to_string()