mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-26 09:43:51 +01:00
Can read files named "cache" now, cache subcommand working too (#275)
Now if a cache file exists in the current directory, and the user passes no arguments to the cache command, the cache file would be displayed. If however the user uses cache command with arguments, the cache command would be executed as normal regardless of whether the file cache exists in the current directory or not. Though now there won't be an error message displayed if the user uses the cache sub command without arguments in any directory that contains a file named cache.
This commit is contained in:
parent
155179a07a
commit
53d0c1deca
@ -1,5 +1,6 @@
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
use atty::{self, Stream};
|
||||
|
||||
@ -102,6 +103,10 @@ impl App {
|
||||
AppSettings::ColorNever
|
||||
};
|
||||
|
||||
// Check if the current directory contains a file name cache, if it does
|
||||
// do not make the arguements for subcommand 'cache' required.
|
||||
let arg_group_required = !Path::new("cache").exists();
|
||||
|
||||
ClapApp::new(crate_name!())
|
||||
.version(crate_version!())
|
||||
.global_setting(clap_color_setting)
|
||||
@ -303,7 +308,7 @@ impl App {
|
||||
).group(
|
||||
ArgGroup::with_name("cache-actions")
|
||||
.args(&["init", "clear", "config-dir"])
|
||||
.required(true),
|
||||
.required(arg_group_required),
|
||||
).arg(
|
||||
Arg::with_name("source")
|
||||
.long("source")
|
||||
|
30
src/main.rs
30
src/main.rs
@ -87,7 +87,8 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn list_languages(assets: &HighlightingAssets, config: &Config) -> Result<()> {
|
||||
pub fn list_languages(config: &Config) -> Result<()> {
|
||||
let assets = HighlightingAssets::new();
|
||||
let mut languages = assets
|
||||
.syntax_set
|
||||
.syntaxes()
|
||||
@ -143,7 +144,8 @@ pub fn list_languages(assets: &HighlightingAssets, config: &Config) -> Result<()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
|
||||
pub fn list_themes(cfg: &Config) -> Result<()> {
|
||||
let assets = HighlightingAssets::new();
|
||||
let themes = &assets.theme_set.themes;
|
||||
let mut config = cfg.clone();
|
||||
let mut style = HashSet::new();
|
||||
@ -174,6 +176,12 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_controller(config: &Config) -> Result<bool> {
|
||||
let assets = HighlightingAssets::new();
|
||||
let controller = Controller::new(&config, &assets);
|
||||
controller.run()
|
||||
}
|
||||
|
||||
/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
|
||||
/// `Some(false)` if any intermediate errors occurred (were printed).
|
||||
fn run() -> Result<bool> {
|
||||
@ -181,24 +189,32 @@ fn run() -> Result<bool> {
|
||||
|
||||
match app.matches.subcommand() {
|
||||
("cache", Some(cache_matches)) => {
|
||||
// If there is a file named 'cache' in the current working directory,
|
||||
// arguments for subcommand 'cache' are not mandatory.
|
||||
// If there are non-zero arguments, execute the subcommand cache, else, open the file cache.
|
||||
if !cache_matches.args.is_empty() {
|
||||
run_cache_subcommand(cache_matches)?;
|
||||
Ok(true)
|
||||
} else {
|
||||
let mut config = app.config()?;
|
||||
config.files = vec![InputFile::Ordinary(&"cache")];
|
||||
|
||||
run_controller(&config)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let config = app.config()?;
|
||||
let assets = HighlightingAssets::new();
|
||||
|
||||
if app.matches.is_present("list-languages") {
|
||||
list_languages(&assets, &config)?;
|
||||
list_languages(&config)?;
|
||||
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("list-themes") {
|
||||
list_themes(&assets, &config)?;
|
||||
list_themes(&config)?;
|
||||
|
||||
Ok(true)
|
||||
} else {
|
||||
let controller = Controller::new(&config, &assets);
|
||||
controller.run()
|
||||
run_controller(&config)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user