Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add -q, --quiet flag to surpress non-error output #48

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Possible log types:
- `[fixed]` for any bug fixes.
- `[security]` to invite users to upgrade in case of vulnerabilities.

### Unreleased

- [added] New --quiet / -q option to suppress most non-error messages

### v1.0.0 (2018-02-11)

Expand Down
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ xdg = "2.1.0"
[dev-dependencies]
tempdir = "^0.3"
assert_cli = "^0.5"
utime = "0.2.0"

[features]
dev = ["clippy"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ These are the clients I tried but failed to compile or run:
-o --os <type> Override the operating system [linux, osx, sunos]
-u --update Update the local cache
-c --clear-cache Clear the local cache
-q --quiet Suppress informational messages
--config-path Show config file path
--seed-config Create a basic config

Expand Down
2 changes: 1 addition & 1 deletion bash_tealdeer
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _tealdeer()
_init_completion || return

case $prev in
-h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|--config-path|--seed-config)
-h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|--config-path|--seed-config|-q|--quiet)
return
;;
-f|--render)
Expand Down
19 changes: 14 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Options:
-o --os <type> Override the operating system [linux, osx, sunos]
-u --update Update the local cache
-c --clear-cache Clear the local cache
-q --quiet Suppress informational messages
--config-path Show config file path
--seed-config Create a basic config

Expand Down Expand Up @@ -105,6 +106,7 @@ struct Args {
flag_os: Option<OsType>,
flag_update: bool,
flag_clear_cache: bool,
flag_quiet: bool,
flag_config_path: bool,
flag_seed_config: bool,
}
Expand Down Expand Up @@ -142,6 +144,7 @@ fn check_cache(args: &Args, cache: &Cache) {
if !args.flag_update {
match cache.last_update() {
Some(ago) if ago > MAX_CACHE_AGE => {
if args.flag_quiet { return; }
println!("{}", Color::Red.paint(format!(
"Cache wasn't updated in {} days.\n\
You should probably run `tldr --update` soon.",
Expand Down Expand Up @@ -207,7 +210,9 @@ fn main() {
};
process::exit(1);
});
println!("Successfully deleted cache.");
if !args.flag_quiet {
println!("Successfully deleted cache.");
}
}

// Update cache, pass through
Expand All @@ -219,7 +224,9 @@ fn main() {
};
process::exit(1);
});
println!("Successfully updated cache.");
if !args.flag_quiet {
println!("Successfully updated cache.");
}
}

// Render local file and exit
Expand Down Expand Up @@ -266,9 +273,11 @@ fn main() {
process::exit(0);
}
} else {
println!("Page {} not found in cache", &command);
println!("Try updating with `tldr --update`, or submit a pull request to:");
eprintln!("https://github.com/tldr-pages/tldr");
if !args.flag_quiet {
println!("Page {} not found in cache", &command);
println!("Try updating with `tldr --update`, or submit a pull request to:");
println!("https://github.com/tldr-pages/tldr");
}
process::exit(1);
}
}
Expand Down
66 changes: 66 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

extern crate assert_cli;
extern crate tempdir;
extern crate utime;

use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -67,6 +68,71 @@ fn test_update_cache() {
.unwrap();
}

#[test]
fn test_quiet_cache() {
let testenv = TestEnv::new();
testenv
.assert()
.with_args(&["--update", "--quiet"])
.succeeds()
.stdout().is("")
.unwrap();

testenv
.assert()
.with_args(&["--clear-cache", "--quiet"])
.succeeds()
.stdout().is("")
.unwrap();
}

#[test]
fn test_quiet_failures() {
let testenv = TestEnv::new();

testenv
.assert()
.with_args(&["--update", "-q"])
.succeeds()
.stdout().is("")
.unwrap();

testenv
.assert()
.with_args(&["fakeprogram", "-q"])
.fails()
.stdout().is("")
.unwrap();
}

#[test]
fn test_quiet_old_cache() {
let testenv = TestEnv::new();

testenv
.assert()
.with_args(&["--update", "-q"])
.succeeds()
.stdout().is("")
.unwrap();

let _ = utime::set_file_times(testenv.cache_dir.path().join("tldr-master"), 1, 1).unwrap();

testenv
.assert()
.with_args(&["tldr"])
.succeeds()
.stdout().contains("Cache wasn't updated in ")
.unwrap();

testenv
.assert()
.with_args(&["tldr", "--quiet"])
.succeeds()
.stdout().doesnt_contain("Cache wasn't updated in ")
.unwrap();
}

#[test]
fn test_setup_seed_config() {
let testenv = TestEnv::new();
Expand Down