1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-10-03 18:42:28 +01:00

Add color flag

Colors are disabled if the terminal is not interactive unless explicitly
set otherwise
This commit is contained in:
Ezinwa Okpoechi
2018-05-05 15:55:16 +02:00
committed by David Peter
parent 23d92d7641
commit d4553c6b38
2 changed files with 75 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
use std::fmt::Write;
use ansi_term::Style;
use ansi_term::Colour::{Fixed, RGB};
use syntect::highlighting;
@@ -25,14 +26,16 @@ fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
}
}
pub fn as_terminal_escaped(v: &[(highlighting::Style, &str)], true_color: bool) -> String {
pub fn as_terminal_escaped(v: &[(highlighting::Style, &str)], true_color: bool, colored: bool) -> String {
let mut s: String = String::new();
for &(ref style, text) in v.iter() {
let style = if true_color {
RGB(style.foreground.r, style.foreground.g, style.foreground.b)
let style = if !colored {
Style::default()
} else if true_color {
RGB(style.foreground.r, style.foreground.g, style.foreground.b).normal()
} else {
let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
Fixed(ansi)
Fixed(ansi).normal()
};
write!(s, "{}", style.paint(text)).unwrap();