We are using drush in scripts and don't want it to use color characters in our logfiles. I am aware of the --nocolor argument, but IMHO it makes sense to check whether stdout is a terminal, and if not, make this option default.

This is a trivial patch to drush shell script which does this:

35a36,40
> # prepend "--nocolor" to the argument list if stdout is not a terminal
> if [ ! -t 1 ]; then
> set -- "--nocolor" ${1+"$@"}
> fi
>

What do you think?

Comments

moshe weitzman’s picture

Assigned: Unassigned » owen barton
Status: Active » Needs review

Seems reasonable. Would be good to get Owen's input.

moshe weitzman’s picture

Or anyone's input. I'm not much of a bash wiz.

greg.1.anderson’s picture

Component: Code » Base system (internal API)
Status: Needs review » Fixed

-t checks to see if the file descriptor is set and is a terminal, and of course fd 1 is stdout. The 'set' line is a bit obtuse, but since it is brief, I left it as written.

Works perfectly; committed.

patrick2000’s picture

@dbt

We are using drush in scripts ...

Sorry, dbt, I am not really in any position to make smart comments here on how you should write your scripts, but I will, nevertheless, do it.

As drush is intended to be used either interactively from the command line OR automatically, by scripts, I think the [ ! -t 1 ] test you proposed should actually be in YOUR script and not in drush.

The case of running scripts from cron is already handled by #740448: tput errors when running "drush" script in cron.

If you want to run your scripts in a terminal and still have --nocolors (probably because you are piping the output to tee /var/log/xyz.log) or something like that, I think it would be your script that should call drush with the "--nocolor" argument in that case and not drush changing its own arguments. That somehow doesn't make sense to me, since it makes drush less flexible.

You've asked - that's what I think.

... any arguments supporting the decision to put the test in drush?

Otherwise, please consider the patch in http://drupal.org/node/740448#comment-3451054

greg.1.anderson’s picture

Status: Fixed » Needs review

Should this patch be backed out?

As was pointed out in #740448: tput errors when running "drush" script in cron, drush ... | less will be devoid of color if this patch stands. To me, this is not a big deal, and the -t test above will help more often than it hurts, as you only very rarely want color when stdout is not a terminal. Still, there is no --color flag, and as #4 above points out, we could just insist that people who care put in the --nocolor flag.

I'm ambivalent.

greg.1.anderson’s picture

Status: Needs review » Closed (won't fix)

#4 won out; the currently committed code does not include this patch. Specify --nocolor if you do not want drush to include color in the output.