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:
committed by
David Peter
parent
23d92d7641
commit
d4553c6b38
@@ -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();
|
||||
|
Reference in New Issue
Block a user