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 colored help #30

Merged
merged 4 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 36 additions & 0 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ name = "mask"
path = "src/lib.rs"

[dependencies]
clap = "2.33.0" # https://github.com/clap-rs/clap
# 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

[dependencies.clap] # https://github.com/clap-rs/clap
version = "2.33.0"
features = ["wrap_help", "color"]
DrSensor marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove "color" here? It's a default feature so we don't need to specify it :)

Suggested change
features = ["wrap_help", "color"]
features = ["wrap_help"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 702afad 😋


[dev-dependencies]
assert_cmd = "0.11.1" # https://github.com/assert-rs/assert_cmd
assert_fs = "0.11.3" # https://github.com/assert-rs/assert_fs
Expand Down
14 changes: 13 additions & 1 deletion 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,6 +15,11 @@ fn main() {
let cli_app = App::new(crate_name!())
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::SubcommandRequired)
.setting(if atty::is(Stream::Stdout) {
AppSettings::ColoredHelp
} else {
AppSettings::ColorNever // in case it run on terminal without color support
})
DrSensor marked this conversation as resolved.
Show resolved Hide resolved
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
Expand Down Expand Up @@ -89,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());
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
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing a few weeks ago, i thought the ColoredHelp setting was inherited from the root cli_app? We actually need to do this here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately yes. Seems like most of AppSettings::* in clap doesn't propagate to Subcommands (or in another word, it doesn't applied to nested App). Probably it propagate to Subcommands only if the setting prefixed with *Subcommands (e.g VersionlessSubcommands) 🤔

example if ColoredHelp only applied to root cli_app

Screenshot_20190918_021758

if !c.subcommands.is_empty() {
subcmd = build_subcommands(subcmd, &c.subcommands);
// If this parent command has no script source, require a subcommand.
Expand Down