[Python-ideas] Add \e escape code

Joshua Landau joshua.landau.ws at gmail.com
Tue Jun 11 20:29:10 CEST 2013


On 11 June 2013 19:12, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> ANSI colors at the shell prompt are getting popular again these days.

I just use:

### CODE ###
"""
    Adapted from pygments.console

    Format colored console output.

    :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

def load():
    global codes
    codes = {
        "reset":     "\N{ESCAPE}[39;49;00m",
        "bold":      "\N{ESCAPE}[01m",
        "faint":     "\N{ESCAPE}[02m",
        "standout":  "\N{ESCAPE}[03m",
        "underline": "\N{ESCAPE}[04m",
        "blink":     "\N{ESCAPE}[05m",
        "overline":  "\N{ESCAPE}[06m"
    }

    dark_colors  = "black", "darkred", "darkgreen", "brown",
"darkblue", "purple", "teal", "lightgray"
    light_colors = "darkgray", "red", "green", "yellow", "blue",
"fuchsia", "turquoise", "white"

    for i, (dark, light) in enumerate(zip(dark_colors, light_colors), 30):
        codes[dark]  = "\N{ESCAPE}[{}m"   .format(i)
        codes[light] = "\N{ESCAPE}[{};01m".format(i)

    codes["darkteal"] = codes["turquoise"]
    codes["darkyellow"] = codes["brown"]
    codes["fuscia"] = codes["fuchsia"]
    codes["white"] = codes["bold"]

def unload():
    global codes
    codes = {code:"" for code in codes}

load()

### END CODE ###

And then write stuff like:
"{yellow}This is yellow!{reset} {bold}And{reset} this is not!".format(**codes)


More information about the Python-ideas mailing list