10个显示彩色字符的批处理代码

一卷旌收千骑虏,万全身出百重围。这篇文章主要讲述10个显示彩色字符的批处理代码相关的知识,希望能为你提供帮助。
??上接第二篇??
代码11:使用ColorTool应用配色方案
ColorTool是微软官方创建的一个设置Windows控制台调色板的工具,给单调的命令行带来了丰富的彩色文本效果,并且可以载入配置文件里定义的配色方案。
默认情况下,将指定的 .itermcolors、.json或.ini文件中的颜色应用于当前控制台窗口,这文件 保存至当前目录下的 \\schemes目录之下。里面几种现成的方案作为例子,当然你也可以把自己喜欢的方案放入其中。
官方主页:??https://github.com/Microsoft/Terminal/tree/main/src/tools/ColorTool,??????????需要安装  .NET 4.0。?
用法:
    ColorTool.exe < Function>
    ColorTool.exe [Options] < Scheme name>
参数
< Function> : 下面"Functions"一节种所列出的选项,只能出现一个。    
< Scheme name> : 配色方案文件名。ColorTool首先加载 .ini文件,如果失败就加载 .json文件,如果再失败就加载.itermcolors文件。
本参数必须放在ColorTool命令行的最后。?
  [Option]: 下面“选项”部分中列出的一个或多个开关。 必须出现在方案名称之前。
Functions:
每次调用ColorTool时,你只能指定以下任何一个开关当中的一个。
-?, --help : 显示帮助信息
-c, --current : 打印当前方案的颜色表
-v, --version:显示版本号
-l, --location :显示方案目录的完整路径
-s, --schemes : 显示所有可用的方案
-o, --output < filename> : 将当前的颜色表输出到一个.ini格式的文件中。
Options:
以下开关写在配色方案文件名之前。
-q, --quiet : 应用后不打印颜色表。
-e, --errors:在控制台报告方案解析错误
-d, --defaults : 仅将方案应用于注册表中的默认值。默认情况下,该方案应用于当前控制台。
-b, --both : 将方案同时应用于当前控制台和默认值。 默认情况下,该方案只应用于当前控制台。
-x, --xterm : 使用VT序列设置颜色。用于在WSL中设置颜色。只适用于Windows版本> =17048。
-t, --terminal : 以JSON格式输出颜色,以便复制到Windows终端设置文件中。
在schemes目录之下有2个重要的配色方案:
cmd-legacy.ini是2017年7月之前的Windows控制台传统配色方案;
campbell是Windows 控制台主机使用的新默认方案。
除此之外还有其它一些.ini 格式和 .itermcolors格式的方案。
我们推荐优秀的?? iTerm2-Color-Schemes??,这里有大量的配色方案和预览图片。同时还可以使用??terminal.sexyerminal.sexy??轻松直观地编辑配色方案。导出方案使用导入和导出选项卡,格式为...itermcolorsiTerm2。
举例: colortool onehalflight.itermcolors
echo ^[[46; 1; 37m

?代码12:使用  PROMPT命令输出彩色字符
之前的几篇文章,在使用批处理输出色彩控制符的时候,都是使用一种技巧:按ALT+27
?
但很多编辑器不接受以这种方式输入字符(例如MS Code),你只有使用第三方的软件来编辑批处理,例如Notpad++。
其实,使用PROMPT命令可以在无需第三方软件的情况下改编控制台的调色板,只需将ASCII转义符code 27替换为$e。用批处理的编程技巧也能输出特定的彩色字符。
??promp命令官方说明。??

@ECHO OFF

:: Do not pollute environment with the %prompt.bak% variable
:: ! forgetting ENDLOCAL at the end of the batch leads to prompt corruption
SETLOCAL

:: Old prompt settings backup
SET prompt.bak=%PROMPT%

:: Entering the "ECHO"-like section

:: Forcing prompt to display after every command (see below)
ECHO ON

:: Setting the prompt using the ANSI Escape sequence(s)
:: - Always start with $E[1A, otherwise the text would appear on a next line
:: - Then the decorated text follows
:: - And it all ends with $E30; 40m, which makes the following command invisible
::- assuming default background color of the screen
@ PROMPT $E[1A$E[30; 42mHELLO$E[30; 40m

:: An "empty" command that forces the prompt to display.
:: The word "rem" is displayed along with the prompt text but is made invisible
rem

:: Just another text to display
@ PROMPT $E[1A$E[33; 41mWORLD$E[30; 40m
rem

:: Leaving the "ECHO"-like section
@ECHO OFF

:: Or a more readable version utilizing the cursor manipulation ASCII ESC sequences

:: the initial sequence
PROMPT $E[1A
:: formating commands
PROMPT %PROMPT%$E[32; 44m
:: the text
PROMPT %PROMPT%This is an "ECHO"ed text...
:: new line; 2000 is to move to the left "a lot"
PROMPT %PROMPT%$E[1B$E[2000D
:: formating commands fro the next line
PROMPT %PROMPT%$E[33; 47m
:: the text (new line)
PROMPT %PROMPT%...spreading over two lines
:: the closing sequence
PROMPT %PROMPT%$E[30; 40m

:: Looks like this without the intermediate comments:
:: PROMPT $E[1A
:: PROMPT %PROMPT%$E[32; 44m
:: PROMPT %PROMPT%This is an "ECHO"ed text...
:: PROMPT %PROMPT%$E[1B$E[2000D
:: PROMPT %PROMPT%$E[33; 47m
:: PROMPT %PROMPT%...spreading over two lines
:: PROMPT %PROMPT%$E[30; 40m

:: show it all at once!
ECHO ON


【10个显示彩色字符的批处理代码】


    推荐阅读