Debian Codegraph @

src/utils/bat/terminal.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 use ansi_term::Color::{self, Fixed, Rgb};
2 use ansi_term::{self, Style};
3
4 use syntect::highlighting::{self, FontStyle};
5
6 pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> Option<ansi_term::Color> {
7 if color.a == 0 {
8 // Themes can specify one of the user-configurable terminal colors by
9 // encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
10 // to the 8-bit color palette number. The built-in themes ansi, base16,
11 // and base16-256 use this.
12 Some(match color.r {
13 // For the first 8 colors, use the Color enum to produce ANSI escape
14 // sequences using codes 30-37 (foreground) and 40-47 (background).
15 // For example, red foreground is \x1b[31m. This works on terminals
16 // without 256-color support.
17 0x00 => Color::Black,
18 0x01 => Color::Red,
19 0x02 => Color::Green,
20 0x03 => Color::Yellow,
21 0x04 => Color::Blue,
22 0x05 => Color::Purple,
23 0x06 => Color::Cyan,
24 0x07 => Color::White,
25 // For all other colors, use Fixed to produce escape sequences using
26 // codes 38;5 (foreground) and 48;5 (background). For example,
27 // bright red foreground is \x1b[38;5;9m. This only works on
28 // terminals with 256-color support.
29 //
30 // TODO: When ansi_term adds support for bright variants using codes
31 // 90-97 (foreground) and 100-107 (background), we should use those
32 // for values 0x08 to 0x0f and only use Fixed for 0x10 to 0xff.
33 n => Fixed(n),
34 })
35 } else if color.a == 1 {
36 // Themes can specify the terminal's default foreground/background color
37 // (i.e. no escape sequence) using the encoding #RRGGBBAA with AA set to
38 // 01. The built-in theme ansi uses this.
39 None
40 } else if true_color {
•41 Some(Rgb(color.r, color.g, color.b))
42 } else {
43 Some(Fixed(ansi_colours::ansi256_from_rgb((
44 color.r, color.g, color.b,
45 ))))
46 }
47 }
48
49 #[allow(dead_code)]
50 pub fn as_terminal_escaped(
51 style: highlighting::Style,
52 text: &str,
53 true_color: bool,
54 colored: bool,
55 italics: bool,
56 background_color: Option<highlighting::Color>,
57 ) -> String {
58 if text.is_empty() {
59 return text.to_string();
60 }
61
62 let mut style = if !colored {
63 Style::default()
64 } else {
65 let mut color = Style {
66 foreground: to_ansi_color(style.foreground, true_color),
67 ..Style::default()
68 };
69 if style.font_style.contains(FontStyle::BOLD) {
70 color = color.bold();
71 }
72 if style.font_style.contains(FontStyle::UNDERLINE) {
73 color = color.underline();
74 }
75 if italics && style.font_style.contains(FontStyle::ITALIC) {
76 color = color.italic();
77 }
78 color
79 };
80
81 style.background = background_color.and_then(|c| to_ansi_color(c, true_color));
82 style.paint(text).to_string()
83 }