在windows上为stderr更改文本输出颜色

在windows上为stderr更改文本输出颜色,windows,cmd,stderr,building,Windows,Cmd,Stderr,Building,我最近发现了一篇文章,它提供了一种解决方案,让stderr输出的文本为Linux(bash)使用不同的颜色 他们创建了以下bash脚本 #!/bin/bash { $* 2>&1>&3|sed 's,.*,\x1B[33m&\x1B[0m,'>&2;} 3>&1 这会导致输出在来自stderr时打印黄色文本。stdout仍然打印相同的颜色 脚本保存在名为color的$PATH目录中。这允许我使用make或scon运行脚本,它将以黄

我最近发现了一篇文章,它提供了一种解决方案,让stderr输出的文本为Linux(bash)使用不同的颜色

他们创建了以下bash脚本

#!/bin/bash
{ $* 2>&1>&3|sed 's,.*,\x1B[33m&\x1B[0m,'>&2;} 3>&1
这会导致输出在来自stderr时打印黄色文本。stdout仍然打印相同的颜色

脚本保存在名为color的$PATH目录中。这允许我使用make或scon运行脚本,它将以黄色显示来自stderr的所有文本。(将33m改为31m可使文字变为红色)

这对于在编译时查找错误非常有用

是否有类似的脚本可用于Windows cmd shell


注意:如果有帮助的话,我已经在我的windows计算机上安装了sed。

关于windows的cmd.exe下对ANSI转义码的支持,请参阅。将重定向逻辑转换为cmd.exe语法后,我准备了以下
color.bat
文件:

@Echo Off
(((%* 1>&3) 2>&1) | "c:\Program Files (x86)\GnuWin32\bin\sed.exe" "s,.*,\x1B[33m&\x1B[0m," 1>&2) 3>&1
不幸的是,流混合在一起(在某些行中,来自stdout和stderr的字符在一行中混合在一起)。也许这种行为取决于使用的sed.exe版本,所以请尝试一下

如果这不起作用,请考虑使用最小安装。我测试了你的

color.sh
脚本,我能够启动一个.bat文件,它在不混合流的情况下正常工作。我使用的语法是:

./color.sh cmd /c test.bat

我设计了一种使用纯批处理命令获得等效解决方案的方法,即不使用Ansi、不使用sed.exe等,只使用findstr:

any_command 2>&1 1>&3 | findstr /N /A:4E "^"
在这种情况下,stderr中的整行不使用不同的颜色,而只使用findstr提供的行号,但该行号应足以满足规定的要求。我将很快编写一个小的辅助.exe程序,用另一种颜色显示整个stderr行。

@MBu,@gnash117

我将
color
重命名为
co
,因为
color
是一个Windows命令,我将其扩展为:

:: color output - Displays stderr output in different color
::
:: Credits to MBu and gnash117 at http://stackoverflow.com/questions/10095886/change-text-output-color-on-windows-for-stderr#10118710
:: 
:: Requires:
::   - http://sourceforge.net/projects/mingw/files/MSYS/
::   - http://adoxa.altervista.org/ansicon/
::   - http://www.autohotkey.com/
::
@echo off

if "%1"=="" goto :Help

