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:
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
bat cache --init
```
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
`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)]
use ansi_term;
use assets::BAT_THEME_DEFAULT;
pub struct App {
pub matches: ArgMatches<'static>,
interactive_output: bool,
@ -271,7 +273,7 @@ impl App {
.value_of("theme")
.map(String::from)
.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))?,
})
}

View File

@ -16,6 +16,8 @@ lazy_static! {
ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
}
pub const BAT_THEME_DEFAULT: &str = "Monokai Extended";
pub struct HighlightingAssets {
pub syntax_set: SyntaxSet,
pub theme_set: ThemeSet,
@ -50,11 +52,8 @@ impl HighlightingAssets {
let theme_dir = source_dir.join("themes");
if let Ok(theme_set) = ThemeSet::load_from_folder(&theme_dir) {
// TODO: If syntect would support this, it would be great to
// load the new themes in addition to the ones in the binary.
assets.theme_set = theme_set;
} else {
let res = extend_theme_set(&mut assets.theme_set, &theme_dir);
if !res.is_ok() {
println!(
"No themes were found in '{}', using the default set",
theme_dir.to_string_lossy()
@ -160,7 +159,7 @@ impl HighlightingAssets {
Yellow.paint("[bat warning]"),
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 {
PROJECT_DIRS.cache_dir().join("themes.bin")
}