diff --git a/src/app.rs b/src/app.rs index 41594dd7..30651a03 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,4 +1,3 @@ -use assets::HighlightingAssets; use atty::{self, Stream}; use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand}; use console::Term; @@ -287,15 +286,8 @@ impl App { .matches .value_of("theme") .map(String::from) - .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 - } - }) - }), + .or_else(|| env::var("BAT_THEME").ok()) + .unwrap_or(String::from("Default")), line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?, }) } @@ -349,7 +341,7 @@ pub struct Config<'a> { pub paging_mode: PagingMode, pub term_width: usize, pub files: Vec>, - pub theme: Option, + pub theme: String, pub line_range: Option, } diff --git a/src/assets.rs b/src/assets.rs index 02f5c3b3..8952ed78 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -2,7 +2,6 @@ use directories::ProjectDirs; use errors::*; use std::borrow::Cow; use std::fs::{self, File}; -use std::io; use std::path::{Path, PathBuf}; use syntect::dumps::{dump_to_file, from_binary, from_reader}; use syntect::highlighting::{Theme, ThemeSet}; @@ -123,17 +122,19 @@ impl HighlightingAssets { Ok(()) } - pub fn get_theme(&self, theme: &str) -> Result<&Theme> { - Ok(self.theme_set.themes.get(theme).ok_or_else(|| { - io::Error::new( - io::ErrorKind::Other, - format!("Could not find '{}' theme", theme), - ) - })?) - } - - pub fn theme_exists(&self, theme: &str) -> bool { - self.theme_set.themes.contains_key(theme) + pub fn get_theme(&self, theme: &str) -> &Theme { + match self.theme_set.themes.get(theme) { + Some(theme) => theme, + None => { + use ansi_term::Colour::Yellow; + eprintln!( + "{}: Unknown theme '{}', using default.", + Yellow.paint("[bat warning]"), + theme + ); + &self.theme_set.themes["Default"] + } + } } pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition { diff --git a/src/features.rs b/src/features.rs index efd38ba8..294c76a6 100644 --- a/src/features.rs +++ b/src/features.rs @@ -57,10 +57,7 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) { } pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result { - let theme = assets.get_theme(match config.theme { - Some(ref theme_name) => theme_name, - None => "Default", - })?; + let theme = assets.get_theme(&config.theme); let mut output_type = OutputType::from_mode(config.paging_mode); let handle = output_type.handle()?;