aboutsummaryrefslogtreecommitdiffstats
path: root/netmodel/util/color.py
blob: 55719469a63acba1cd0ee1fde7e8c22a841bf199 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# ANSI escape codes for terminals.
#  X11 xterm: always works, all platforms
#  cygwin dosbox: run through |cat and then colors work
#  linux: works on console & gnome-terminal
#  mac: untested
 
BLACK      = "\033[0;30m"
BLUE       = "\033[0;34m"
GREEN      = "\033[0;32m"
CYAN       = "\033[0;36m"
RED        = "\033[0;31m"
PURPLE     = "\033[0;35m"
BROWN      = "\033[0;33m"
GRAY       = "\033[0;37m"
BOLDGRAY   = "\033[1;30m"
BOLDBLUE   = "\033[1;34m"
BOLDGREEN  = "\033[1;32m"
BOLDCYAN   = "\033[1;36m"
BOLDRED    = "\033[1;31m"
BOLDPURPLE = "\033[1;35m"
BOLDYELLOW = "\033[1;33m"
WHITE      = "\033[1;37m"

MYCYAN     = "\033[96m"
MYGREEN    = '\033[92m'
MYBLUE     = '\033[94m'
MYWARNING  = '\033[93m'
MYRED      = '\033[91m'
MYHEADER   = '\033[95m'
MYEND      = '\033[0m'
 
NORMAL = "\033[0m"

colors = {
    'white':        "\033[1;37m",
    'yellow':       "\033[1;33m",
    'green':        "\033[1;32m",
    'blue':         "\033[1;34m",
    'cyan':         "\033[1;36m",
    'red':          "\033[1;31m",
    'magenta':      "\033[1;35m",
    'black':        "\033[1;30m",
    'darkwhite':    "\033[0;37m",
    'darkyellow':   "\033[0;33m",
    'darkgreen':    "\033[0;32m",
    'darkblue':     "\033[0;34m",
    'darkcyan':     "\033[0;36m",
    'darkred':      "\033[0;31m",
    'darkmagenta':  "\033[0;35m",
    'darkblack':    "\033[0;30m",
    'off':          "\033[0;0m"
}

def textcolor(color, string):
    """
    This function is useful to output information to the stdout by exploiting
    different colors, depending on the result of the last command executed.

    It is possible to chose one of the following colors:
        - white
        - yellow
        - green
        - blue
        - cyan
        - red
        - magenta
        - black
        - darkwhite
        - darkyellow
        - darkgreen
        - darkblue
        - darkcyan
        - darkred
        - darkmagenta
        - darkblack
        - off

    :param color: The color of the output string, chosen from the previous
        list.
    :param string: The string to color
    :return: The colored string if the color is valid, the original string
        otherwise.
    """

    try:
        return colors[color] + string + colors['off']
    except:
        return string

if __name__ == '__main__':
    # Display color names in their color
    for name, color in locals().items():
        if name.startswith('__'): continue
        print(color, name, MYEND)