Add fallback theme, remove BAT_THEME_DEFAULT

This commit is contained in:
sharkdp 2020-03-21 20:31:32 +01:00 committed by David Peter
parent 094c526a0e
commit 83dc13a86d
2 changed files with 16 additions and 6 deletions

View File

@ -11,15 +11,18 @@ use crate::errors::*;
use crate::inputfile::{InputFile, InputFileReader}; use crate::inputfile::{InputFile, InputFileReader};
use crate::syntax_mapping::SyntaxMapping; use crate::syntax_mapping::SyntaxMapping;
pub const BAT_THEME_DEFAULT: &str = "Monokai Extended";
#[derive(Debug)] #[derive(Debug)]
pub struct HighlightingAssets { pub struct HighlightingAssets {
pub(crate) syntax_set: SyntaxSet, pub(crate) syntax_set: SyntaxSet,
pub(crate) theme_set: ThemeSet, pub(crate) theme_set: ThemeSet,
fallback_theme: Option<&'static str>,
} }
impl HighlightingAssets { impl HighlightingAssets {
pub fn default_theme() -> &'static str {
"Monokai Extended"
}
pub fn from_files(source_dir: &Path, start_empty: bool) -> Result<Self> { pub fn from_files(source_dir: &Path, start_empty: bool) -> Result<Self> {
let mut theme_set = if start_empty { let mut theme_set = if start_empty {
ThemeSet { ThemeSet {
@ -60,6 +63,7 @@ impl HighlightingAssets {
Ok(HighlightingAssets { Ok(HighlightingAssets {
syntax_set: syntax_set_builder.build(), syntax_set: syntax_set_builder.build(),
theme_set, theme_set,
fallback_theme: None,
}) })
} }
@ -85,6 +89,7 @@ impl HighlightingAssets {
Ok(HighlightingAssets { Ok(HighlightingAssets {
syntax_set, syntax_set,
theme_set, theme_set,
fallback_theme: None,
}) })
} }
@ -103,6 +108,7 @@ impl HighlightingAssets {
HighlightingAssets { HighlightingAssets {
syntax_set, syntax_set,
theme_set, theme_set,
fallback_theme: None,
} }
} }
@ -138,6 +144,10 @@ impl HighlightingAssets {
Ok(()) Ok(())
} }
pub fn set_fallback_theme(&mut self, theme: &'static str) {
self.fallback_theme = Some(theme);
}
pub fn syntaxes(&self) -> &[SyntaxReference] { pub fn syntaxes(&self) -> &[SyntaxReference] {
self.syntax_set.syntaxes() self.syntax_set.syntaxes()
} }
@ -156,7 +166,7 @@ impl HighlightingAssets {
Yellow.paint("[bat warning]"), Yellow.paint("[bat warning]"),
theme theme
); );
&self.theme_set.themes[BAT_THEME_DEFAULT] &self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
} }
} }
} }

View File

@ -17,7 +17,7 @@ use console::Term;
use ansi_term; use ansi_term;
use bat::{ use bat::{
assets::BAT_THEME_DEFAULT, assets::HighlightingAssets,
config::{Config, PagingMode}, config::{Config, PagingMode},
errors::*, errors::*,
inputfile::InputFile, inputfile::InputFile,
@ -195,12 +195,12 @@ impl App {
.or_else(|| env::var("BAT_THEME").ok()) .or_else(|| env::var("BAT_THEME").ok())
.map(|s| { .map(|s| {
if s == "default" { if s == "default" {
String::from(BAT_THEME_DEFAULT) String::from(HighlightingAssets::default_theme())
} else { } else {
s s
} }
}) })
.unwrap_or_else(|| String::from(BAT_THEME_DEFAULT)), .unwrap_or_else(|| String::from(HighlightingAssets::default_theme())),
line_ranges: self line_ranges: self
.matches .matches
.values_of("line-range") .values_of("line-range")