Version 0 of Get Colour Escape String

Updated 2018-06-06 04:47:15 by CecilWesterhof

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 ::dcblUtilities::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.