:: 1;31 ... intense red foreground (see https://github.com/adoxa/ansicon/blob/master/sequences.txt for more colors)
:: \x07 ... BEL, but doesn't work :-(
(((%* 1>&3) 2>&1) | sed "s/.*/\x07\x1B[1;31m&\x1B[0m/" 1>&2) 3>&1
goto :EOF

:Help
setlocal
::------------------------------------------------
:: Adapt these in pairs according to your likings
set invokeKeys=[Shift]+[Enter]
set invokeHotkeys=+Enter
set invokeAfterRemoveKeys=[Alt]+[Enter]
set invokeAfterRemoveHotkeys=!Enter
set removeKeys=[Alt]+[c]
set removeHotkeys=!c
::-----------------------------------------------
set invokeText=invokes the entered command after preceding it with '%0 '
set invokeAfterRemoveText=invokes the entered command after removing the first three characters from the beginning
set removeText=removes the first three characters from the command line
echo Colors a command's stderr output as defined in %~f0
echo Usage:
echo   - Preceed a command with '%0 ' (e.g.: 'co dir not.existing.file' will show the resulting error message in a different color)
echo   - If the AutoHotkey script below is active:
echo     - %invokeKeys% ... %invokeText%
echo     - %invokeAfterRemoveKeys% ... %invokeAfterRemoveText%
echo     - %removeKeys% ... %removeText%
echo(
echo       The latter two are useful when using the command line history and having used %invokeKeys% before.
echo(
echo     Enabled by AutoHotkey script:
echo(
echo       #IfWinActive ahk_class ConsoleWindowClass
echo       ; %invokeText%
echo       %invokeHotkeys%::Send {Home}%0 {Enter}
echo       ; %invokeAfterRemoveText%
echo       %invokeAfterRemoveHotkeys%::SendInput {Home}{Del 3}{Enter}
echo       ; %removeText%
echo       %removeHotkeys%::SendInput {Home}{Del 3}{End}
echo       #IfWinActive
endlocal
:EOF

谢谢,我现在不在我的计算机上,但我会尝试一下并让您知道。谢谢非常有用,因为我在路径中使用了sed,所以我不必指定完整路径,只留下@Echo Off(((((*1>&3)2>&1)|“sed.exe”s、.*、\x1B[31m&\x1B[0m,“1>&2)3>&1在我的Win7上,此命令确实会为行着色,但只输出stderr,stdout未显示。我最终将脚本命名为hilite(突出显示的拼写错误),因为我也遇到了颜色问题。不过,我非常喜欢该脚本。:)
:: color output - Displays stderr output in different color
::
:: Credits to MBu and gnash117 at http://stackoverflow.com/questions/10095886/change-text-output-color-on-windows-for-stderr#10118710
:: 
:: Requires:
::   - http://sourceforge.net/projects/mingw/files/MSYS/
::   - http://adoxa.altervista.org/ansicon/
::   - http://www.autohotkey.com/
::
@echo off

if "%1"=="" goto :Help

:: 1;31 ... intense red foreground (see https://github.com/adoxa/ansicon/blob/master/sequences.txt for more colors)
:: \x07 ... BEL, but doesn't work :-(
(((%* 1>&3) 2>&1) | sed "s/.*/\x07\x1B[1;31m&\x1B[0m/" 1>&2) 3>&1
goto :EOF

:Help
setlocal
::------------------------------------------------
:: Adapt these in pairs according to your likings
set invokeKeys=[Shift]+[Enter]
set invokeHotkeys=+Enter
set invokeAfterRemoveKeys=[Alt]+[Enter]
set invokeAfterRemoveHotkeys=!Enter
set removeKeys=[Alt]+[c]
set removeHotkeys=!c
::-----------------------------------------------
set invokeText=invokes the entered command after preceding it with '%0 '
set invokeAfterRemoveText=invokes the entered command after removing the first three characters from the beginning
set removeText=removes the first three characters from the command line
echo Colors a command's stderr output as defined in %~f0
echo Usage:
echo   - Preceed a command with '%0 ' (e.g.: 'co dir not.existing.file' will show the resulting error message in a different color)
echo   - If the AutoHotkey script below is active:
echo     - %invokeKeys% ... %invokeText%
echo     - %invokeAfterRemoveKeys% ... %invokeAfterRemoveText%
echo     - %removeKeys% ... %removeText%
echo(
echo       The latter two are useful when using the command line history and having used %invokeKeys% before.
echo(
echo     Enabled by AutoHotkey script:
echo(
echo       #IfWinActive ahk_class ConsoleWindowClass
echo       ; %invokeText%
echo       %invokeHotkeys%::Send {Home}%0 {Enter}
echo       ; %invokeAfterRemoveText%
echo       %invokeAfterRemoveHotkeys%::SendInput {Home}{Del 3}{Enter}
echo       ; %removeText%
echo       %removeHotkeys%::SendInput {Home}{Del 3}{End}
echo       #IfWinActive
endlocal
:EOF