bat/src/terminal.rs

83 lines
3.0 KiB
Rust
Raw Normal View History

use ansi_term::Color::{self, Fixed, RGB};
use ansi_term::{self, Style};
2018-08-22 22:29:12 +02:00
use syntect::highlighting::{self, FontStyle};
2018-04-23 23:56:47 +02:00
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> Option<ansi_term::Color> {
if color.a == 0 {
// Themes can specify one of the user-configurable terminal colors by
// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
// to the 8-bit color palette number. The built-in themes ansi, base16,
// and base16-256 use this.
Some(match color.r {
// For the first 8 colors, use the Color enum to produce ANSI escape
// sequences using codes 30-37 (foreground) and 40-47 (background).
// For example, red foreground is \x1b[31m. This works on terminals
// without 256-color support.
0x00 => Color::Black,
0x01 => Color::Red,
0x02 => Color::Green,
0x03 => Color::Yellow,
0x04 => Color::Blue,
0x05 => Color::Purple,
0x06 => Color::Cyan,
0x07 => Color::White,
// For all other colors, use Fixed to produce escape sequences using
// codes 38;5 (foreground) and 48;5 (background). For example,
// bright red foreground is \x1b[38;5;9m. This only works on
// terminals with 256-color support.
//
// TODO: When ansi_term adds support for bright variants using codes
// 90-97 (foreground) and 100-107 (background), we should use those
// for values 0x08 to 0x0f and only use Fixed for 0x10 to 0xff.
n => Fixed(n),
})
} else if color.a == 1 {
// Themes can specify the terminal's default foreground/background color
// (i.e. no escape sequence) using the encoding #RRGGBBAA with AA set to
// 01. The built-in theme ansi uses this.
None
} else if true_color {
Some(RGB(color.r, color.g, color.b))
} else {
Some(Fixed(ansi_colours::ansi256_from_rgb((
color.r, color.g, color.b,
))))
}
}
2018-05-06 14:53:52 +02:00
pub fn as_terminal_escaped(
2018-05-12 15:40:47 +02:00
style: highlighting::Style,
2018-05-12 06:59:26 +02:00
text: &str,
2018-05-06 14:53:52 +02:00
true_color: bool,
colored: bool,
2018-11-01 17:32:04 +01:00
italics: bool,
2018-10-10 06:25:33 +02:00
background_color: Option<highlighting::Color>,
2018-05-06 14:53:52 +02:00
) -> String {
if text.is_empty() {
return text.to_string();
}
2018-10-10 06:25:33 +02:00
let mut style = if !colored {
2018-05-12 06:59:26 +02:00
Style::default()
} else {
let mut color = Style {
foreground: to_ansi_color(style.foreground, true_color),
..Style::default()
};
2018-05-12 15:40:47 +02:00
if style.font_style.contains(FontStyle::BOLD) {
color = color.bold();
}
if style.font_style.contains(FontStyle::UNDERLINE) {
color = color.underline();
}
if italics && style.font_style.contains(FontStyle::ITALIC) {
color = color.italic();
2018-05-12 15:40:47 +02:00
}
color
2018-05-12 06:59:26 +02:00
};
2018-04-23 23:56:47 +02:00
style.background = background_color.and_then(|c| to_ansi_color(c, true_color));
style.paint(text).to_string()
2018-04-23 23:56:47 +02:00
}