diff --git a/src/app.rs b/src/app.rs index dab20924..0616c83f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,4 @@ +use assets::HighlightingAssets; use atty::{self, Stream}; use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand}; use console::Term; @@ -282,7 +283,18 @@ impl App { }, term_width: Term::stdout().size().1 as usize, files, - theme: self.matches.value_of("theme"), + theme: self.matches + .value_of("theme") + .and_then(|theme_name_arg| Some(String::from(theme_name_arg))) + .or_else(|| { + env::var("BAT_THEME").ok().and_then(|theme_name_env| { + if HighlightingAssets::new().theme_exists(&theme_name_env) { + Some(theme_name_env) + } else { + None + } + }) + }), line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?, }) } @@ -336,7 +348,7 @@ pub struct Config<'a> { pub paging_mode: PagingMode, pub term_width: usize, pub files: Vec>, - pub theme: Option<&'a str>, + pub theme: Option, pub line_range: Option, } diff --git a/src/assets.rs b/src/assets.rs index 5a39daed..02f5c3b3 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -132,6 +132,10 @@ impl HighlightingAssets { })?) } + pub fn theme_exists(&self, theme: &str) -> bool { + self.theme_set.themes.contains_key(theme) + } + pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition { let syntax = match (language, filename) { (Some(language), _) => self.syntax_set.find_syntax_by_token(language), diff --git a/src/features.rs b/src/features.rs index c46ecb78..efd38ba8 100644 --- a/src/features.rs +++ b/src/features.rs @@ -57,7 +57,11 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) { } pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result { - let theme = assets.get_theme(config.theme.unwrap_or("Default"))?; + let theme = assets.get_theme(match config.theme { + Some(ref theme_name) => theme_name, + None => "Default", + })?; + let mut output_type = OutputType::from_mode(config.paging_mode); let handle = output_type.handle()?; let mut printer = Printer::new(handle, &config);