on master help sign in
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

commit052425b12f79aa42b1ca6453725b501be87dcc8d authorsharkdp <davidpeter@web.de> dateMon 20 Aug 2018, 21:39 (8 years ago) parent2df3305
6 files changed+23−14
README.md−6 assets/syntaxes.bin assets/themes.bin assets/themes/Default.tmTheme−1 src/app.rs+3−1 src/assets.rs+20−6
README.md−6
@@ -188,18 +188,12 @@ cd "$BAT_CONFIG_DIR/themes"
188188# Download a theme in '.tmTheme' format, for example:
189189git clone https://github.com/greggb/sublime-snazzy
190190
191−# Create a link to specify the new default theme
192−ln -sf "sublime-snazzy/Sublime Snazzy.tmTheme" Default.tmTheme
193−
194191# Update the binary cache
195192bat cache --init
196193```
197194
198195Finally, use `bat --list-themes` to check if the new themes are available.
199196
200−**Note:** Unlike for syntax definitions, adding custom themes currently *removes all default
201−themes*. If you want to go back to the default themes, call `bat cache --clear`.
202−
203197### Using a different pager
204198
205199`bat` uses the pager that is specified in the `PAGER` environment variable. If this variable is not
assets/syntaxes.bin

Binary file not shown.

assets/themes.bin

Binary file not shown.

assets/themes/Default.tmThemedeleted−1
@@ -1 +0,0 @@
1−sublime-monokai-extended/Monokai Extended.tmTheme
src/app.rs+3−1
@@ -9,6 +9,8 @@ use style::{OutputComponent, OutputComponents, OutputWrap};
99#[cfg(windows)]
1010use ansi_term;
1111
12+use assets::BAT_THEME_DEFAULT;
13+
1214pub struct App {
1315 pub matches: ArgMatches<'static>,
1416 interactive_output: bool,
@@ -271,7 +273,7 @@ impl App {
271273 .value_of("theme")
272274 .map(String::from)
273275 .or_else(|| env::var("BAT_THEME").ok())
274− .unwrap_or(String::from("Default")),
276+ .unwrap_or(String::from(BAT_THEME_DEFAULT)),
275277 line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
276278 })
277279 }
src/assets.rs+20−6
@@ -16,6 +16,8 @@ lazy_static! {
1616 ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
1717}
1818
19+pub const BAT_THEME_DEFAULT: &str = "Monokai Extended";
20+
1921pub struct HighlightingAssets {
2022 pub syntax_set: SyntaxSet,
2123 pub theme_set: ThemeSet,
@@ -50,11 +52,8 @@ impl HighlightingAssets {
5052
5153 let theme_dir = source_dir.join("themes");
5254
53− if let Ok(theme_set) = ThemeSet::load_from_folder(&theme_dir) {
54− // TODO: If syntect would support this, it would be great to
55− // load the new themes in addition to the ones in the binary.
56− assets.theme_set = theme_set;
57− } else {
55+ let res = extend_theme_set(&mut assets.theme_set, &theme_dir);
56+ if !res.is_ok() {
5857 println!(
5958 "No themes were found in '{}', using the default set",
6059 theme_dir.to_string_lossy()
@@ -160,7 +159,7 @@ impl HighlightingAssets {
160159 Yellow.paint("[bat warning]"),
161160 theme
162161 );
163− &self.theme_set.themes["Default"]
162+ &self.theme_set.themes[BAT_THEME_DEFAULT]
164163 }
165164 }
166165 }
@@ -194,6 +193,21 @@ impl HighlightingAssets {
194193 }
195194}
196195
196+// TODO: ideally, this function would be part of syntect's `ThemeSet`.
197+fn extend_theme_set<P: AsRef<Path>>(theme_set: &mut ThemeSet, folder: P) -> Result<()> {
198+ let paths = ThemeSet::discover_theme_paths(folder)?;
199+ for p in &paths {
200+ let theme = ThemeSet::get_theme(p)?;
201+ let basename = p
202+ .file_stem()
203+ .and_then(|x| x.to_str())
204+ .ok_or("Could not get theme basename")?;
205+ theme_set.themes.insert(basename.to_owned(), theme);
206+ }
207+
208+ Ok(())
209+}
210+
197211fn theme_set_path() -> PathBuf {
198212 PROJECT_DIRS.cache_dir().join("themes.bin")
199213}