Updated 2018-08-24 14:55:41 by pooryorick

Created by CecilWesterhof.

When displaying the prompt I like to use colours. For this I created the following function (probably only works with XWindows):
proc getColour {colour {background False}} {
    set colourArr(black)       "0;30"
    set colourArr(blue)        "0;34"
    set colourArr(blueLight)   "1;34"
    set colourArr(brown)       "0;33"
    set colourArr(cyan)        "0;36"
    set colourArr(cyanLight)   "1;36"
    set colourArr(grayDark)    "1;30"
    set colourArr(grayLight)   "0;37"
    set colourArr(green)       "0;32"
    set colourArr(greenLight)  "1;32"
    set colourArr(nothing)     "0"
    set colourArr(purple)      "0;35"
    set colourArr(purpleLight) "1;35"
    set colourArr(red)         "0;31"
    set colourArr(redLight)    "1;31"
    set colourArr(white)       "1;37"
    set colourArr(yellow)      "1;33"
    set colourEnd              "m\002"
    set colourStart            "\001\033\["

    if {$colour eq "names"} {
        return [lsort [array names colourArr]]
    }
    if {! [info exist colourArr($colour)]} {
        error [format "ERROR: %s got a non existing colour (%s)" \
                   [getProcName] $colour]
    }
    set colourCode $colourArr($colour)
    # No need to check for nothing, replace out of range does nothing.
    if {$background} {
        set colourCode [string replace $colourCode 2 2 4]
    }
    return "$colourStart$colourCode$colourEnd"
}

With names you get a list of defined colours. When given a non-existing colour an error is generated. A background colour has at index 2 a 4 instead of a 3.

As always: comments, tips and questions are appreciated.

bll 2018-6-6 Octal specifications with a leading zero will go away: https://core.tcl.tk/tips/doc/trunk/tip/114.md

Note that this is for ansi/vt100/vt220 terminals that support color.

Myself, I would still prefer a portable implementation using the output from tput, though I suppose it doesn't matter much any more. There will still be some companies that use a terminal emulator with something other than vt100 since they must support access to some legacy software.

Also note that some of the terminal emulators out there are not very good (e.g. rxvt). Also note that an ansi terminal is not the same as a vt100 (but really close). xterm has an excellent vt100 terminal emulation.

There's a good answer here (using tput) that specifies the basic 8 colors that most color terminals support and goes on to show how to use tput with more colors. https://unix.stackexchange.com/questions/269077/tput-setaf-color-table-how-to-determine-color-codes#269195

But, this is all sort of becoming historical and I am rambling on.

ak - 2018-06-06 19:43:17

Hi bll I am not sure what the removal of octal interpretation for "08" etc. in expressions has to do with the terminal color codes ?! If you are looking at the "\0..." escapes, these are backslash substitutions for characters, under rule 9 of the Dodekalogue, and are not affected by the TIP, at all. The TIP is about numbers in expressions, nothing else. It will not affect "file permissions", nor the backslash substitutions.

bll Ok, thanks for the clarification.