on master help sign in
Improve cache subcommand

* Remove the old `init-cache` subcommand * Introduce a new `cache` subcommand that can be used like this: * `bat cache -h` - Show help * `bat cache --init` - Initialize cache from config dir * `bat cache --clear` - Reset the cache * `bat cache --config-dir` - Show config directory * Update README closes #44

commitef7c39b159e920d30badf69a4c869e8527432d9a authorsharkdp <davidpeter@web.de> dateTue 8 May 2018, 19:48 (9 years ago) parent8a399c8
2 files changed+89−34
README.md+13−5 src/main.rs+76−29
README.md+13−5
@@ -64,8 +64,10 @@ To build your own language-set and theme, follow these steps:
6464Create a folder with a syntax highlighting theme:
6565
6666``` bash
67−mkdir -p ~/.config/bat/themes
68−cd ~/.config/bat/themes
67+BAT_CONFIG_DIR="$(bat cache --config-dir)"
68+
69+mkdir -p "$BAT_CONFIG_DIR/themes"
70+cd "$BAT_CONFIG_DIR/themes"
6971
7072# Download a theme, for example:
7173git clone https://github.com/jonschlinkert/sublime-monokai-extended
@@ -77,8 +79,8 @@ ln -s "sublime-monokai-extended/Monokai Extended.tmTheme" Default.tmTheme
7779Create a folder with language definition files:
7880
7981``` bash
80−mkdir -p ~/.config/bat/syntax
81−cd ~/.config/bat/syntax
82+mkdir -p "$BAT_CONFIG_DIR/syntax"
83+cd "$BAT_CONFIG_DIR/syntax"
8284
8385# Download some language definition files, for example:
8486git clone https://github.com/sublimehq/Packages/
@@ -90,5 +92,11 @@ Finally, use the following command to parse all these files into a binary
9092cache:
9193
9294``` bash
93−bat init-cache
95+bat cache --init
96+```
97+
98+If you ever want to go back to the default settings, call:
99+
100+``` bash
101+bat cache --clear
94102```
src/main.rs+76−29
@@ -24,13 +24,13 @@ use std::collections::HashMap;
2424use std::env;
2525use std::fs::{self, File};
2626use std::io::{self, BufRead, BufReader, Write};
27−use std::path::Path;
27+use std::path::{Path, PathBuf};
2828use std::process::{self, Child, Command, Stdio};
2929
3030use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
3131use ansi_term::Style;
3232use atty::Stream;
33−use clap::{App, AppSettings, Arg, SubCommand};
33+use clap::{App, AppSettings, Arg, ArgGroup, SubCommand};
3434use directories::ProjectDirs;
3535use git2::{DiffOptions, IntoCString, Repository};
3636
@@ -279,6 +279,14 @@ struct HighlightingAssets {
279279}
280280
281281impl HighlightingAssets {
282+ fn theme_set_path() -> PathBuf {
283+ PROJECT_DIRS.cache_dir().join("theme_set")
284+ }
285+
286+ fn syntax_set_path() -> PathBuf {
287+ PROJECT_DIRS.cache_dir().join("syntax_set")
288+ }
289+
282290 fn from_files() -> Result<Self> {
283291 let config_dir = PROJECT_DIRS.config_dir();
284292
@@ -307,45 +315,46 @@ impl HighlightingAssets {
307315
308316 fn save(&self) -> Result<()> {
309317 let cache_dir = PROJECT_DIRS.cache_dir();
310− let theme_set_path = cache_dir.join("theme_set");
311− let syntax_set_path = cache_dir.join("syntax_set");
312−
313318 let _ = fs::create_dir(cache_dir);
314319
315− dump_to_file(&self.theme_set, &theme_set_path).map_err(|_| {
320+ print!(
321+ "Writing theme set to {} ... ",
322+ Self::theme_set_path().to_string_lossy()
323+ );
324+ dump_to_file(&self.theme_set, &Self::theme_set_path()).map_err(|_| {
316325 io::Error::new(
317326 io::ErrorKind::Other,
318327 format!(
319328 "Could not save theme set to {}",
320− theme_set_path.to_string_lossy()
329+ Self::theme_set_path().to_string_lossy()
321330 ),
322331 )
323332 })?;
324− println!("Wrote theme set to {}", theme_set_path.to_string_lossy());
333+ println!("okay");
325334
326− dump_to_file(&self.syntax_set, &syntax_set_path).map_err(|_| {
335+ print!(
336+ "Writing syntax set to {} ... ",
337+ Self::syntax_set_path().to_string_lossy()
338+ );
339+ dump_to_file(&self.syntax_set, &Self::syntax_set_path()).map_err(|_| {
327340 io::Error::new(
328341 io::ErrorKind::Other,
329342 format!(
330343 "Could not save syntax set to {}",
331− syntax_set_path.to_string_lossy()
344+ Self::syntax_set_path().to_string_lossy()
332345 ),
333346 )
334347 })?;
335− println!("Wrote syntax set to {}", syntax_set_path.to_string_lossy());
348+ println!("okay");
336349
337350 Ok(())
338351 }
339352
340353 fn from_cache() -> Result<Self> {
341− let cache_dir = PROJECT_DIRS.cache_dir();
342− let theme_set_path = cache_dir.join("theme_set");
343− let syntax_set_path = cache_dir.join("syntax_set");
344−
345− let syntax_set_file = File::open(&syntax_set_path).chain_err(|| {
354+ let syntax_set_file = File::open(&Self::syntax_set_path()).chain_err(|| {
346355 format!(
347356 "Could not load cached syntax set '{}'",
348− syntax_set_path.to_string_lossy()
357+ Self::syntax_set_path().to_string_lossy()
349358 )
350359 })?;
351360 let mut syntax_set: SyntaxSet = from_reader(syntax_set_file).map_err(|_| {
@@ -356,10 +365,10 @@ impl HighlightingAssets {
356365 })?;
357366 syntax_set.link_syntaxes();
358367
359− let theme_set_file = File::open(&theme_set_path).chain_err(|| {
368+ let theme_set_file = File::open(&Self::theme_set_path()).chain_err(|| {
360369 format!(
361370 "Could not load cached theme set '{}'",
362− theme_set_path.to_string_lossy()
371+ Self::theme_set_path().to_string_lossy()
363372 )
364373 })?;
365374 let theme_set: ThemeSet = from_reader(theme_set_file).map_err(|_| {
@@ -398,11 +407,14 @@ fn run() -> Result<()> {
398407
399408 let app_matches = App::new(crate_name!())
400409 .version(crate_version!())
401− .setting(clap_color_setting)
402− .setting(AppSettings::DeriveDisplayOrder)
403− .setting(AppSettings::UnifiedHelpMessage)
404− .setting(AppSettings::NextLineHelp)
405− .setting(AppSettings::DisableVersion)
410+ .global_setting(clap_color_setting)
411+ .global_setting(AppSettings::DeriveDisplayOrder)
412+ .global_setting(AppSettings::UnifiedHelpMessage)
413+ .global_setting(AppSettings::NextLineHelp)
414+ .setting(AppSettings::InferSubcommands)
415+ .setting(AppSettings::ArgsNegateSubcommands)
416+ .setting(AppSettings::DisableHelpSubcommand)
417+ .setting(AppSettings::VersionlessSubcommands)
406418 .max_term_width(90)
407419 .about(crate_description!())
408420 .arg(
@@ -442,17 +454,52 @@ fn run() -> Result<()> {
442454 .help("When to use the pager"),
443455 )
444456 .subcommand(
445− SubCommand::with_name("init-cache")
446− .about("Load syntax definitions and themes into cache"),
457+ SubCommand::with_name("cache")
458+ .about("Modify the syntax-definition and theme cache")
459+ .arg(
460+ Arg::with_name("init")
461+ .long("init")
462+ .short("i")
463+ .help("Initialize the cache by loading from the config dir"),
464+ )
465+ .arg(
466+ Arg::with_name("clear")
467+ .long("clear")
468+ .short("c")
469+ .help("Reset the cache"),
470+ )
471+ .arg(
472+ Arg::with_name("config-dir")
473+ .long("config-dir")
474+ .short("d")
475+ .help("Show the configuration directory"),
476+ )
477+ .group(
478+ ArgGroup::with_name("cache-actions")
479+ .args(&["init", "clear", "config-dir"])
480+ .required(true),
481+ ),
447482 )
448483 .help_message("Print this help message.")
449484 .version_message("Show version information.")
450485 .get_matches();
451486
452487 match app_matches.subcommand() {
453− ("init-cache", Some(_)) => {
454− let assets = HighlightingAssets::from_files()?;
455− assets.save()?;
488+ ("cache", Some(cache_matches)) => {
489+ if cache_matches.is_present("init") {
490+ let assets = HighlightingAssets::from_files()?;
491+ assets.save()?;
492+ } else if cache_matches.is_present("clear") {
493+ print!("Clearing theme set cache ... ");
494+ fs::remove_file(HighlightingAssets::theme_set_path())?;
495+ println!("okay");
496+
497+ print!("Clearing syntax set cache ... ");
498+ fs::remove_file(HighlightingAssets::syntax_set_path())?;
499+ println!("okay");
500+ } else if cache_matches.is_present("config-dir") {
501+ println!("{}", PROJECT_DIRS.config_dir().to_string_lossy());
502+ }
456503 }
457504 _ => {
458505 let files: Vec<Option<&str>> = app_matches