on master help sign in
Always show a warning when theme is unknown
commit28397b8f788a3a6a6ecb2946495fb7df777ef699 authorsharkdp <davidpeter@web.de> dateMon 23 Jul 2018, 21:38 (8 years ago) parentc899849
3 files changed+17−27
src/app.rs+3−11 src/assets.rs+13−12 src/features.rs+1−4
src/app.rs+3−11
@@ -1,4 +1,3 @@
1−use assets::HighlightingAssets;
21use atty::{self, Stream};
32use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand};
43use console::Term;
@@ -287,15 +286,8 @@ impl App {
287286 .matches
288287 .value_of("theme")
289288 .map(String::from)
290− .or_else(|| {
291− env::var("BAT_THEME").ok().and_then(|theme_name_env| {
292− if HighlightingAssets::new().theme_exists(&theme_name_env) {
293− Some(theme_name_env)
294− } else {
295− None
296− }
297− })
298− }),
289+ .or_else(|| env::var("BAT_THEME").ok())
290+ .unwrap_or(String::from("Default")),
299291 line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
300292 })
301293 }
@@ -349,7 +341,7 @@ pub struct Config<'a> {
349341 pub paging_mode: PagingMode,
350342 pub term_width: usize,
351343 pub files: Vec<Option<&'a str>>,
352− pub theme: Option<String>,
344+ pub theme: String,
353345 pub line_range: Option<LineRange>,
354346}
355347
src/assets.rs+13−12
@@ -2,7 +2,6 @@ use directories::ProjectDirs;
22use errors::*;
33use std::borrow::Cow;
44use std::fs::{self, File};
5−use std::io;
65use std::path::{Path, PathBuf};
76use syntect::dumps::{dump_to_file, from_binary, from_reader};
87use syntect::highlighting::{Theme, ThemeSet};
@@ -123,17 +122,19 @@ impl HighlightingAssets {
123122 Ok(())
124123 }
125124
126− pub fn get_theme(&self, theme: &str) -> Result<&Theme> {
127− Ok(self.theme_set.themes.get(theme).ok_or_else(|| {
128− io::Error::new(
129− io::ErrorKind::Other,
130− format!("Could not find '{}' theme", theme),
131− )
132− })?)
133− }
134−
135− pub fn theme_exists(&self, theme: &str) -> bool {
136− self.theme_set.themes.contains_key(theme)
125+ pub fn get_theme(&self, theme: &str) -> &Theme {
126+ match self.theme_set.themes.get(theme) {
127+ Some(theme) => theme,
128+ None => {
129+ use ansi_term::Colour::Yellow;
130+ eprintln!(
131+ "{}: Unknown theme '{}', using default.",
132+ Yellow.paint("[bat warning]"),
133+ theme
134+ );
135+ &self.theme_set.themes["Default"]
136+ }
137+ }
137138 }
138139
139140 pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition {
src/features.rs+1−4
@@ -57,10 +57,7 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
5757}
5858
5959pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result<bool> {
60− let theme = assets.get_theme(match config.theme {
61− Some(ref theme_name) => theme_name,
62− None => "Default",
63− })?;
60+ let theme = assets.get_theme(&config.theme);
6461
6562 let mut output_type = OutputType::from_mode(config.paging_mode);
6663 let handle = output_type.handle()?;