Home Python color enum class for ANSI color codes
Post
Cancel

Python color enum class for ANSI color codes

When you want to print something with color and/or styling in a terminal, you can use one of the existing modules, such as colorama or sty. They are well-documented, flexible and easy to use.

Enum class

A very good guideline in software engineering is to avoid reinventing the wheel :upside_down_face:

But let’s face it, for some projects you really don’t want to bring in an external dependency. If the task is simple enough, you can just roll your own implementation. In this case, you’d just need to look into ANSI escape codes and find some examples.

For my very limited use cases when printing with color in a terminal, I just need to define a few foreground colors as well as the bold and the blink styles. I’m using an Enum class such that color names are class members, thus avoiding any possible misspelling issues:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from enum import Enum, auto


class Color(Enum):
    RED = 31
    GREEN = auto()
    YELLOW = auto()
    BLUE = auto()
    MAGENTA = auto()
    CYAN = auto()
    LIGHTRED = 91
    LIGHTGREEN = auto()
    LIGHTYELLOW = auto()
    LIGHTBLUE = auto()
    LIGHTMAGENTA = auto()
    LIGHTCYAN = auto()

    _START = '\u001b['
    _BOLD = ';1'
    _BLINK = ';5'
    _END = 'm'
    _RESET = '\u001b[0m'

    @staticmethod
    def colored(color, msg, bold=False, blink=False):
        if not(isinstance(color, Color)):
            raise TypeError(f'Unknown color {color}')

        fmt_msg = Color._START.value + str(color.value)

        if bold:
            fmt_msg += Color._BOLD.value
        if blink:
            fmt_msg += Color._BLINK.value

        return fmt_msg + Color._END.value + str(msg) + Color._RESET.value

We can use this class as follows:

1
2
3
4
5
6
7
8
9
from color import Color

for item in Color:
    if item.name.startswith('_'):
        continue
    print(Color.colored(item, item.name))
    if item.name.startswith('LIGHT'):
        print(Color.colored(item, '{} bold == {} bold'.format(
            item.name[5:], item.name), bold=True))

Here’s the output:

Colored output using the Color enum

Accompanying code

The full code accompanying this post can be found on my GitHub repository.

This post is licensed under CC BY 4.0 by the author.

Custom logger in Python for stdout and/or file log

Merge Mercurial branch into default

Comments powered by Disqus.