Skip to content

Commit

Permalink
Disable clap color on blind terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSensor committed Sep 11, 2019
1 parent efc2b6d commit 2885f7a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ name = "mask"
path = "src/lib.rs"

[dependencies]
# see https://rust-lang-nursery.github.io/cli-wg/in-depth/machine-communication.html?highlight=atty#whos-reading-this
atty = "0.2" # https://github.com/softprops/atty
colored = "1.8.0" # https://github.com/mackwic/colored
pulldown-cmark = { version = "0.5", default-features = false } # https://github.com/raphlinus/pulldown-cmark

Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;
use std::path::Path;

use atty::{self, Stream};
use clap::{
crate_authors, crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgMatches,
SubCommand,
Expand All @@ -14,7 +15,11 @@ fn main() {
let cli_app = App::new(crate_name!())
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::SubcommandRequired)
.setting(AppSettings::ColoredHelp)
.setting(if atty::is(Stream::Stdout) {
AppSettings::ColoredHelp
} else {
AppSettings::ColorNever // in case it run on terminal without color support
})
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
Expand Down Expand Up @@ -90,7 +95,13 @@ fn build_subcommands<'a, 'b>(
subcommands: &'a Vec<Command>,
) -> App<'a, 'b> {
for c in subcommands {
let mut subcmd = SubCommand::with_name(&c.name).about(c.desc.as_ref()).setting(AppSettings::ColoredHelp);
let mut subcmd = SubCommand::with_name(&c.name)
.about(c.desc.as_ref())
.setting(if atty::is(Stream::Stdout) {
AppSettings::ColoredHelp
} else {
AppSettings::ColorNever // in case it run on terminal without color support
});
if !c.subcommands.is_empty() {
subcmd = build_subcommands(subcmd, &c.subcommands);
// If this parent command has no script source, require a subcommand.
Expand Down

0 comments on commit 2885f7a

Please sign in to comment.