The following shell script works but removes colored formatting generated by rspec
:
#!/bin/bash
OUTPUT=`rspec`
echo "$OUTPUT"
How to preserve the colors?
bashcolorsescape-charactersscriptingshell-script
The following shell script works but removes colored formatting generated by rspec
:
#!/bin/bash
OUTPUT=`rspec`
echo "$OUTPUT"
How to preserve the colors?
It is common for unix application that do support colour output (such as grep
and ls
) to have a command line option as well as possibly automatic detection.
looking at man ls
we see
--color[=WHEN]
colorize the output. WHEN defaults to `always' or can be `never' or `auto'. More info below
So by default ls will always use colour, notice the auto
option.
man grep
shows similar
--color[=WHEN], --colour[=WHEN]
<snipped> WHEN is never, always, or auto.
If the auto
option is enabled (which for your application could be the default) then something like the following answer (suggested by Gilles) may be what you are looking for https://unix.stackexchange.com/a/10065/4635
That is if your output is a terminal and supports colour, and the user hasn't disabled it you should use color. Of course the user using always
or never
skips this check.
I find it useful if programs have a --color=always
even if they support automatic detection, as this means that I can force colour usage 'when I know better' (say my destination isn't a terminal but I know it supports colour').
On some systems I have an alias for ls
to ls --color=always
.
Noting a comment about "Easy using awk", that would be relative to sed.
OP was describing a not-very-well documented feature of logcat
(see commit) which tells logcat
to assign hardcoded-colors to each line based on the log entry's priority.
There are other choices, such as logcat-color, PID cat and coloredlogcat which let you color different fields, e.g., raising the possibility of multiple colors per line.
Here's an awk script which recognizes the hardcoded logcat colors, but allows multiple colors per line:
#!/usr/bin/awk -f
BEGIN {
colors["0"] = "Black";
colors["38;5;40"] = "Green";
colors["38;5;196"] = "Red";
colors["38;5;75"] = "Blue";
colors["38;5;166"] = "Sienna";
colors["38;5;231"] = "DarkSlateGray";
color = "";
last = "";
this = "";
save = "";
}
/\033/ {
done = "";
while ( $0 ~ /\033\[[;0-9]*m/ ) {
mark = match($0, /\033\[[;0-9]*m/ );
if ( mark > 1 ) { done = done substr($0, 1, mark - 1); }
item = substr($0, RSTART + 2, RLENGTH - 3);
$0 = substr($0, RSTART + RLENGTH);
if ( match($0, /^\033\[[;0-9]*m/ ) > 0 ) continue;
color = colors[item];
if ( done == "" ) this = color;
if ( item == "0" ) color = "";
if ( color == "" ) {
if ( $0 != "" ) last = color;
$0 = "[/COLOR]" $0;
} else if (color != last) {
$0 = "[COLOR=\"" color "\"]" $0;
last = color;
}
}
$0 = done $0;
if ( last != "" ) $0 = $0 "[/COLOR]";
}
{
if ( NR > 1 ) {
if ( this == last) sub("\[/COLOR\]$", "", save);
print save;
}
save = $0;
}
END {
if ( NR > 0 ) print save;
}
With the original example:
^[[0m^[[38;5;231mV/Zygote ( 4666): Switching descriptor 55 to /dev/null
^[[0m^[[38;5;231mV/Zygote ( 4666): Switching descriptor 9 to /dev/null
^[[0m^[[38;5;40mI/ggheart ( 1111): onStop
^[[0m^[[38;5;40mI/Test ( 1111): onStop
^[[0m^[[38;5;75mD/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapUtilization:0.75
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapMinFree:2097152
you would get the requested output:
[COLOR="DarkSlateGray"]V/Zygote ( 4666): Switching descriptor 55 to /dev/null
V/Zygote ( 4666): Switching descriptor 9 to /dev/null
[COLOR="Green"]I/ggheart ( 1111): onStop
I/Test ( 1111): onStop
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
D/ActivityThread( 4666): setTargetHeapUtilization:0.75
D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]
but changing the input to
^[[0m^[[38;5;231mV/Zygote ( 4666): Switching descriptor 55 to /dev/null
^[[0m^[[38;5;231mV/Zygote ( 4666): Switching descriptor 9 to /dev/null
^[[0m^[[38;5;40mI/ggheart ( ^[[38;5;75mI/ggheart 1111^[[0m): onStop
^[[0m^[[38;5;40mI/Test ( 1111): onStop
^[[0m^[[38;5;75mD/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapUtilization:0.75
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapMinFree:2097152
gives this result:
[COLOR="DarkSlateGray"]V/Zygote ( 4666): Switching descriptor 55 to /dev/null
V/Zygote ( 4666): Switching descriptor 9 to /dev/null[/COLOR]
[COLOR="Green"]I/ggheart ( [COLOR="Blue"]I/ggheart 1111[/COLOR]): onStop
[COLOR="Green"]I/Test ( 1111): onStop
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
D/ActivityThread( 4666): setTargetHeapUtilization:0.75
D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]
Best Answer
It's common for programs with colorized output to disable it if they're not being run directly in a TTY, since you might be piping the output to a log file or to another process that expects plain text. Typically the programs offer a switch to manually force colors enabled, and
rspec
has one (--color
), but for some reason it ignores it if you're not running in a TTY, which is really unusual behavior.I think your only options are to edit
rspec
to take out that check (seedef color
inrspec-core-2.11.1/lib/rspec/core/configuration.rb
), or run it within a program that will trick it into thinking it has a TTY, likeexpect