diff --git a/README.md b/README.md index 865d25b5..1898dd44 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/assets/syntaxes.bin b/assets/syntaxes.bin index 05a9b05c..689c50d7 100644 Binary files a/assets/syntaxes.bin and b/assets/syntaxes.bin differ diff --git a/assets/themes.bin b/assets/themes.bin index c7b4366d..5e1710c9 100644 Binary files a/assets/themes.bin and b/assets/themes.bin differ diff --git a/assets/themes/Default.tmTheme b/assets/themes/Default.tmTheme deleted file mode 120000 index 0344e33b..00000000 --- a/assets/themes/Default.tmTheme +++ /dev/null @@ -1 +0,0 @@ -sublime-monokai-extended/Monokai Extended.tmTheme \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index d3394d89..0b98c7ca 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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))?, }) } diff --git a/src/assets.rs b/src/assets.rs index aa7d4734..b36233b5 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -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>(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") }