Always show a warning when theme is unknown

This commit is contained in:
sharkdp 2018-07-23 21:38:45 +02:00 committed by David Peter
parent c899849101
commit 28397b8f78
3 changed files with 17 additions and 27 deletions

View File

@ -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<Option<&'a str>>,
pub theme: Option<String>,
pub theme: String,
pub line_range: Option<LineRange>,
}

View File

@ -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 {

View File

@ -57,10 +57,7 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
}
pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result<bool> {
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()?;