How to have multiple colors in a Windows batch file?

前端 未结 12 1595
我寻月下人不归
我寻月下人不归 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:36

    After my previous answer was deleted for failing to include the code, on the basis the Ansi characters used can not be displayed by stack overflow rendering the presence of the code somewhat pointless given it was based on them, I have redesigned the code to include the method of populating the Escape character detailed by @Sam Hasler

    In the process, I've also taken out all the subroutines in favor of macro's, and adapted my approach to passing parameters to the macro's.

    All macro's balance the Setlocal / Endlocal pairings to prevent exceeding recursion level's through use of ^&^& endlocal after completeing their handling of the Args.

    The std.out macro also demonstrates how to adapt the macros to store output into variables that survive past the Endlocal barrier.

    @Echo off & Mode 1000
    
    ::: / Creates two variables with one character DEL=Ascii-08 and /AE=Ascii-27 escape code. only /AE is used
    ::: - http://www.dostips.com/forum/viewtopic.php?t=1733
    ::: - https://stackoverflow.com/a/34923514/12343998
    :::
    ::: - DEL and ESC can be used  with and without DelayedExpansion, except during Macro Definition
        Setlocal
        For /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
            Endlocal
            Set "DEL=%%a"
            Set "/AE=%%b"
        )
    ::: \
    
    ::: / Establish Environment for macro Definition
        Setlocal DisableDelayedExpansion
    
        (Set LF=^
    
    
        %= NewLine =%)
    
        Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
    ::: \
    
    ::: / Ascii code variable assignment
    ::: - Variables used for cursor Positiong Ascii codes in the form of bookends to prevent ansi escape code disrupting macro definition
        Set "[=%/AE%["
        Set "]=H"
    ::: - define Variables for Ascii color code values
        Set "Red=%/AE%[31m"
        Set "Green=%/AE%[32m"
        Set "Yellow=%/AE%[33m"
        Set "Blue=%/AE%[34m"
        Set "Purple=%/AE%[35m"
        Set "Cyan=%/AE%[36m"
        Set "White=%/AE%[37m"
        Set "Grey=%/AE%[90m"
        Set "Pink=%/AE%[91m"
        Set "BrightGreen=%/AE%[92m"
        Set "Beige=%/AE%[93m"
        Set "Aqua=%/AE%[94m"
        Set "Magenta=%/AE%[95m"
        Set "Teal=%/AE%[96m"
        Set "BrightWhite=%/AE%[97m"
        Set "Off=%/AE%[0m"
    ::: \
    
    ::: / mini-Macro to Pseudo pipe complex strings into Macros.
        Set "Param|=Set Arg-Output="
    ::: \
    
    ::: / Macro for outputing to cursor position Arg1 in color Arg2
        Set Pos.Color=^&for /L %%n in (1 1 2) do if %%n==2 (%\n%
            For /F "tokens=1,2 delims=, " %%G in ("!argv!") do (%\n%
                Echo(![!%%G!]!!%%H!!Arg-Output!!Off!^&^&Endlocal%\n%
            ) %\n%
        ) ELSE setlocal enableDelayedExpansion ^& set argv=, 
    ::: \
    
    ::: / Macro variable for creating a Colored prompt with pause at Cursor pos Arg1 in Color Arg2
        Set Prompt.Pause=^&for /L %%n in (1 1 2) do if %%n==2 (%\n%
            For /F "tokens=1,2 delims=, " %%G in ("!argv!") do (%\n%
            Echo.!/AE![%%G!]!!/AE![%%Hm!Arg-Output!!Off!%\n%
            pause^>nul ^&^& Endlocal%\n%
            ) %\n%
        ) ELSE setlocal enableDelayedExpansion ^& set argv=, 
    ::: \
    
    ::: / Macro variable for outputing to stdout on a new line with selected color Arg1 and store output to VarName Arg2
        Set std.out=^&for /L %%n in (1 1 2) do if %%n==2 (%\n%
            For /F "tokens=1,2 delims=, " %%G in ("!argv!") do (%\n%
            Echo.!/AE![%%Gm!Arg-Output!!Off!^&^& Endlocal ^&(Set %%H=!Arg-Output!)%\n%
            ) %\n%
        ) ELSE setlocal enableDelayedExpansion ^& set argv=, 
    ::: \
    
    ::: / Stringlength Macro. Not utilized in this example.
    ::: Usage: %Param|%string or expanded variable%get.strLen% ResultVar
    Set get.strLen=^&for /L %%n in (1 1 2) do if %%n==2 (%\n%
        For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
            Set tmpLen=!Arg-Output!%\n%
            Set LenTrim=Start%\n%
            For /L %%a in (1,1,250) Do (%\n%
                IF NOT "!LenTrim!"=="" (%\n%
                    Set LenTrim=!tmpLen:~0,-%%a!%\n%
                    If "!LenTrim!"=="" Echo.>nul ^&^& Endlocal ^&(Set %%G=%%a)%\n%
                )%\n%
            ) %\n%
        ) %\n%
    ) ELSE setlocal enableDelayedExpansion ^& set argv=, 
    ::: \
    
    
    ::: / Create Script break for Subroutines. Subroutines not utilized in this example
        Goto :main
    ::: \
    
    ::: / Subroutines
    
    ::: \
    
    ::: / Script main Body
    :::::: - Example usage
    :main
    
        Setlocal EnableDelayedExpansion
    
    For %%A in ("31,1,37" "41,1,47" "90,1,97" "100,1,107") do For /L %%B in (%%~A) Do %Param|%[Color Code = %%B.]%std.out% %%B AssignVar
    
            Set "XP=20"
        For %%A in (red aqua white brightwhite brightgreen beige blue magenta green pink cyan grey yellow purple teal) do (
            Set /A XP+=1
            Set /A YP+=1
            Set "output=%%A !YP!;!XP!"
            %Param|%Cursor Pos: !YP!;!XP!, %%A %Pos.Color% !YP!;!XP! %%A
        )
        %Param|%Example %green%Complete.%prompt.pause% 32;10 31
    
        Endlocal
    
    Exit /B
    ::: \ End Script
    

    My thanks to @Martijn Pieters for deleting my previous answer. In rewriting my code I also discovered for myself some new ways to manipulate macro's.

    Script output

提交回复
热议问题