diff --git a/src/app.rs b/src/app.rs index 112548c6..0e61cc7d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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") diff --git a/src/main.rs b/src/main.rs index 1c56d757..ff87f9b8 100644 --- a/src/main.rs +++ b/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 { + 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 { @@ -181,24 +189,32 @@ fn run() -> Result { match app.matches.subcommand() { ("cache", Some(cache_matches)) => { - run_cache_subcommand(cache_matches)?; - Ok(true) + // 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) } } }