我知道color bf命令设置了整个命令行窗口的颜色,但我想打印不同颜色的单行。
当前回答
我想用不同的颜色打印一行。
使用ANSI转义序列。
Windows 10之前-在控制台上不支持ANSI颜色
对于低于10的Windows版本,Windows命令控制台默认情况下不支持输出着色。您可以安装Cmder, ConEmu, ANSICON或Mintty(在GitBash和Cygwin中默认使用)来为Windows命令控制台添加着色支持。
Windows 10 -命令行颜色
从Windows 10开始,Windows控制台默认支持ANSI转义序列和一些颜色。该功能于2015年11月随Threshold 2更新一起发布。
MSDN文档
更新(05-2019):ColorTool允许您更改控制台的配色方案。这是微软终端项目的一部分。
Demo
批处理命令
win10colors。cmd是由Michele Locati写的:
下面的文本去掉了特殊字符,不能正常工作。你必须从这里复制。
@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.
echo [101;93m NORMAL FOREGROUND COLORS [0m
echo ^<ESC^>[30m [30mBlack[0m (black)
echo ^<ESC^>[31m [31mRed[0m
echo ^<ESC^>[32m [32mGreen[0m
echo ^<ESC^>[33m [33mYellow[0m
echo ^<ESC^>[34m [34mBlue[0m
echo ^<ESC^>[35m [35mMagenta[0m
echo ^<ESC^>[36m [36mCyan[0m
echo ^<ESC^>[37m [37mWhite[0m
echo.
echo [101;93m NORMAL BACKGROUND COLORS [0m
echo ^<ESC^>[40m [40mBlack[0m
echo ^<ESC^>[41m [41mRed[0m
echo ^<ESC^>[42m [42mGreen[0m
echo ^<ESC^>[43m [43mYellow[0m
echo ^<ESC^>[44m [44mBlue[0m
echo ^<ESC^>[45m [45mMagenta[0m
echo ^<ESC^>[46m [46mCyan[0m
echo ^<ESC^>[47m [47mWhite[0m (white)
echo.
echo [101;93m STRONG FOREGROUND COLORS [0m
echo ^<ESC^>[90m [90mWhite[0m
echo ^<ESC^>[91m [91mRed[0m
echo ^<ESC^>[92m [92mGreen[0m
echo ^<ESC^>[93m [93mYellow[0m
echo ^<ESC^>[94m [94mBlue[0m
echo ^<ESC^>[95m [95mMagenta[0m
echo ^<ESC^>[96m [96mCyan[0m
echo ^<ESC^>[97m [97mWhite[0m
echo.
echo [101;93m STRONG BACKGROUND COLORS [0m
echo ^<ESC^>[100m [100mBlack[0m
echo ^<ESC^>[101m [101mRed[0m
echo ^<ESC^>[102m [102mGreen[0m
echo ^<ESC^>[103m [103mYellow[0m
echo ^<ESC^>[104m [104mBlue[0m
echo ^<ESC^>[105m [105mMagenta[0m
echo ^<ESC^>[106m [106mCyan[0m
echo ^<ESC^>[107m [107mWhite[0m
echo.
echo [101;93m COMBINATIONS [0m
echo ^<ESC^>[31m [31mred foreground color[0m
echo ^<ESC^>[7m [7minverse foreground ^<-^> background[0m
echo ^<ESC^>[7;31m [7;31minverse red foreground color[0m
echo ^<ESC^>[7m and nested ^<ESC^>[31m [7mbefore [31mnested[0m
echo ^<ESC^>[31m and nested ^<ESC^>[7m [31mbefore [7mnested[0m
其他回答
I'm adding an answer to address an issue noted in some comments above: that inline ansi color codes can misbehave when inside a FOR loop (actually, within any parenthesized block of code). The .bat code below demonstrates (1) the use of inline color codes, (2) the color failure that can occur when inline color codes are used in a FOR loop or within a parenthesized block of code, and (3) a solution to the problem. When the .bat code executes, tests 2 and 3 demonstrate the colorcode failure, and test 4 shows no failure because it implements the solution.
[编辑2020-04-07:我发现了另一个解决方案,可能比调用子例程更有效。将FINDSTR短语括在括号中,如下所示:
echo success | (findstr /R success)
ENDEDIT]
注意:根据我的(有限的)经验,颜色代码问题只有在输入被管道输送到代码块中的FINDSTR之后才会出现。这就是下面的.bat重现问题的方式。颜色代码问题可能比输送到FINDSTR之后更普遍。如果有人能解释问题的本质,如果有更好的解决方法,我会很感激。
@goto :main
:resetANSI
EXIT /B
rem The resetANSI subroutine is used to fix the colorcode
rem bug, even though it appears to do nothing.
:main
@echo off
setlocal EnableDelayedExpansion
rem Define some useful colorcode vars:
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESCchar=%%E"
set "green=%ESCchar%[92m"
set "yellow=%ESCchar%[93m"
set "magenta=%ESCchar%[95m"
set "cyan=%ESCchar%[96m"
set "white=%ESCchar%[97m"
set "black=%ESCchar%[30m"
echo %white%Test 1 is NOT in a FOR loop nor within parentheses, and color works right.
echo %yellow%[Test 1] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.%yellow%
echo %green%This is green.
echo %cyan%Test 1 completed.
echo %white%Test 2 is within parentheses, and color stops working after the pipe to FINDSTR.
( echo %yellow%[Test 2] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 2 completed.
echo %white%Test 3 is within a FOR loop, and color stops working after the pipe to FINDSTR.
for /L %%G in (3,1,3) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success'.
echo %green%This is supposed to be green.
)
echo %cyan%Test 3 completed.
echo %white%Test 4 is in a FOR loop but color works right because subroutine :resetANSI is
echo called after the pipe to FINDSTR, before the next color code is used.
for /L %%G in (4,1,4) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
call :resetANSI
echo %magenta%This is magenta and FINDSTR found and displayed 'success'.
echo %green%This is green.
)
echo %cyan%Test 4 completed.%white%
EXIT /B
我想用不同的颜色打印一行。
使用ANSI转义序列。
Windows 10之前-在控制台上不支持ANSI颜色
对于低于10的Windows版本,Windows命令控制台默认情况下不支持输出着色。您可以安装Cmder, ConEmu, ANSICON或Mintty(在GitBash和Cygwin中默认使用)来为Windows命令控制台添加着色支持。
Windows 10 -命令行颜色
从Windows 10开始,Windows控制台默认支持ANSI转义序列和一些颜色。该功能于2015年11月随Threshold 2更新一起发布。
MSDN文档
更新(05-2019):ColorTool允许您更改控制台的配色方案。这是微软终端项目的一部分。
Demo
批处理命令
win10colors。cmd是由Michele Locati写的:
下面的文本去掉了特殊字符,不能正常工作。你必须从这里复制。
@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.
echo [101;93m NORMAL FOREGROUND COLORS [0m
echo ^<ESC^>[30m [30mBlack[0m (black)
echo ^<ESC^>[31m [31mRed[0m
echo ^<ESC^>[32m [32mGreen[0m
echo ^<ESC^>[33m [33mYellow[0m
echo ^<ESC^>[34m [34mBlue[0m
echo ^<ESC^>[35m [35mMagenta[0m
echo ^<ESC^>[36m [36mCyan[0m
echo ^<ESC^>[37m [37mWhite[0m
echo.
echo [101;93m NORMAL BACKGROUND COLORS [0m
echo ^<ESC^>[40m [40mBlack[0m
echo ^<ESC^>[41m [41mRed[0m
echo ^<ESC^>[42m [42mGreen[0m
echo ^<ESC^>[43m [43mYellow[0m
echo ^<ESC^>[44m [44mBlue[0m
echo ^<ESC^>[45m [45mMagenta[0m
echo ^<ESC^>[46m [46mCyan[0m
echo ^<ESC^>[47m [47mWhite[0m (white)
echo.
echo [101;93m STRONG FOREGROUND COLORS [0m
echo ^<ESC^>[90m [90mWhite[0m
echo ^<ESC^>[91m [91mRed[0m
echo ^<ESC^>[92m [92mGreen[0m
echo ^<ESC^>[93m [93mYellow[0m
echo ^<ESC^>[94m [94mBlue[0m
echo ^<ESC^>[95m [95mMagenta[0m
echo ^<ESC^>[96m [96mCyan[0m
echo ^<ESC^>[97m [97mWhite[0m
echo.
echo [101;93m STRONG BACKGROUND COLORS [0m
echo ^<ESC^>[100m [100mBlack[0m
echo ^<ESC^>[101m [101mRed[0m
echo ^<ESC^>[102m [102mGreen[0m
echo ^<ESC^>[103m [103mYellow[0m
echo ^<ESC^>[104m [104mBlue[0m
echo ^<ESC^>[105m [105mMagenta[0m
echo ^<ESC^>[106m [106mCyan[0m
echo ^<ESC^>[107m [107mWhite[0m
echo.
echo [101;93m COMBINATIONS [0m
echo ^<ESC^>[31m [31mred foreground color[0m
echo ^<ESC^>[7m [7minverse foreground ^<-^> background[0m
echo ^<ESC^>[7;31m [7;31minverse red foreground color[0m
echo ^<ESC^>[7m and nested ^<ESC^>[31m [7mbefore [31mnested[0m
echo ^<ESC^>[31m and nested ^<ESC^>[7m [31mbefore [7mnested[0m
我对cmd中缺乏适当的着色感到恼火,所以我继续创建cmdcolor。它只是一个标准输出代理,它寻找一组有限的ANSI/VT100控制序列(换句话说,就像在bash中),即echo \033[31m RED \033[0m DEFAULT | cmdcolor.exe。
使用和下载。
一个高级宏处理光标颜色,位置和属性的Windows 10。 有关使用方法的信息,请参阅帮助和使用示例。
支持和显示的例子:
Cursor Positioning Absolute Relative to last Cursor Position ; left right by n columns ; up down by n lines Combinations of Relative and Absolute Position. Show / Hide Cursor Cursor Graphics properties [ Color ; Foreground and Background ] Same line multicolor output Easily chain multiple VT graphics sequences. Clearing of all text on a line from a given position. Deletion of a number of characters to the right of the cursor on the current line. Optionally Save the Cursor position at the time of expansion as independent Y and X values. /Save Cursor position storing component of the macro adpated from Jeb's answer here NEW: Switching between Screen buffers.
Edit: I've included below the final usage example a command line that uses VT codes to achieve the same result as that example, to illustrate the difference in readability when using multiple Terminal sequences in the same Cursor output. NOTES On changing Buffers: Cursor position is tied to the active buffer; It is not availale when switching to an alternate buffer. When reverting to the main buffer: The cursor position originally occupied in the main buffer is restored, and the content of the alternate buffer is discarded.
::: Cout cursor Macro. Author: T3RRY ::: Filename: Cout.bat
::: OS requirement: Windows 10
::: Purpose: Facilitate advanced console display output with the easy use of Virtual terminal codes
::: Uses a macro function to effect display without users needing to memorise or learn specific
::: virtual terminal sequences.
::: Enables output of text in 255 bit color at absolute or relative Y;X positions.
::: Allows cursor to be hidden or shown during and after text output. See help for more info.
@Echo off & Setlocal EnableExtensions
============================================== :# Usage
If not "%~1" == "" Echo/%~1.|findstr /LIC:"/?" > nul && (
If "%~2" == "" (Cls & Mode 1000,50 & Color 30)
If "%~2" == "Usage" ( Color 04 & ( Echo/n|choice /n /C:o 2> nul ) & timeout /T 5 > nul )
If "%~2" == "DE" ( Color 04 & Echo/ --- Delayed expansion detected^^^! Must not be enabled prior to calling %~n0 ---&( Echo/n|choice /n /C:o 2> nul ))
If not Exist "%TEMP%\%~n0helpfile.~tmp" (For /F "Delims=" %%G in ('Type "%~f0"^| Findstr.exe /BLIC:":::" 2^> nul ')Do (
For /F "Tokens=2* Delims=[]" %%v in ("%%G")Do Echo(^|%%v^|
))>"%TEMP%\%~n0helpfile.~tmp"
Type "%TEMP%\%~n0helpfile.~tmp" | More
timeout /T 60 > nul
Color 07
If "%~2" == "DE" (Exit)Else Exit /B 1
)
If "!![" == "[" Call "%~f0" "/?" "DE"
:::[=====================================================================================================================]
:::[ cout /? ]
:::[ %COUT% Cursor output macro. ]
:::[ * Valid Args for COUT: {/Y:Argvalue} {/X:Argvalue} {/S:Argvalue} {/C:Argvalue} ]
:::[ - Args Must be encased in curly braces. Arg order does not matter ; Each Arg is optional. ]
:::[ * Valid Switches for COUT: /Save /Alt /Main ]
:::[ /Save - Stores the Y and X position at the start of the current expansion to .lY and .lX variables ]
:::[ /Alt - Switch console to alternate screen Buffer. Persists until /Main switch is used. ]
:::[ /Main - Restore console to main screen Buffer. Console default is the main buffer. ]
:::[ ]
:::[ USAGE: ]
:::[ * ArgValue Options ; '#' is an integer: ]
:::[ {/Y:up|down|#} {/Y:up#|down#|#} {/Y:#up|#down|#} {/X:left|right|#} {/X:left#|right#|#} {/X:#left|#right|#} ]
:::[ * note: {/Y:option} {/X:option} - 1 option only per Arg. ]
:::[ - directions: 'up' 'down' 'left' 'right' are relative to the cursors last position. ]
:::[ - /Y and /X options - #direction or direction#: ]
:::[ Positions the cursor a number of cells from the current position in the given direction. ]
:::[ Example; To move the cursor 5 rows up in the same column, without displaying any new text: ]
:::[ %COUT%{/Y:5up} ]
:::[ - '#' (Absolute position) is the column number {/X:#} or row number {/Y:#} the cursor ]
:::[ * Integers for absolute positions contained in variables must be Expanded: {/Y:%varname%} ]
:::[ is to be positioned at, allowing cursor position to be set on single or multiple axis. ]
:::[ * Absolute Y and X positions capped at line and column maximum of the console display. ]
:::[ * Exceeding the maximum Y positions the cursor at the start of the last line in the console display. ]
:::[ * Exceeding the maximum X positions the cursor at the start of the next line ]
:::[ ]
:::[ {/S:Output String} {/S:(-)Output String} {/S:Output String(+)} {/S:Output String(K)} {/S:Output String(.#.)} ]
:::[ * note: (-) Hide or (+) Show the Cursor during output of the string. ]
:::[ (K) Clears the row of text from the position (K) occurs. ]
:::[ Example; Delete 5 characters from the current row to the right of the curser: ]
:::[ %COUT%{/S:(.5.)} ]
:::[ {/C:VTcode} {/C:VTcode-VTcode} {/C:VTcode-VTcode-VTcode} ]
:::[ * note: Chain multiple graphics rendition codes using '-' ]
:::[ See: https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#text-formatting ]
:::[ See also: https://www.rapidtables.com/web/color/RGB_Color.html ]
:::[=====================================================================================================================]
============================================== :# PreScript variable definitions
rem /* generate Vitual Terminal Escape Control .Character */
For /F %%a in ( 'Echo prompt $E ^| cmd' )Do Set "\E=%%a"
rem /* https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences */
(Set \n=^^^
%= Newline variable for macro definitions. DO NOT MODIFY this line or above 2 lines. =%)
================== :# Screen Dimensions [Customise columns,lines using the mode command.]
Mode 160,38 & Cls
rem /* Get screen dimensions [lines] [columns]. Must be done before delayed expansion is enabled. */
For /F "tokens=1,2 Delims=:" %%G in ('Mode')Do For %%b in (%%H)Do For %%a in (%%G)Do Set "%%a=%%b"
rem /* NON ENGLISH VERSION USERS: You will need to manually set Columns and lines for their desired console size */
If not defined columns (Set "columns=100"& Set "lines=30")
rem /* Cursor position codes - https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#simple-cursor-positioning */
Set "left=D"&Set "right=C"&Set "up=A"&set "down=B"
For /L %%n in (1 1 %lines%)Do (Set "%%ndown=[%%nB"&Set "down%%n=[%%nB"& set "%%nup=[%%nA"&Set "up%%n=[%%nA")
For /L %%n in (1 1 %columns%)Do (Set "%%nleft=[%%nD"&Set "left%%n=[%%nD"&set "%%nright=[%%nC"&set "right%%n=[%%nC")
%= Catch Args =%Set COUT=For %%n in (1 2)Do If %%n==2 ( %\n%
%= Test No Args =%If "!Args!" == "" (CLS^&Echo/Usage Error. Args Required. ^& Call "%~f0" "/?" "Usage" ^|^| Exit /B 1) %\n%
%= Test Braces Used =%If "!Args:}=!" == "!Args!" (CLS^&Echo/Usage Error. Args must be enclosed in curly braces ^& Call "%~f0" "/?" "Usage" ^|^| Exit /B 1) %\n%
%= Reset macro =%Set ".Y=" ^& Set ".X=" ^& Set ".Str=" ^& Set ".C=" %\n%
%= internal vars =%Set "Arg1=" ^& Set "Arg2=" ^& Set "Arg3=" ^& Set "Arg4=" %\n%
%= Split Args. =%For /F "Tokens=1,2,3,4 Delims={}" %%1 in ("!Args!")Do ( %\n%
%= Substring =%Set "Arg1=%%~1" %\n%
%= modification =%Set "Arg2=%%~2" %\n%
%= identifies Args =%Set "Arg3=%%~3" %\n%
%= during handling. =%Set "Arg4=%%~4" %\n%
%= =%) %\n%
%= Check /Save switch =%If not "!Args:/Save=!" == "!Args!" (%\n%
%= Reset Cursor Save =%Set ".Cpos=" ^&Set ".Char="%\n%
%= 10 char max; Repeat =%For /L %%l in (2 1 12)Do (%\n%
%= until R returned =%If not "!.Char!" == "R" (%\n%
%= from esc[6n =%^<nul set /p "=%\E%[6n" %\n%
%= Redirects to =%FOR /L %%z in (1 1 %%l) DO pause ^< CON ^> NUL%\n%
%= prevent blocking =%Set ".Char=;"%\n%
%= script execution =%for /F "tokens=1 skip=1 delims=*" %%C in ('"REPLACE /W ? . < con"') DO (Set ".Char=%%C")%\n%
%= Append string w.out R =%If "!.Cpos!" == "" (Set ".Cpos=!.Char!")Else (set ".Cpos=!.Cpos!!.Char:R=!") %\n%
%= =%)%\n%
%= =%)%\n%
%= Split Captured Pos =%For /F "tokens=1,2 Delims=;" %%X in ("!.Cpos!")Do Set ".lY=%%X" ^& Set ".LX=%%Y" %\n%
%= End of Pos /Save =%)%\n%
%= Begin Arg =%For %%i in (1 2 3 4)Do For %%S in (Y X C S)Do If not "!Arg%%i!" == "" ( %\n%
%= Processing. 4 Args =%If not "!Arg%%i:/%%S:=!" == "!Arg%%i!" ( %\n%
%= Flagged with Y X C S =%Set "Arg%%i=!Arg%%i:/%%S:=!" %\n%
%= Strip /Flag In Arg# =%For %%v in ("!Arg%%i!")Do ( %\n%
%= /Y Lines Arg handling =%If "%%S" == "Y" ( %\n%
%= Test if arg is variable =%If Not "!%%~v!" == "" ( %\n%
%= assign down / up value =%Set ".Y=%\E%!%%~v!" %\n%
%= -OR- =%)Else ( %\n%
%= assign using operation =%Set /A ".Y=!Arg%%i!" %\n%
%= to allow use of offsets; =%If !.Y! GEQ !Lines! (Set /A ".Y=Lines-1") %\n%
%= constrained to console =%Set ".Y=%\E%[!.Y!d" %\n%
%= maximum lines. =%)) %\n%
%= /X Cols Arg handling =%If "%%S" == "X" ( %\n%
%= processing follows same =%If Not "!%%~v!" == "" ( %\n%
%= logic as /Y; =%Set ".X=%\E%!%%~v!" %\n%
%= except if Columns =%)Else ( %\n%
%= exceed console max =%Set /A ".X=!Arg%%i!" %\n%
%= columns line wrapping =%If !.X! GEQ !Columns! (Set ".X=1"^& Set ".Y=%\E%!Down!") %\n%
%= is effected. =%Set ".X=%\E%[!.X!G" %\n%
%= =%)) %\n%
%= /C Color Arg Handling. %If "%%S" == "C" ( %\n%
%= Substituition =%Set ".C=%\E%[!Arg%%i!" %\n%
%= replaces '-' with VT =%Set ".C=!.C:-=m%\E%[!" %\n%
%= chain - m\E[ =%Set ".C=!.C!m" %\n%
%= =%) %\n%
%= /S String Arg Handle =%If "%%S" == "S" ( %\n%
%= Substitute Sub-Args =%Set ".Str=!Arg%%i!" %\n%
%= (-) hide cursor =%Set ".Str=!.Str:(-)=%\E%[?25l!" %\n%
%= (+) show cursor =%Set ".Str=!.Str:(+)=%\E%[?25h!" %\n%
%= (K) clear line =%Set ".Str=!.Str:(K)=%\E%[K!" %\n%
%= (.#.) delete # of =%Set ".Str=!.Str:(.=%\E%[!" %\n%
%= characters =%Set ".Str=!.Str:.)=P!" %\n%
%= =%) %\n%
%= End Arg Handling =%))) %\n%
%= /Main /Alt Switch =%If not "!Args:/Main=!" == "!Args!" ( %\n%
%= handling for =%^< nul Set /P "=%\E%[?1049l!.Y!!.X!!.C!!.Str!%\E%[0m" %\n%
%= switching console =%)Else If not "!Args:/Alt=!" == "!Args!" ( %\n%
%= buffers. No Switch =%^< nul Set /P "=%\E%[?1049h!.Y!!.X!!.C!!.Str!%\E%[0m" %\n%
%= outputs to current =%)Else ( ^< nul Set /P "=!.Y!!.X!!.C!!.Str!%\E%[0m" ) %\n%
%= buffer. =%)Else Set Args=
rem /* Simple subsecond delay macro. Uses call to a non existentent label # number of times to delay script execution. */
For /F "tokens=1,2 delims==" %%G in ('wmic cpu get maxclockspeed /format:value')Do Set /A "%%G=%%H/20" 2> nul
If not defined Maxclockspeed Set "Maxclockspeed=200"
Set "Hash=#"& Set "delay=(If "!Hash!" == "#" (Set /A "Delay.len=Maxclockspeed")Else Set "Delay.len=#")& For /L %%i in (1 1 !Delay.Len!)Do call :[_false-label_] 2> Nul"
============================================== :# Script Body [Demo]
rem /* Enable Delayed Expansion after macro definiton in order to expand macro. */
Setlocal EnableDelayedExpansion & CD "%TEMP%"
rem /* Usage examples */
%COUT%{/X:10}{/Y:5}{/C:34}{"/S:(-)hello there^^^!"}
%Delay%
rem /* Example use of mixed foreground / background color and other graphics rendition properties */
%COUT%{"/C:31-1-4-48;2;0;80;130"}{/S:Bye for now.}{/Y:down}
%Delay%
%COUT%{/Y:up}{/C:35}{/S:again}{/X:16}
%Delay%
%COUT%{"/S:(K)^_^"}{/X:right}{/C:32}{/Y:down} /Save
%Delay%
rem /* Switch to Alternate screen buffer: /Alt */
%COUT%{"/S:(-)(K)o_o"}{/X:.lX+1}{/Y:6}{/C:33}{/Y:down} /Alt
%Delay%
%COUT%{"/S:Don't worry, they'll be back"}{/Y:down}{/X:15left}{/C:7-31}
rem /* Cursor position is tied to the active console buffer. The contents of the Alternate buffer are discarded when reverting to the Main buffer. */
%Delay%
rem /* Return to Main screen buffer: /Main */
%COUT%{/X:3left}{/Y:5up}{"/S:That's all folks."} /Save /Main
rem /* Cursor position is tied to the active console buffer. */
%Delay%
rem /* restore cursor position /Save .lX value with +7 offset ; Overwrite all and delete 6 following characters:(.6.) ; restore cursor: (+) */
%COUT%{/X:10left}{/S:How(.6.)(+)}{/C:32}
rem /* The same as the above line using VT codes manually. */
::: <nul Set /P "=%\E%[10D%\E%[32mHow%\E%[6P%\E%[?25l"
%Delay%
%COUT%{/Y:100}
Endlocal
Goto :eof
上面这个宏的另一个版本使用了一个更简单、可读性更好的结构来处理参数,可以在这里找到。
自Windows XP以来,通过使用PowerShell作为通过命名管道链接到控制台输出的子进程,可以快速有效地用cmd批处理着色。这也可以用FindStr来完成,但是PowerShell提供了更多的选项,而且似乎更快。
保持PowerShell为子进程,使用管道进行通信的主要好处是,显示要比为每一行显示启动PowerShell或FindStr快得多。
其他优点:
不需要临时文件 通过管道进行回显允许显示完整的ASCII表而不需要转义。 与fd重定向工作良好。以stderr为例,或者重定向到一个文件/其他进程。
下面是一个示例代码:
::
:: Launch a PowerShell child process in the background linked to the console and
:: earing through named pipe PowerShellCon_%PID%
::
:: Parameters :
:: [ PID ] : Console Process ID used as an identifier for the named pipe, launcher PID by default.
:: [ timeout ] : Subprocess max life in seconds, 300 by default. If -1, the subprocess
:: will not terminate while the process %PID% is still alive.
:: Return :
:: 0 if the child PowerShell has been successfully launched and the named pipe is available.
:: 1 if it fails.
:: 2 if we can't get a PID.
:: 3 if PowerShell is not present or doesn't work.
::
:LaunchPowerShellSubProcess
SET LOCALV_PID=
SET LOCALV_TIMEOUT=300
IF NOT "%~1" == "" SET LOCALV_PID=%~1
IF NOT "%~2" == "" SET LOCALV_TIMEOUT=%~2
powershell -command "$_" 2>&1 >NUL
IF NOT "!ERRORLEVEL!" == "0" EXIT /B 3
IF "!LOCALV_PID!" == "" (
FOR /F %%P IN ('powershell -command "$parentId=(Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId; write-host (Get-WmiObject Win32_Process -Filter ProcessId=$parentId).ParentProcessId;"') DO (
SET LOCALV_PID=%%P
)
)
IF "!LOCALV_PID!" == "" EXIT /B 2
START /B powershell -command "$cmdPID=$PID; Start-Job -ArgumentList $cmdPID -ScriptBlock { $ProcessActive = $true; $timeout=!LOCALV_TIMEOUT!; while((!LOCALV_TIMEOUT! -eq -1 -or $timeout -gt 0) -and $ProcessActive) { Start-Sleep -s 1; $timeout-=1; $ProcessActive = Get-Process -id !LOCALV_PID! -ErrorAction SilentlyContinue; } if ($timeout -eq 0 -or ^! $ProcessActive) { Stop-Process -Id $args; } } | Out-Null ; $npipeServer = new-object System.IO.Pipes.NamedPipeServerStream('PowerShellCon_!LOCALV_PID!', [System.IO.Pipes.PipeDirection]::In); Try { $npipeServer.WaitForConnection(); $pipeReader = new-object System.IO.StreamReader($npipeServer); while(($msg = $pipeReader.ReadLine()) -notmatch 'QUIT') { $disp='write-host '+$msg+';'; invoke-expression($disp); $npipeServer.Disconnect(); $npipeServer.WaitForConnection(); }; } Finally { $npipeServer.Dispose(); }" 2>NUL
SET /A LOCALV_TRY=20 >NUL
:LaunchPowerShellSubProcess_WaitForPipe
powershell -nop -c "& {sleep -m 50}"
SET /A LOCALV_TRY=!LOCALV_TRY! - 1 >NUL
IF NOT "!LOCALV_TRY!" == "0" cmd /C "ECHO -NoNewLine|MORE 1>\\.\pipe\PowerShellCon_!LOCALV_PID!" 2>NUL || GOTO:LaunchPowerShellSubProcess_WaitForPipe
IF "!LOCALV_TRY!" == "0" EXIT /B 1
EXIT /B 0
这个“代码”是用延迟展开ON编写的,但可以重写为没有它的情况下工作。有很多安全要点需要考虑,不要在野外直接使用。
如何使用:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 (
ECHO Extension inapplicable
EXIT /B 1
)
::
SETLOCAL ENABLEDELAYEDEXPANSION
IF ERRORLEVEL 1 (
ECHO Expansion inapplicable
EXIT /B 1
)
CALL:LaunchPowerShellSubProcess
IF NOT ERRORLEVEL 0 EXIT /B 1
CALL:Color Cyan "I write this in Cyan"
CALL:Blue "I write this in Blue"
CALL:Green "And this in green"
CALL:Red -nonewline "And mix Red"
CALL:Yellow "with Yellow"
CALL:Green "And not need to trouble with ()<>&|;,%""^ and so on..."
EXIT /B 0
:Color
ECHO -foregroundcolor %*>\\.\pipe\PowerShellCon_!LOCALV_PID!
ECHO[|SET /P=>NUL
GOTO:EOF
:Blue
ECHO -foregroundcolor Blue %*>\\.\pipe\PowerShellCon_!LOCALV_PID!
ECHO[|SET /P=>NUL
GOTO:EOF
:Green
ECHO -foregroundcolor Green %*>\\.\pipe\PowerShellCon_!LOCALV_PID!
ECHO[|SET /P=>NUL
GOTO:EOF
:Red
ECHO -foregroundcolor Red %*>\\.\pipe\PowerShellCon_!LOCALV_PID!
ECHO[|SET /P=>NUL
GOTO:EOF
:Yellow
ECHO -foregroundcolor Yellow %*>\\.\pipe\PowerShellCon_!LOCALV_PID!
ECHO[|SET /P=>NUL
GOTO:EOF
::
:: Launch a PowerShell child process in the background linked to the console and
:: earing through named pipe PowerShellCon_%PID%
::
:: Parameters :
:: [ PID ] : Console Process ID used as an identifier for the named pipe, launcher PID by default.
:: [ timeout ] : Subprocess max life in seconds, 300 by default. If -1, the subprocess
:: will not terminate while the process %PID% is still alive.
:: Return :
:: 0 if the child PowerShell has been successfully launched and the named pipe is available.
:: 1 if it fails.
:: 2 if we can't get a PID.
:: 3 if PowerShell is not present or doesn't work.
::
:LaunchPowerShellSubProcess
SET LOCALV_PID=
SET LOCALV_TIMEOUT=300
IF NOT "%~1" == "" SET LOCALV_PID=%~1
IF NOT "%~2" == "" SET LOCALV_TIMEOUT=%~2
powershell -command "$_" 2>&1 >NUL
IF NOT "!ERRORLEVEL!" == "0" EXIT /B 3
IF "!LOCALV_PID!" == "" (
FOR /F %%P IN ('powershell -command "$parentId=(Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId; write-host (Get-WmiObject Win32_Process -Filter ProcessId=$parentId).ParentProcessId;"') DO (
SET LOCALV_PID=%%P
)
)
IF "!LOCALV_PID!" == "" EXIT /B 2
START /B powershell -command "$cmdPID=$PID; Start-Job -ArgumentList $cmdPID -ScriptBlock { $ProcessActive = $true; $timeout=!LOCALV_TIMEOUT!; while((!LOCALV_TIMEOUT! -eq -1 -or $timeout -gt 0) -and $ProcessActive) { Start-Sleep -s 1; $timeout-=1; $ProcessActive = Get-Process -id !LOCALV_PID! -ErrorAction SilentlyContinue; } if ($timeout -eq 0 -or ^! $ProcessActive) { Stop-Process -Id $args; } } | Out-Null ; $npipeServer = new-object System.IO.Pipes.NamedPipeServerStream('PowerShellCon_!LOCALV_PID!', [System.IO.Pipes.PipeDirection]::In); Try { $npipeServer.WaitForConnection(); $pipeReader = new-object System.IO.StreamReader($npipeServer); while(($msg = $pipeReader.ReadLine()) -notmatch 'QUIT') { $disp='write-host '+$msg+';'; invoke-expression($disp); $npipeServer.Disconnect(); $npipeServer.WaitForConnection(); }; } Finally { $npipeServer.Dispose(); }" 2>NUL
SET /A LOCALV_TRY=20 >NUL
:LaunchPowerShellSubProcess_WaitForPipe
powershell -nop -c "& {sleep -m 50}"
SET /A LOCALV_TRY=!LOCALV_TRY! - 1 >NUL
IF NOT "!LOCALV_TRY!" == "0" cmd /C "ECHO -NoNewLine|MORE 1>\\.\pipe\PowerShellCon_!LOCALV_PID!" 2>NUL || GOTO:LaunchPowerShellSubProcess_WaitForPipe
IF "!LOCALV_TRY!" == "0" EXIT /B 1
EXIT /B 0
链接到我在同一主题上的原始答案。
推荐文章
- Android-Facebook应用程序的键散列
- 如何在PowerShell中输出一些东西
- 从matplotlib中的颜色映射中获取单个颜色
- 更改UITextField和UITextView光标/插入符颜色
- 如何在命令提示符中使用空格?
- 在Python中如何在Linux和Windows中使用“/”(目录分隔符)?
- 命令行从操作系统级配置中删除环境变量
- 在特定的文件夹中打开Cygwin
- 命令行svn for Windows?
- Gulp命令未找到-安装Gulp后错误
- 如何找到并运行keytool
- 我的Windows应用程序的图标应该包括哪些大小?
- 如何使用xargs复制名称中有空格和引号的文件?
- 在Windows上设置Python simpleHTTPserver
- 如何从批处理文件运行PowerShell脚本