Ripgrep: --color never not supported for error output

Created on 9 Feb 2017  路  11Comments  路  Source: BurntSushi/ripgrep

My shell does not support colours and consequently I set the environmental variable TERM=dumb to instruct programs never to colourise output.

However, even when I pass --color never to rg, the error output includes colour escape codes as can be seen below:

% rg --color never -asd
error: Found argument '-d' which wasn't expected, or isn't valid in this context

USAGE:

    rg [OPTIONS] <pattern> [<path> ...]
    rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...]
    rg [OPTIONS] --files [<path> ...]
    rg [OPTIONS] --type-list

For more information try --help
bug help wanted

All 11 comments

@andreastt that's from claps (rg's CLI parser) error message, not rg's.

I think there's a hacky way to workaround this...but it's hacky. You'd essentially be parsing the CLI twice in the case of a CLI error and then double checking that --color never. An abbriviated version is something like:

let mut app = build_cli();
let args: Args = match app.get_matches_safe() {
    Err(e) => {
        if (env::args().find(|arg| arg == "--color").is_some() && env::arg().find(|arg| arg == "never")) || env::args().find(|arg| arg == "--color=never") {
            app.setting(AppSettings::ColorNever).get_matches();
        } 
        e.exit();
    },
    Ok(m) => m.into(),
};

There's probably a far better way to do all that, but I'm going a lack of sleep and 8 plane rides 馃槣

@kbknapp Yeah, probably the "right" way here is for clap to recognize TERM=dumb, which should mean "no colors." (It's pretty standard in my experience, and termcolor will Do The Right Thing.)

@andreastt if you wouldn't mind, could you file an issue in claps repo too? This is something that I'd like to at least attempt to address on that side of things. Actually if you could just post a comment in kbknapp/clap-rs#836 as a reminder to me, that's fine!

@BurntSushi yep, once I get some time to sit down and actually implement kbknapp/clap-rs#836 that's my exact plan :wink:

@kbknapp While you're at it, note that the atty crate now supports MSYS terminals and native Windows consoles. I've wondered whether termcolor itself should use the atty crate directly.

If someone wants to fix this using @kbknapp's hack, then I'd be open to it. It might be hard to write a regression test for it though.

Sure, I'll take a stab at it.

There's a PR for clap-rs to comply with terminfo when TERM=dumb (https://github.com/kbknapp/clap-rs/pull/963).
Anything else needs to be done? Still prefer to be colorless if --color never?

@nateozem Let's try to get away with TERM=dumb support instead of introducing a hack.

It looks like the clap PR was merged so I'm marking this as fixed. (You still can't use --color never, but TERM=dumb should be respected.)

Was this page helpful?
0 / 5 - 0 ratings