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
Comment #1
moshe weitzman commentedSeems reasonable. Would be good to get Owen's input.
Comment #2
moshe weitzman commentedOr anyone's input. I'm not much of a bash wiz.
Comment #3
greg.1.anderson commented-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.
Comment #4
patrick2000 commented@dbt
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
Comment #5
greg.1.anderson commentedShould this patch be backed out?
As was pointed out in #740448: tput errors when running "drush" script in cron,
drush ... | lesswill 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.
Comment #6
greg.1.anderson commented#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.