on master help sign in
Enable 8 bit color support, closes #11
commit38762c34d9180ca9e34302bf7f666a9f73a41618 authorsharkdp <davidpeter@web.de> dateMon 23 Apr 2018, 23:56 (9 years ago) parent9435e16
2 files changed+83−3
src/main.rs+17−3 src/terminal.rs+66
src/main.rs+17−3
@@ -7,6 +7,8 @@ extern crate syntect;
77#[macro_use]
88extern crate clap;
99
10+mod terminal;
11+
1012use std::collections::HashMap;
1113use std::env;
1214use std::io::{self, BufRead, ErrorKind, Result, StdoutLock, Write};
@@ -23,7 +25,12 @@ use git2::{DiffOptions, IntoCString, Repository};
2325use syntect::easy::HighlightFile;
2426use syntect::highlighting::{Theme, ThemeSet};
2527use syntect::parsing::SyntaxSet;
26−use syntect::util::as_24_bit_terminal_escaped;
28+
29+use terminal::as_terminal_escaped;
30+
31+struct Options {
32+ true_color: bool,
33+}
2734
2835#[derive(Copy, Clone, Debug)]
2936enum LineChange {
@@ -52,6 +59,7 @@ fn print_horizontal_line(
5259}
5360
5461fn print_file<P: AsRef<Path>>(
62+ options: &Options,
5563 theme: &Theme,
5664 syntax_set: &SyntaxSet,
5765 filename: P,
@@ -101,7 +109,7 @@ fn print_file<P: AsRef<Path>>(
101109 Fixed(244).paint(format!("{:4}", line_nr)),
102110 line_change,
103111 Fixed(GRID_COLOR).paint("│"),
104− as_24_bit_terminal_escaped(&regions, false)
112+ as_terminal_escaped(&regions, options.true_color)
105113 )?;
106114 }
107115
@@ -169,6 +177,12 @@ fn run(matches: &ArgMatches) -> Result<()> {
169177 "Could not get home directory",
170178 ))?;
171179
180+ let colorterm = env::var("COLORTERM").unwrap_or("".into());
181+
182+ let options = Options {
183+ true_color: colorterm == "truecolor" || colorterm == "24bit",
184+ };
185+
172186 let theme_dir = home_dir.join(".config").join("bat").join("themes");
173187 let theme_set = ThemeSet::load_from_folder(theme_dir)
174188 .map_err(|_| io::Error::new(ErrorKind::Other, "Could not load themes"))?;
@@ -179,7 +193,7 @@ fn run(matches: &ArgMatches) -> Result<()> {
179193 if let Some(files) = matches.values_of("FILE") {
180194 for file in files {
181195 let line_changes = get_git_diff(file.to_string());
182− print_file(theme, &syntax_set, file, line_changes)?;
196+ print_file(&options, theme, &syntax_set, file, line_changes)?;
183197 }
184198 }
185199
src/terminal.rsnew file+66
@@ -0,0 +1,66 @@
1+use std::fmt::Write;
2+
3+use ansi_term::Colour::{Fixed, RGB};
4+use syntect::highlighting;
5+
6+/// Approximate a 24 bit color value by a 8 bit ANSI code
7+fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
8+ const BLACK: u8 = 16;
9+ const WHITE: u8 = 231;
10+
11+ if r == g && g == b {
12+ if r < 8 {
13+ BLACK
14+ } else if r > 248 {
15+ WHITE
16+ } else {
17+ let fr = r as f32;
18+ (((fr - 8.) / 247.) * 24.) as u8 + 232
19+ }
20+ } else {
21+ let fr = r as f32;
22+ let fg = g as f32;
23+ let fb = b as f32;
24+ 16 + (36 * (fr / 255. * 5.) as u8) + (6 * (fg / 255. * 5.) as u8) + (fb / 255. * 5.) as u8
25+ }
26+}
27+
28+pub fn as_terminal_escaped(v: &[(highlighting::Style, &str)], true_color: bool) -> String {
29+ let mut s: String = String::new();
30+ for &(ref style, text) in v.iter() {
31+ let style = if true_color {
32+ RGB(style.foreground.r, style.foreground.g, style.foreground.b)
33+ } else {
34+ let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
35+ Fixed(ansi)
36+ };
37+
38+ write!(s, "{}", style.paint(text)).unwrap();
39+ }
40+
41+ s
42+}
43+
44+#[test]
45+fn test_rgb2ansi_black_white() {
46+ assert_eq!(16, rgb2ansi(0x00, 0x00, 0x00));
47+ assert_eq!(231, rgb2ansi(0xff, 0xff, 0xff));
48+}
49+
50+#[test]
51+fn test_rgb2ansi_gray() {
52+ assert_eq!(241, rgb2ansi(0x6c, 0x6c, 0x6c));
53+ assert_eq!(233, rgb2ansi(0x1c, 0x1c, 0x1c));
54+}
55+
56+#[test]
57+fn test_rgb2ansi_color() {
58+ assert_eq!(96, rgb2ansi(0x87, 0x5f, 0x87));
59+ assert_eq!(141, rgb2ansi(0xaf, 0x87, 0xff));
60+ assert_eq!(193, rgb2ansi(0xd7, 0xff, 0xaf));
61+}
62+
63+#[test]
64+fn test_rgb2ansi_approx() {
65+ assert_eq!(231, rgb2ansi(0xfe, 0xfe, 0xfe));
66+}