on master help sign in
Support a hidden arg --no-custom-assets that skips loading assets from the cache
commitb040efff79b641ba23704e7f34655d11fb91d6e9 authorMartin Nordholts <enselic@gmail.com> dateTue 27 Jul 2021, 20:11 (5 years ago) parenta810096
7 files changed+33−6
assets/completions/bat.zsh.in+1 src/bin/bat/app.rs+1 src/bin/bat/assets.rs+7−3 src/bin/bat/clap_app.rs+6 src/bin/bat/main.rs+3−3 src/config.rs+4 tests/integration_tests.rs+11
assets/completions/bat.zsh.in+1
@@ -45,6 +45,7 @@ _{{PROJECT_EXECUTABLE}}_main() {
4545 '(-r --line-range)'{-r+,--line-range=}'[Only print the lines from N to M]:<N\:M>...'
4646 '(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]'
4747 '(: --no-config)'--no-config'[Do not use the configuration file]'
48+ '(: --no-custom-assets)'--no-custom-assets'[Do not load custom assets]'
4849 '(: --config-dir)'--config-dir'[Show bat'"'"'s configuration directory]'
4950 '(: --config-file)'--config-file'[Show path to the configuration file]'
5051 '(: --generate-config-file)'--generate-config-file'[Generates a default configuration file]'
src/bin/bat/app.rs+1
@@ -234,6 +234,7 @@ impl App {
234234 .map(LineRanges::from)
235235 .map(HighlightedLineRanges)
236236 .unwrap_or_default(),
237+ use_custom_assets: !self.matches.is_present("no-custom-assets"),
237238 })
238239 }
239240
src/bin/bat/assets.rs+7−3
@@ -23,7 +23,7 @@ pub fn clear_assets() {
2323 clear_asset("metadata.yaml", "metadata file");
2424}
2525
26−pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> {
26+pub fn assets_from_cache_or_binary(use_custom_assets: bool) -> Result<HighlightingAssets> {
2727 let cache_dir = PROJECT_DIRS.cache_dir();
2828 if let Some(metadata) = AssetsMetadata::load_from_folder(&cache_dir)? {
2929 if !metadata.is_compatible_with(crate_version!()) {
@@ -41,8 +41,12 @@ pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> {
4141 }
4242 }
4343
44− Ok(HighlightingAssets::from_cache(&cache_dir)
45− .unwrap_or_else(|_| HighlightingAssets::from_binary()))
44+ let custom_assets = if use_custom_assets {
45+ HighlightingAssets::from_cache(&cache_dir).ok()
46+ } else {
47+ None
48+ };
49+ Ok(custom_assets.unwrap_or_else(HighlightingAssets::from_binary))
4650}
4751
4852fn clear_asset(filename: &str, description: &str) {
src/bin/bat/clap_app.rs+6
@@ -450,6 +450,12 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
450450 .hidden(true)
451451 .help("Do not use the configuration file"),
452452 )
453+ .arg(
454+ Arg::with_name("no-custom-assets")
455+ .long("no-custom-assets")
456+ .hidden(true)
457+ .help("Do not load custom assets"),
458+ )
453459 .arg(
454460 Arg::with_name("config-file")
455461 .long("config-file")
src/bin/bat/main.rs+3−3
@@ -80,7 +80,7 @@ fn get_syntax_mapping_to_paths<'a>(
8080pub fn get_languages(config: &Config) -> Result<String> {
8181 let mut result: String = String::new();
8282
83− let assets = assets_from_cache_or_binary()?;
83+ let assets = assets_from_cache_or_binary(config.use_custom_assets)?;
8484 let mut languages = assets
8585 .get_syntaxes()?
8686 .iter()
@@ -178,7 +178,7 @@ fn theme_preview_file<'a>() -> Input<'a> {
178178}
179179
180180pub fn list_themes(cfg: &Config) -> Result<()> {
181− let assets = assets_from_cache_or_binary()?;
181+ let assets = assets_from_cache_or_binary(cfg.use_custom_assets)?;
182182 let mut config = cfg.clone();
183183 let mut style = HashSet::new();
184184 style.insert(StyleComponent::Plain);
@@ -219,7 +219,7 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
219219}
220220
221221fn run_controller(inputs: Vec<Input>, config: &Config) -> Result<bool> {
222− let assets = assets_from_cache_or_binary()?;
222+ let assets = assets_from_cache_or_binary(config.use_custom_assets)?;
223223 let controller = Controller::new(&config, &assets);
224224 controller.run(inputs)
225225}
src/config.rs+4
@@ -82,6 +82,10 @@ pub struct Config<'a> {
8282
8383 /// Ranges of lines which should be highlighted with a special background color
8484 pub highlighted_lines: HighlightedLineRanges,
85+
86+ /// Whether or not to allow custom assets. If this is false or if custom assets (a.k.a.
87+ /// cached assets) are not available, assets from the binary will be used instead.
88+ pub use_custom_assets: bool,
8589}
8690
8791#[cfg(all(feature = "application", feature = "paging"))]
tests/integration_tests.rs+11
@@ -795,6 +795,17 @@ fn does_not_print_unwanted_file_named_cache() {
795795 bat_with_config().arg("cach").assert().failure();
796796}
797797
798+#[test]
799+fn accepts_no_custom_assets_arg() {
800+ // Just make sure --no-custom-assets is considered a valid arg
801+ // Don't bother to actually verify that it works
802+ bat()
803+ .arg("--no-custom-assets")
804+ .arg("test.txt")
805+ .assert()
806+ .success();
807+}
808+
798809#[test]
799810fn unicode_wrap() {
800811 bat_with_config()