From 2df3305b94ff85878b61415d6136c8a5b2d3e439 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Mon, 20 Aug 2018 21:12:52 +0200 Subject: [PATCH] Add new '--blank' option for 'bat cache --init' closes #206 --- assets/create.sh | 2 +- src/app.rs | 6 ++++++ src/assets.rs | 22 ++++++++++++++++++++-- src/main.rs | 4 +++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/assets/create.sh b/assets/create.sh index 84c4ea35..60b70b32 100644 --- a/assets/create.sh +++ b/assets/create.sh @@ -2,4 +2,4 @@ ASSET_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -bat cache --init --source="$ASSET_DIR" --target="$ASSET_DIR" +bat cache --init --blank --source="$ASSET_DIR" --target="$ASSET_DIR" diff --git a/src/app.rs b/src/app.rs index 6c9a17ab..d3394d89 100644 --- a/src/app.rs +++ b/src/app.rs @@ -210,6 +210,12 @@ impl App { .help( "Use a different directory to store the cached syntax and theme set", ), + ).arg( + Arg::with_name("blank") + .long("blank") + .requires("init") + .help("Create completely new syntax and theme sets \ + (instead of appending to the default sets") ), ).help_message("Print this help message.") .version_message("Show version information.") diff --git a/src/assets.rs b/src/assets.rs index c497cfa4..aa7d4734 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -1,6 +1,7 @@ use directories::ProjectDirs; use errors::*; use std::borrow::Cow; +use std::collections::BTreeMap; use std::fs::{self, File}; use std::path::{Path, PathBuf}; use syntect::dumps::{dump_to_file, from_binary, from_reader}; @@ -25,10 +26,27 @@ impl HighlightingAssets { Self::from_cache().unwrap_or_else(|_| Self::from_binary()) } - pub fn from_files(dir: Option<&Path>) -> Result { + fn empty() -> Self { + let mut syntax_set = SyntaxSet::new(); + syntax_set.load_plain_text_syntax(); + + let theme_set = ThemeSet { + themes: BTreeMap::new(), + }; + HighlightingAssets { + syntax_set, + theme_set, + } + } + + pub fn from_files(dir: Option<&Path>, start_empty: bool) -> Result { let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir()); - let mut assets = Self::from_binary_unlinked(); + let mut assets = if start_empty { + Self::empty() + } else { + Self::from_binary_unlinked() + }; let theme_dir = source_dir.join("themes"); diff --git a/src/main.rs b/src/main.rs index 3d882b75..1ec440c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,9 @@ fn run() -> Result { let source_dir = cache_matches.value_of("source").map(Path::new); let target_dir = cache_matches.value_of("target").map(Path::new); - let assets = HighlightingAssets::from_files(source_dir)?; + let blank = cache_matches.is_present("blank"); + + let assets = HighlightingAssets::from_files(source_dir, blank)?; assets.save(target_dir)?; } else if cache_matches.is_present("clear") { clear_assets();