【问题标题】:How to write special characters in colored lines on batch如何批量写入彩色线条中的特殊字符
【发布时间】:2018-03-18 01:09:06
【问题描述】:

我目前正在尝试制作一个简单的选择 RPG 游戏,它会有很多对话,我希望对话以不同的颜色出现,但也共享同一个屏幕(如果你愿意的话,就像一个聊天室) ,有一段时间,我在This video找到的代码对我有很大帮助,但我很快遇到了一个问题,我似乎不能写特殊字符像!?'*^,。他们在对话中必不可少,有人知道我可以解决这个问题的方法吗?在每个字符不起作用之前使用“^”,也许更改代码会有所帮助?

@echo off
MODE 60,20
title Mage's Adventure 
color 0F
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"
)
goto :SplashScreen

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

:SplashScreen
cls
call :ColorText 0B "Welcome!, Welcome^!"
pause>nul

【问题讨论】:

标签: batch-file text colors character


【解决方案1】:

有几个解决方案。


卡洛斯的颜色 v23c

玩弄它会有一些奇怪的输出。测试使用

call :color 04 "Test poison '&|<>!()" \n
call :color 06 ""
call :color 0A " can be printed if you use empty strings" \n

生产(德国控制台):

">" kann syntaktisch an dieser Stelle nicht verarbeitet werden.

" can be printed if you use empty strings

它有时会产生一些重大错误并且有点慢......


VT100 替代 (Win10+)

@echo off
for /f "delims=" %%E in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x1B"') do (
    set "ESC=%%E["
)

call :vtEcho 93 "No escaping needed for most characters: '&|<>!()"
call :vtEcho 94 "Problems occur with '^' and '"' though"

:vtEcho   [VT100 colorcode] [message]
<NUL set /P "=%ESC%%1m%~2%ESC%0m"
echo+
exit /B 0

输出:

No escaping needed for most characters: '&|<>!()
Problems occur with '^^' and '"'

那么这是如何工作的呢?基本上使用set /P 回显几乎可以解决所有毒字符,VT 序列使用颜色代码生成颜色。我们首先使用 forfiles 生成 ESC 字符,然后在函数中使用它。但是由于我们试图将其作为单个标志提供给子例程,因此我们需要将其双引号......这意味着我们在显示它时遇到了问题。此外,set /P 只能打印单个 ^,如果您将其设为 &lt;NUL set /P^"=^^",则在任何其他情况下都会失败。

"^ 使用标志转换的解决方法:

:vtEcho   [VT100 colorcode] [message] [message] [message] ...
:: Use """" for displaying a '"'
:: Use "^" for displaying a '^'
:: Use "\n" to make a new line
<NUL set /P "=%ESC%%1m"
:__vtEcho
shift
if not "%~1" == "" (
    if "%~1" == """" (
        <NUL set /P ^"="""^"
        goto __vtEcho
    )
    if "%~1" == "^^" (
        <NUL set /P ^"=^^"
        goto __vtEcho
    )
    if "%~1" == "\n" (
        echo+
        goto __vtEcho
    )
    <NUL set /P "=%~1"
    goto __vtEcho
)
echo %ESC%0m
exit /B 0

测试:

call :vtEcho 96 "No problems with '" """" "' and '" "^" "' and has" \n "support for '\n'"`

结果:

No problems with '"' and '^' and has
support for '\n'

为了补偿"^,我们检查标志的各个部分,如果它们是错误的对应部分("" / ^^)并打印正确的输出。

但是

使用set /P 可以使您不能以空格开头。为此,没有很好的包装函数。如果你不需要,你可以使用消失的 qoutes:

@echo off
set ""="
for /f "delims=" %%E in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x1B"') do (
    set "BLUE=echo !"!%%E[94m"
    set "CYAN=echo !"!%%E[96m"
    set "END=%%E[0m"
)

setlocal EnableDelayedExpansion
%BLUE%Has no problems with '&|<>" characters%END%
%CYAN%'^^' and "^^!" need to be escaped%END%

结果:

Has no problems with '&|<>" characters
'^' and "!" need to be escaped

我希望你能从中有所收获。感谢阅读


参考资料:

【讨论】:

    猜你喜欢
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2021-10-31
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多