我知道color bf命令设置了整个命令行窗口的颜色,但我想打印不同颜色的单行。


当前回答

您可以用要打印的单词的名称创建文件,使用findstr(可以彩色打印),然后擦除该文件。试试这个例子:

@echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

call :ColorText 0a "green"
call :ColorText 0C "red"
call :ColorText 0b "cyan"
echo(
call :ColorText 19 "blue"
call :ColorText 2F "white"
call :ColorText 4e "yellow"

goto :eof

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof

运行color /?得到一个颜色列表。

其他回答

Windows 10 - TH2及以上版本:

(a.k.a Version 1511, build 10586, release 2015-11-10)

在命令提示符:

echo ^[[32m HI ^[[0m

使用实际按键:echo Ctrl+[[32m HI Ctrl+[[0mEnter .

您应该在它下面看到一个绿色的“HI”。

代码可以在这里找到:

https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

记事本:

要保存到记事本,你可以用Alt+027和numpad键入ESC,然后[32m部分。当我使用笔记本电脑时,另一个技巧是将上面的行重定向到一个文件中,然后剪切和粘贴:

echo echo ^[[32m HI ^[[0m >> batch_file.cmd

您可以使用ANSICON在旧版本的Windows中启用ANSI终端代码。我在Windows XP和Windows 7中使用的是32位和64位版本。

Solution for changing the foreground and background colors and writing without new lines. It does not create any temporary files. No special editors are required, so Notepad can be used for editing. The first parameter for the :color subroutine is the color code, the rest of the (optional) parameters are the text to display. If the last parameter is $ then a new line is written at the end. The color codes are the same as for the color command. The :echo subroutine can be used to display a text without new line (unlike regular echo).

@echo off
call :color 4
call :echo Red foreground
call :color 7 " and "
call :color 4f
echo Red background

call :color 
echo Back to normal

call :color 70 "Black "
call :color 1 "Blue "
call :color 2 "Green "
call :color 3 "Aqua "
call :color 4 "Red "
call :color 5 "Purple "
call :color 6 "Yellow "
call :color 7 "White "
call :color 8 "Gray "
call :color 9 "LightBlue" $
call :color a "LightGreen "
call :color b "LightAqua "
call :color c "LightRed "
call :color d "LightPurple "
call :color e "LightYellow "
call :color f "BrightWhite " $

call :color 1f Blue back
call :color 2f Green back
call :color 3f Aqua back
call :color 4f Red back
call :color 5f Purple back
call :color 6f Yellow back
call :color 7f White back
call :color 8f Gray back
call :color 9f "LightBlue back" $
call :color a0 LightGreen back
call :color b0 LightAqua back
call :color c0 LightRed back
call :color d0 LightPurple back
call :color e0 LightYellow back
call :color f0 LightWhite back $

call :color
echo %ESC%[4mUnderline%ESC%[0m.
pause 


goto :eof

:: Displays a text without new line at the end (unlike echo)
:echo
@<nul set /p ="%*"
@goto :eof

:: Change color to the first parameter (same codes as for the color command) 
:: And display the other parameters (write $ at the end for new line)
:color
@echo off
IF [%ESC%] == [] for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
SET color=0%1
IF [%color%] == [0] SET color=07
SET fore=%color:~-1%
SET back=%color:~-2,1% 
SET color=%ESC%[
if %fore% LEQ 7 (
  if %fore% == 0 SET color=%ESC%[30
  if %fore% == 1 SET color=%ESC%[34
  if %fore% == 2 SET color=%ESC%[32
  if %fore% == 3 SET color=%ESC%[36
  if %fore% == 4 SET color=%ESC%[31
  if %fore% == 5 SET color=%ESC%[35
  if %fore% == 6 SET color=%ESC%[33
  if %fore% == 7 SET color=%ESC%[37
) ELSE (
  if %fore% == 8 SET color=%ESC%[90
  if %fore% == 9  SET color=%ESC%[94
  if /i %fore% == a SET color=%ESC%[92
  if /i %fore% == b SET color=%ESC%[96
  if /i %fore% == c SET color=%ESC%[91
  if /i %fore% == d SET color=%ESC%[95
  if /i %fore% == e SET color=%ESC%[93
  if /i %fore% == f SET color=%ESC%[97
)
if %back% == 0 (SET color=%color%;40) ELSE (
  if %back% == 1 SET color=%color%;44
  if %back% == 2 SET color=%color%;42
  if %back% == 3 SET color=%color%;46
  if %back% == 4 SET color=%color%;41
  if %back% == 5 SET color=%color%;45
  if %back% == 6 SET color=%color%;43
  if %back% == 7 SET color=%color%;47
  if %back% == 8 SET color=%color%;100
  if %back% == 9  SET color=%color%;104
  if /i %back% == a SET color=%color%;102
  if /i %back% == b SET color=%color%;106
  if /i %back% == c SET color=%color%;101
  if /i %back% == d SET color=%color%;105
  if /i %back% == e SET color=%color%;103
  if /i %back% == f SET color=%color%;107
)
SET color=%color%m
:repeatcolor
if [%2] NEQ [$] SET color=%color%%~2
shift
if [%2] NEQ [] if [%2] NEQ [$] SET color=%color% & goto :repeatcolor
if [%2] EQU [$] (echo %color%) else (<nul set /p ="%color%")
goto :eof

最简单的方法是像这样对powershell进行系统调用:

s=os.system('powershell Write-Host "I am so bored with this. Work already" -ForegroundColor Blue')

否则:

←[94mPff

另一种选择是使用NodeJS。

这里有一个例子:

const os = require('os');
const colors = require('colors');

console.log("Operative System:".green,os.type(),os.release());
console.log("Uptime:".blue,os.uptime());

这就是结果: