@@ -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(®ions, false)
112+ as_terminal_escaped(®ions, 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