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:
Shreyansh Chouhan 2018-09-06 02:52:12 +05:30 committed by David Peter
parent 155179a07a
commit 53d0c1deca
2 changed files with 31 additions and 10 deletions

View File

@ -1,5 +1,6 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::env; use std::env;
use std::path::Path;
use atty::{self, Stream}; use atty::{self, Stream};
@ -102,6 +103,10 @@ impl App {
AppSettings::ColorNever 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!()) ClapApp::new(crate_name!())
.version(crate_version!()) .version(crate_version!())
.global_setting(clap_color_setting) .global_setting(clap_color_setting)
@ -303,7 +308,7 @@ impl App {
).group( ).group(
ArgGroup::with_name("cache-actions") ArgGroup::with_name("cache-actions")
.args(&["init", "clear", "config-dir"]) .args(&["init", "clear", "config-dir"])
.required(true), .required(arg_group_required),
).arg( ).arg(
Arg::with_name("source") Arg::with_name("source")
.long("source") .long("source")

View File

@ -87,7 +87,8 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
Ok(()) 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 let mut languages = assets
.syntax_set .syntax_set
.syntaxes() .syntaxes()
@ -143,7 +144,8 @@ pub fn list_languages(assets: &HighlightingAssets, config: &Config) -> Result<()
Ok(()) 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 themes = &assets.theme_set.themes;
let mut config = cfg.clone(); let mut config = cfg.clone();
let mut style = HashSet::new(); let mut style = HashSet::new();
@ -174,6 +176,12 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
Ok(()) 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 /// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
/// `Some(false)` if any intermediate errors occurred (were printed). /// `Some(false)` if any intermediate errors occurred (were printed).
fn run() -> Result<bool> { fn run() -> Result<bool> {
@ -181,24 +189,32 @@ fn run() -> Result<bool> {
match app.matches.subcommand() { match app.matches.subcommand() {
("cache", Some(cache_matches)) => { ("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)?; run_cache_subcommand(cache_matches)?;
Ok(true) Ok(true)
} else {
let mut config = app.config()?;
config.files = vec![InputFile::Ordinary(&"cache")];
run_controller(&config)
}
} }
_ => { _ => {
let config = app.config()?; let config = app.config()?;
let assets = HighlightingAssets::new();
if app.matches.is_present("list-languages") { if app.matches.is_present("list-languages") {
list_languages(&assets, &config)?; list_languages(&config)?;
Ok(true) Ok(true)
} else if app.matches.is_present("list-themes") { } else if app.matches.is_present("list-themes") {
list_themes(&assets, &config)?; list_themes(&config)?;
Ok(true) Ok(true)
} else { } else {
let controller = Controller::new(&config, &assets); run_controller(&config)
controller.run()
} }
} }
} }