Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

SetColor & EchoColor - Set Output Color

May
383
4
I use color a lot in my scripts. My TCC has a black background and I find things look much prettier if I can colorize the output. I have two functions that do this...SetColor and EchoColor.

EchoColor: This is used to print one line in color. After that line is printed it goes back to the default. This used SetColor to do the heavy lifting. Useful for short little colored lines.

SetColor: This will turn on a color with ANSI sequences and everything that is echoed after this command will be in that color. If the color "NORMAL" is sent, it changes the color back to the default.

Code:
:: --------------------------------------------------------------------------------------------
:: EchoColor [color] ["text"]
:: Simply calls SetColor to desired color, prints the text, then sets color back to normal
:: --------------------------------------------------------------------------------------------
EchoColor {
    :: Ensure two arguments are entered
    iff "%@Words[%*]" lt "2" then
        EchoColor RED "EchoColor Error: Two parameters needed"
        echo.
        echo Usage:  EchoColor COLOR "TEXT"
        echo.
    else
        SetColor %1
        echo %@Unquote[%2]
        SetColor NORMAL
    endiff
}

Code:
:: -----------------------------------------------------------------------------------------
:: SetColor [Color]
:: Simple routine that sets the current console color to what is chosen.  Sending
:: the color "Normal" will set the color back to the standard color.
:: -----------------------------------------------------------------------------------------
SetColor {
    Switch %@Upper[%1]
        Case CYAN
            echos ^e[36;1m

        Case RED
            echos ^e[31;1m

        Case YELLOW
            echos ^e[33;1m

        Case MAGENTA
            echos ^e[35;1m

        Case WHITE
            echos ^e[37;1m

        Case GREEN
            echos ^e[32;1m

        Case BLACK
            echos ^e[30m

        Case BLUE
            echos ^e[34m
        
        Case NORMAL
            echos ^e[0m

        Default
            echo ** SETCOLOR ERROR:  Unknown Color:  '%1' **

    EndSwitch
}
 
Back
Top
[FOX] Ultimate Translator
Translate