How to have multiple colors in a Windows batch file?

前端 未结 12 1475
我寻月下人不归
我寻月下人不归 2020-11-22 01:05

I was wondering if its possible to have different colored text on the same line in a Windows batch file, for example if it says

echo hi world
12条回答
  •  逝去的感伤
    2020-11-22 01:37

    Combining dbenham's bird and syntax with skrebbel's powershell write-host method, it seems that powershell can render complex art more quickly than dbenham's pure batch method (well, after powershell has been primed once, anyway). Minimal massaging of the strings are needed, although I haven't tested this with anything other than the bird. If you want a bright green end-of-transmission character for example, you may be out of luck. :)

    This method requires echoing out to a temp file, simply because invoking powershell for each call :c takes forever, and it's much faster to queue the output for one powershell invocation. But it does have the advantage of simplicity and efficiency.

    @echo off
    setlocal disableDelayedExpansion
    set q=^"
    echo(
    echo(
    call :c 0E "                ,      .-;" /n
    call :c 0E "             ,  |\    / /  __," /n
    call :c 0E "             |\ '.`-.|  |.'.-'" /n
    call :c 0E "              \`'-:  `; : /" /n
    call :c 0E "               `-._'.  \'|" /n
    call :c 0E "              ,_.-=` ` `  ~,_" /n
    call :c 0E "               '--,.    "&call :c 0c ".-. "&call :c 0E ",=!q!." /n
    call :c 0E "                 /     "&call :c 0c "{ "&call :c 0A "* "&call :c 0c ")"&call :c 0E "`"&call :c 06 ";-."&call :c 0E "}" /n
    call :c 0E "                 |      "&call :c 0c "'-' "&call :c 06 "/__ |" /n
    call :c 0E "                 /          "&call :c 06 "\_,\|" /n
    call :c 0E "                 |          (" /n
    call :c 0E "             "&call :c 0c "__ "&call :c 0E "/ '          \" /n
    call :c 02 "     /\_    "&call :c 0c "/,'`"&call :c 0E "|     '   "&call :c 0c ".-~!q!~~-." /n
    call :c 02 "     |`.\_ "&call :c 0c "|   "&call :c 0E "/  ' ,    "&call :c 0c "/        \" /n
    call :c 02 "   _/  `, \"&call :c 0c "|  "&call :c 0E "; ,     . "&call :c 0c "|  ,  '  . |" /n
    call :c 02 "   \   `,  "&call :c 0c "|  "&call :c 0E "|  ,  ,   "&call :c 0c "|  :  ;  : |" /n
    call :c 02 "   _\  `,  "&call :c 0c "\  "&call :c 0E "|.     ,  "&call :c 0c "|  |  |  | |" /n
    call :c 02 "   \`  `.   "&call :c 0c "\ "&call :c 0E "|   '     "&call :c 0A "|"&call :c 0c "\_|-'|_,'\|" /n
    call :c 02 "   _\   `,   "&call :c 0A "`"&call :c 0E "\  '  . ' "&call :c 0A "| |  | |  |           "&call :c 02 "__" /n
    call :c 02 "   \     `,   "&call :c 0E "| ,  '    "&call :c 0A "|_/'-|_\_/     "&call :c 02 "__ ,-;` /" /n
    call :c 02 "    \    `,    "&call :c 0E "\ .  , ' .| | | | |   "&call :c 02 "_/' ` _=`|" /n
    call :c 02 "     `\    `,   "&call :c 0E "\     ,  | | | | |"&call :c 02 "_/'   .=!q!  /" /n
    call :c 02 "     \`     `,   "&call :c 0E "`\      \/|,| ;"&call :c 02 "/'   .=!q!    |" /n
    call :c 02 "      \      `,    "&call :c 0E "`\' ,  | ; "&call :c 02 "/'    =!q!    _/" /n
    call :c 02 "       `\     `,  "&call :c 05 ".-!q!!q!-. "&call :c 0E "': "&call :c 02 "/'    =!q!     /" /n
    call :c 02 "    jgs _`\    ;"&call :c 05 "_{  '   ; "&call :c 02 "/'    =!q!      /" /n
    call :c 02 "       _\`-/__"&call :c 05 ".~  `."&call :c 07 "8"&call :c 05 ".'.!q!`~-. "&call :c 02 "=!q!     _,/" /n
    call :c 02 "    __\      "&call :c 05 "{   '-."&call :c 07 "|"&call :c 05 ".'.--~'`}"&call :c 02 "    _/" /n
    call :c 02 "    \    .=!q!` "&call :c 05 "}.-~!q!'"&call :c 0D "u"&call :c 05 "'-. '-..'  "&call :c 02 "__/" /n
    call :c 02 "   _/  .!q!    "&call :c 05 "{  -'.~('-._,.'"&call :c 02 "\_,/" /n
    call :c 02 "  /  .!q!    _/'"&call :c 05 "`--; ;  `.  ;" /n
    call :c 02 "   .=!q!  _/'      "&call :c 05 "`-..__,-'" /n
    call :c 02 "    __/'" /n
    
    if exist "%temp%\color.psm1" (
        powershell -command "&{set-executionpolicy remotesigned; Import-Module '%temp%\color.psm1'}"
        del "%temp%\color.psm1"
    )
    
    echo(
    
    exit /b
    
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    
    :c   
    setlocal enabledelayedexpansion
    set "colors=0-black;1-darkblue;2-darkgreen;3-darkcyan;4-darkred;5-darkmagenta;6-darkyellow;7-gray;8-darkgray;9-blue;a-green;b-cyan;c-red;d-magenta;e-yellow;f-white"
    set "p=%~1"
    set "bg=!colors:*%p:~0,1%-=!"
    set bg=%bg:;=&rem.%
    set "fg=!colors:*%p:~-1%-=!"
    set fg=%fg:;=&rem.%
    
    if not "%~3"=="/n" set "br=-nonewline"
    set "str=%~2" & set "str=!str:'=''!"
    
    >>"%temp%\color.psm1" echo write-host '!str!' -foregroundcolor '%fg%' -backgroundcolor '%bg%' %br%
    endlocal
    

    Result:

    enter image description here

提交回复
热议问题