Skip to content

Commit

Permalink
Fixed issue sharkdp#245
Browse files Browse the repository at this point in the history
Now if a cache file exists in the current directory, and the use 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 wether the file cache exists
in the current directory or not.
  • Loading branch information
BK1603 committed Sep 4, 2018
1 parent 532fb92 commit 2d8286a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashSet;
use std::env;
use std::path::Path;

use atty::{self, Stream};

Expand All @@ -9,7 +10,6 @@ use console::Term;

#[cfg(windows)]
use ansi_term;

use assets::BAT_THEME_DEFAULT;
use errors::*;
use line_range::LineRange;
Expand Down Expand Up @@ -102,6 +102,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)
Expand All @@ -125,8 +129,7 @@ impl App {
to read from standard input.",
).multiple(true)
.empty_values(false),
)
.arg(
).arg(
Arg::with_name("language")
.short("l")
.long("language")
Expand Down Expand Up @@ -303,7 +306,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")
Expand Down Expand Up @@ -433,4 +436,4 @@ impl App {
},
))
}
}
}
17 changes: 15 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,21 @@ fn run() -> Result<bool> {

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()?;
let assets = HighlightingAssets::new();

config.files = vec![InputFile::Ordinary(&"cache")];

let controller = Controller::new(&config, &assets);
controller.run()
}
}
_ => {
let config = app.config()?;
Expand Down

0 comments on commit 2d8286a

Please sign in to comment.