mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-21 20:38:44 +00:00
Allow specifying the theme via the BAT_THEME
environment variable
The `--theme` command line option stills takes precedence and this change preserves how errors are handled when it's used: If a theme name that doesn't exist is specified using the argument, this error is fatal. However, if a theme that doesn't exist is specified using the environment variable, the error is logged to `stderr` and the "Default" theme is loaded as a fallback.
This commit is contained in:
parent
6b57f4eebc
commit
c68aa0f424
16
src/app.rs
16
src/app.rs
@ -1,3 +1,4 @@
|
|||||||
|
use assets::HighlightingAssets;
|
||||||
use atty::{self, Stream};
|
use atty::{self, Stream};
|
||||||
use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand};
|
use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand};
|
||||||
use console::Term;
|
use console::Term;
|
||||||
@ -282,7 +283,18 @@ impl App {
|
|||||||
},
|
},
|
||||||
term_width: Term::stdout().size().1 as usize,
|
term_width: Term::stdout().size().1 as usize,
|
||||||
files,
|
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))?,
|
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 paging_mode: PagingMode,
|
||||||
pub term_width: usize,
|
pub term_width: usize,
|
||||||
pub files: Vec<Option<&'a str>>,
|
pub files: Vec<Option<&'a str>>,
|
||||||
pub theme: Option<&'a str>,
|
pub theme: Option<String>,
|
||||||
pub line_range: Option<LineRange>,
|
pub line_range: Option<LineRange>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
pub fn get_syntax(&self, language: Option<&str>, filename: Option<&str>) -> &SyntaxDefinition {
|
||||||
let syntax = match (language, filename) {
|
let syntax = match (language, filename) {
|
||||||
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
(Some(language), _) => self.syntax_set.find_syntax_by_token(language),
|
||||||
|
@ -57,7 +57,11 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result<bool> {
|
pub fn print_files(assets: &HighlightingAssets, config: &Config) -> Result<bool> {
|
||||||
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 mut output_type = OutputType::from_mode(config.paging_mode);
|
||||||
let handle = output_type.handle()?;
|
let handle = output_type.handle()?;
|
||||||
let mut printer = Printer::new(handle, &config);
|
let mut printer = Printer::new(handle, &config);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user