1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-01-31 10:11:07 +00:00

Load customized themes in addition to defaults

- New themes in `$BAT_CONFIG_DIR/themes` are now loaded *in addition* to
  the default themes (they may also override).
- The `Default.tmTheme` symlink is not necessary anymore.

This relates to #172
This commit is contained in:
sharkdp 2018-08-20 21:39:21 +02:00
parent 2df3305b94
commit 052425b12f
6 changed files with 23 additions and 14 deletions

View File

@ -188,18 +188,12 @@ cd "$BAT_CONFIG_DIR/themes"
# Download a theme in '.tmTheme' format, for example: # Download a theme in '.tmTheme' format, for example:
git clone https://github.com/greggb/sublime-snazzy git clone https://github.com/greggb/sublime-snazzy
# Create a link to specify the new default theme
ln -sf "sublime-snazzy/Sublime Snazzy.tmTheme" Default.tmTheme
# Update the binary cache # Update the binary cache
bat cache --init bat cache --init
``` ```
Finally, use `bat --list-themes` to check if the new themes are available. Finally, use `bat --list-themes` to check if the new themes are available.
**Note:** Unlike for syntax definitions, adding custom themes currently *removes all default
themes*. If you want to go back to the default themes, call `bat cache --clear`.
### Using a different pager ### Using a different pager
`bat` uses the pager that is specified in the `PAGER` environment variable. If this variable is not `bat` uses the pager that is specified in the `PAGER` environment variable. If this variable is not

Binary file not shown.

Binary file not shown.

View File

@ -1 +0,0 @@
sublime-monokai-extended/Monokai Extended.tmTheme

View File

@ -9,6 +9,8 @@ use style::{OutputComponent, OutputComponents, OutputWrap};
#[cfg(windows)] #[cfg(windows)]
use ansi_term; use ansi_term;
use assets::BAT_THEME_DEFAULT;
pub struct App { pub struct App {
pub matches: ArgMatches<'static>, pub matches: ArgMatches<'static>,
interactive_output: bool, interactive_output: bool,
@ -271,7 +273,7 @@ impl App {
.value_of("theme") .value_of("theme")
.map(String::from) .map(String::from)
.or_else(|| env::var("BAT_THEME").ok()) .or_else(|| env::var("BAT_THEME").ok())
.unwrap_or(String::from("Default")), .unwrap_or(String::from(BAT_THEME_DEFAULT)),
line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?, line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
}) })
} }

View File

@ -16,6 +16,8 @@ lazy_static! {
ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory"); ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
} }
pub const BAT_THEME_DEFAULT: &str = "Monokai Extended";
pub struct HighlightingAssets { pub struct HighlightingAssets {
pub syntax_set: SyntaxSet, pub syntax_set: SyntaxSet,
pub theme_set: ThemeSet, pub theme_set: ThemeSet,
@ -50,11 +52,8 @@ impl HighlightingAssets {
let theme_dir = source_dir.join("themes"); let theme_dir = source_dir.join("themes");
if let Ok(theme_set) = ThemeSet::load_from_folder(&theme_dir) { let res = extend_theme_set(&mut assets.theme_set, &theme_dir);
// TODO: If syntect would support this, it would be great to if !res.is_ok() {
// load the new themes in addition to the ones in the binary.
assets.theme_set = theme_set;
} else {
println!( println!(
"No themes were found in '{}', using the default set", "No themes were found in '{}', using the default set",
theme_dir.to_string_lossy() theme_dir.to_string_lossy()
@ -160,7 +159,7 @@ impl HighlightingAssets {
Yellow.paint("[bat warning]"), Yellow.paint("[bat warning]"),
theme theme
); );
&self.theme_set.themes["Default"] &self.theme_set.themes[BAT_THEME_DEFAULT]
} }
} }
} }
@ -194,6 +193,21 @@ impl HighlightingAssets {
} }
} }
// TODO: ideally, this function would be part of syntect's `ThemeSet`.
fn extend_theme_set<P: AsRef<Path>>(theme_set: &mut ThemeSet, folder: P) -> Result<()> {
let paths = ThemeSet::discover_theme_paths(folder)?;
for p in &paths {
let theme = ThemeSet::get_theme(p)?;
let basename = p
.file_stem()
.and_then(|x| x.to_str())
.ok_or("Could not get theme basename")?;
theme_set.themes.insert(basename.to_owned(), theme);
}
Ok(())
}
fn theme_set_path() -> PathBuf { fn theme_set_path() -> PathBuf {
PROJECT_DIRS.cache_dir().join("themes.bin") PROJECT_DIRS.cache_dir().join("themes.bin")
} }