17个显示彩色字符和动画的批处理代码(三)

网友投稿 1122 2022-09-16

17个显示彩色字符和动画的批处理代码(三)

17个显示彩色字符和动画的批处理代码(三)

​​上接第二篇​​

代码12:使用ColorTool应用配色方案

ColorTool是微软官方创建的一个设置Windows控制台调色板的工具,给单调的命令行带来了丰富的彩色文本效果,并且可以载入配置文件里定义的配色方案。

默认情况下,将指定的 .itermcolors、.json或.ini文件中的颜色应用于当前控制台窗口,这文件 保存至当前目录下的 \schemes目录之下。里面几种现成的方案作为例子,当然你也可以把自己喜欢的方案放入其中。

官方主页:​​​​

ColorTool.exe [Options]

参数

: 下面"Functions"一节种所列出的选项,只能出现一个。

: 配色方案文件名。ColorTool首先加载 .ini文件,如果失败就加载 .json文件,如果再失败就加载.itermcolors文件。

本参数必须放在ColorTool命令行的最后。

[Option] : 下面“选项”部分中列出的一个或多个开关。 必须出现在方案名称之前。

Functions:

每次调用ColorTool时,你只能指定以下任何一个开关当中的一个。

-?, --help : 显示帮助信息

-c, --current : 打印当前方案的颜色表

-v, --version:显示版本号

-l, --location :显示方案目录的完整路径

-s, --schemes : 显示所有可用的方案

-o, --output : 将当前的颜色表输出到一个.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

代码13:使用 PROMPT命令输出彩色字符

之前的几篇文章,在使用批处理输出色彩控制符的时候,都是使用一种技巧:按ALT+27

但很多编辑器不接受以这种方式输入字符(例如MS Code),你只有使用第三方的软件来编辑批处理,例如Notpad++。

其实,使用PROMPT命令可以在无需第三方软件的情况下改变命令提示符颜色,只需将ASCII转义符code 27替换为$e。

​​promp命令官方说明。​​

@ECHO OFF ::以ANSI编码保存批处理 ::不要再使用变量prompt.bak,因为已经用来保存当前的prompt参数 ::批处理的最后别忘了ENDLOCAL SETLOCAL :: 备份旧的prompt参数 SET prompt.bak=%PROMPT% :: 进入"ECHO"部分 :: 执行每个命令之后都显示提示符 (见下文) ECHO ON :: 使用ANSI转义序列设置提示符 :: - 始终以$E1A开头,否则文本会出现在下一行 :: - 之后是被装饰后的文字 :: - 最后以 $E30;40m结束,由于前景色与背景色都是黑色,之后输入的命令不可见 :: - 假定屏幕的默认背景颜色 @ PROMPT $E[1A$E[30;42mHELLO$E[30;40m ::一个 "空 "命令,强制显示提示符 :: "rem "一词与提示文本一起显示,但不可见 rem :: 如果要显示另一个文本 @ PROMPT $E[1A$E[33;41mWORLD$E[30;40m rem :: "ECHO"部分结束 @ECHO OFF :: 利用光标操作ASCII ESC序列的更易读的版本 :: 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

代码14:另一种方式使用 PROMPT命令输出彩色字符

批处理显示进度条。

@ECHO OFF :: ! To observe color effects on prompt below in this script :: run the script from a fresh cmd window with no custom :: prompt settings :: Only not to pollute the environment with the %\e% variable (see below) :: Not needed because of the `PROMPT` variable SETLOCAL :: Parsing the `escape` character (ASCII 27) to a %\e% variable :: Use %\e% in place of `Esc` in the [ FOR /F "delims=#" %%E IN ('"prompt #$E# & FOR %%E IN (1) DO rem"') DO SET "\e=%%E" :: Demonstrate that prompt did not get corrupted by the previous FOR ECHO ON rem : After for @ECHO OFF :: Some fancy ASCII ESC staff ECHO [ ] FOR /L %%G IN (1,1,10) DO ( TIMEOUT /T 1 > NUL ECHO %\e%[1A%\e%[%%GC%\e%[31;43m. ECHO %\e%[1A%\e%[11C%\e%[37;40m] ) :: ECHO another decorated text :: - notice the `%\e%[30C` cursor positioning sequence :: for the sake of the "After ECHO" test below ECHO %\e%[1A%\e%[13C%\e%[32;47mHELLO WORLD%\e%[30C :: Demonstrate that prompt did not get corrupted by ECHOing :: neither does the cursor positioning take effect. :: ! But the color settings do. ECHO ON rem : After ECHO @ECHO OFF ENDLOCAL :: Demonstrate that color settings do not reset :: even when out of the SETLOCAL scope ECHO ON rem : After ENDLOCAL @ECHO OFF :: Reset the `PROMPT` color :: - `PROMPT` itself is untouched so we did not need to backup it. :: - Still ECHOING in color apparently collide with user color cmd settings (if any). :: ! Resetting `PROMPT` color this way extends the `PROMPT` :: by the initial `$E[37;40m` sequence every time the script runs. :: - Better solution then would be to end every (or last) `ECHO` command :: with the `%\e%[37;40m` sequence and avoid setting `PROMPT` altogether. :: which makes this technique preferable to the previous one (before EDIT) :: - I am keeping it this way only to be able to :: demonstrate the `ECHO` color effects on the `PROMPT` above. PROMPT $E[37;40m%PROMPT% ECHO ON rem : After PROMPT color reset @ECHO OFFEXIT /B 0

代码15:适用于WIN10的批处理,直接写颜色控制码

@echo offcall :color 4call :echo Red foregroundcall :color 7 " and "call :color 4fecho Red backgroundcall :color echo Back to normalcall :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 backcall :color 2f Green backcall :color 3f Aqua backcall :color 4f Red backcall :color 5f Purple backcall :color 6f Yellow backcall :color 7f White backcall :color 8f Gray backcall :color 9f "LightBlue back" $call :color a0 LightGreen backcall :color b0 LightAqua backcall :color c0 LightRed backcall :color d0 LightPurple backcall :color e0 LightYellow backcall :color f0 LightWhite back $call :colorecho %ESC%[4mUnderline%ESC%[0m.pause goto :eof:: Displays a text without new line at the end (unlike echo):echo@

​​跳转至第四篇​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:WIN 2012R2 自动激活
下一篇:C# 数据操作系列 - 15 SqlSugar 增删改查详解(cctv5)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~