无法用不同的颜色回应批处理文件

无法用不同的颜色回应批处理文件

当我在 VS 代码上运行批处理文件时,颜色不会出现并显示原始代码。

echo off
echo %ESC%[42mCan support unscramble your logs?%ESC%[0m

当我手动运行代码时,它不会显示颜色,也不会显示原始代码。

echo %ESC%[42mCan support unscramble your logs?%ESC%[0m

在此处输入图片描述

有什么想法吗?我在 Windows 10 上,没有管理员权限。

昨天我尝试在 VS 代码上运行这个批处理文件,颜色发生了变化。

但是对于 cmd.exe,它仍然将显示原始代码而没有任何颜色。

提到了多个问题,例如 https://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line

答案1

您需要定义ESC环境变量:

for /F %a in ('echo prompt $E ^| cmd') do @set "ESC=%a"

然后您就可以使用它,例如如下:

echo ^<ESC^>[4m %ESC%[4mUnderline Underline%ESC%[0m
echo ^<ESC^>[32m %ESC%[32mGreen  Green  Green%ESC%[0m
echo ^<ESC^>[33m %ESC%[33mYellow Yellow Yellow%ESC%[0m

结果:

在此处输入图片描述

使用以下代码片段从批处理脚本进行测试:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
for /F %%a in ('echo prompt $E ^| cmd') do (
  set "ESC=%%a"
)
echo ^<ESC^>[4m %ESC%[4mUnderline DisableDelayedExpansion%ESC%[0m

SETLOCAL EnableDelayedExpansion

echo !ESC![101;93m STYLES !ESC![0m
echo ^<ESC^>[4m !ESC![4mUnderline!ESC![0m
echo ^<ESC^>[0m !ESC![0mReset!ESC![0m
echo ^<ESC^>[1m !ESC![1mBold!ESC![0m
echo ^<ESC^>[7m !ESC![7mInverse!ESC![0m
echo.
echo !ESC![101;93m NORMAL FOREGROUND COLORS !ESC![0m
echo ^<ESC^>[30m !ESC![30mBlack!ESC![0m (black)
echo ^<ESC^>[31m !ESC![31mRed!ESC![0m
echo ^<ESC^>[32m !ESC![32mGreen!ESC![0m
echo ^<ESC^>[33m !ESC![33mYellow!ESC![0m
echo ^<ESC^>[34m !ESC![34mBlue!ESC![0m
echo ^<ESC^>[35m !ESC![35mMagenta!ESC![0m
echo ^<ESC^>[36m !ESC![36mCyan!ESC![0m
echo ^<ESC^>[37m !ESC![37mWhite!ESC![0m
echo.
echo !ESC![101;93m NORMAL BACKGROUND COLORS !ESC![0m
echo ^<ESC^>[40m !ESC![40mBlack!ESC![0m
echo ^<ESC^>[41m !ESC![41mRed!ESC![0m
echo ^<ESC^>[42m !ESC![42mGreen!ESC![0m
echo ^<ESC^>[43m !ESC![43mYellow!ESC![0m
echo ^<ESC^>[44m !ESC![44mBlue!ESC![0m
echo ^<ESC^>[45m !ESC![45mMagenta!ESC![0m
echo ^<ESC^>[46m !ESC![46mCyan!ESC![0m
echo ^<ESC^>[47m !ESC![47mWhite!ESC![0m (white)
echo.
echo !ESC![101;93m STRONG FOREGROUND COLORS !ESC![0m
echo ^<ESC^>[90m !ESC![90mWhite!ESC![0m
echo ^<ESC^>[91m !ESC![91mRed!ESC![0m
echo ^<ESC^>[92m !ESC![92mGreen!ESC![0m
echo ^<ESC^>[93m !ESC![93mYellow!ESC![0m
echo ^<ESC^>[94m !ESC![94mBlue!ESC![0m
echo ^<ESC^>[95m !ESC![95mMagenta!ESC![0m
echo ^<ESC^>[96m !ESC![96mCyan!ESC![0m
echo ^<ESC^>[97m !ESC![97mWhite!ESC![0m
echo.
echo !ESC![101;93m STRONG BACKGROUND COLORS !ESC![0m
echo ^<ESC^>[100m !ESC![100mBlack!ESC![0m
echo ^<ESC^>[101m !ESC![101mRed!ESC![0m
echo ^<ESC^>[102m !ESC![102mGreen!ESC![0m
echo ^<ESC^>[103m !ESC![103mYellow!ESC![0m
echo ^<ESC^>[104m !ESC![104mBlue!ESC![0m
echo ^<ESC^>[105m !ESC![105mMagenta!ESC![0m
echo ^<ESC^>[106m !ESC![106mCyan!ESC![0m
echo ^<ESC^>[107m !ESC![107mWhite!ESC![0m
echo.
echo !ESC![101;93m COMBINATIONS !ESC![0m
echo ^<ESC^>[31m                     !ESC![31mred foreground color!ESC![0m
echo ^<ESC^>[7m                      !ESC![7minverse foreground ^<-^> background!ESC![0m
echo ^<ESC^>[7;31m                   !ESC![7;31minverse red foreground color!ESC![0m
echo ^<ESC^>[7m and nested !ESC![31m !ESC![7mbefore !ESC![31mnested!ESC![0m
echo ^<ESC^>[31m and nested !ESC![7m !ESC![31mbefore !ESC![7mnested!ESC![0m

答案2

当然,您可以通过多种方式利用 ascii 转义码进行颜色输出,包括使用 RGB 颜色方案。这里有一个这样的例子。

@Echo Off & Setlocal DisableDelayedExpansion
mode 170,40
::: { Creates variable /AE = Ascii-27 escape code.
::: - %/AE% can be used  with and without DelayedExpansion.
    For /F %%a in ('echo prompt $E ^| cmd')do set "/AE=%%a"
::: }

(Set \n=^^^
%=Newline DNR=%
)
::: / Color Print Macro -
::: Usage: %Print%{RRR;GGG;BBB}text to output
::: \n at the end of the string echo's a new line
::: valid range for RGB values: 0 - 255
  Set Print=For %%n in (1 2)Do If %%n==2 (%\n%
    For /F "Delims=" %%G in ("!Args!")Do (%\n%
      For /F "Tokens=1 Delims={}" %%i in ("%%G")Do Set "Output=%/AE%[0m%/AE%[38;2;%%im!Args:{%%~i}=!"%\n%
      ^< Nul set /P "=!Output:\n=!%/AE%[0m"%\n%
      If "!Output:~-2!"=="\n" (Echo/^&Endlocal)Else (Endlocal)%\n%
    )%\n%
  )Else Setlocal EnableDelayedExpansion ^& Set Args=
::: / Erase Macro -
::: Usage: %Erase%{string of the length to be erased}
  Set Erase=For %%n in (1 2)Do If %%n==2 (%\n%
    For /F "Tokens=1 Delims={}" %%G in ("!Args!")Do (%\n%
      Set "Nul=!Args:{%%G}=%%G!"%\n%
      For /L %%# in (0 1 100) Do (If Not "!Nul:~%%#,1!"=="" ^< Nul set /P "=%/AE%[D%/AE%[K")%\n%
    )%\n%
    Endlocal%\n%
  )Else Setlocal EnableDelayedExpansion ^& Set Args=

rem // usage example
 %Print%{150;75;50}This is a Demo
 %Print%{140;60;120} of same line multicolor output.\n
 %Print%{75;190;150}Includes End Of Line marker.
 Timeout 1 /NoBreak > Nul
 %Erase%{marker.}
 %Print%{150;150;80}marker and erase macro.\n

相关内容