Quick Formatting ANSI Colour Escape Strings

WJG (17/05/14) I wanted a quick and easy way of marking up text strings for output to the console -- Tcl string map magic came in useful yet again.

#---------------
# Ansi colour markup. All settings are intense.
# r,g,b,c,m,y,blk,wht = effects foreground settings
# R,G,B,C,M,Y,BLK,WHT = background settings
# n = reset to normal
#---------------
# bold 1 light 2 blink 5 invert 7
# FG  black 30 red 31 green 32 yellow 33 blue 34 purple 35 cyan 36 white 37
# BG  Black 40 Red 41 Green 42 Yellow 43 Blue 44 Purple 45 Cyan 46 White 47
#---------------
proc g_ansi { str } {
        
        set clr_map "
        <r> \033\[01;31m
        <g> \033\[01;32m
        <b> \033\[01;34m

        <c> \033\[01;36m
        <m> \033\[01;35m
        <y> \033\[01;33m

        <R> \033\[01;41m
        <G> \033\[01;42m
        <B> \033\[01;44m

        <C> \033\[01;46m
        <M> \033\[01;45m
        <Y> \033\[01;43m

        <wht> \033\[01;37m
        <blk> \033\[01;30m
        
        <WHT> \033\[01;47m
        <BLK> \033\[01;40m

        <n> \033\[;0m
        /<        <
        />        >
        "
        return [string map $clr_map ${str}<n>]
}

puts [g_ansi "<r> red <g> green <b> blue <c> cyan <m> magenta <y> yellow"]
puts [g_ansi "<R> red <G> green <B> blue <C> cyan <M> magenta <Y> yellow" ]
puts [g_ansi "<WHT>HELLO<n> underline <R>/<WHT/>"]

AMG: You could also use procs:

foreach {name value} {r 1 g 2 b 4 c 6 m 5 y 3 wht 7 blk 0} {
    proc $name                  {} "return \033\\\[01\\;3${value}m"
    proc [string toupper $name] {} "return \033\\\[01\\;4${value}m"
}
proc n {} {return \033\[\;0m}
puts "[r] red [g] green [b] blue [c] cyan [m] magenta [y] yellow"
puts "[R] red [G] green [B] blue [C] cyan [M] magenta [Y] yellow"
puts "[WHT]HELLO[n] underline [R]\[WHT\]"