summaryrefslogtreecommitdiffstats
path: root/colorclass/color.py
blob: 2849d06b4aa49bfc19665030b0a75edfcb11299e (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Color class used by library users."""

from colorclass.core import ColorStr


class Color(ColorStr):
    """Unicode (str in Python3) subclass with ANSI terminal text color support.

    Example syntax: Color('{red}Sample Text{/red}')

    Example without parsing logic: Color('{red}Sample Text{/red}', keep_tags=True)

    For a list of codes, call: colorclass.list_tags()
    """

    @classmethod
    def colorize(cls, color, string, auto=False):
        """Color-code entire string using specified color.

        :param str color: Color of string.
        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        tag = '{0}{1}'.format('auto' if auto else '', color)
        return cls('{%s}%s{/%s}' % (tag, string, tag))

    @classmethod
    def black(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('black', string, auto=auto)

    @classmethod
    def bgblack(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bgblack', string, auto=auto)

    @classmethod
    def red(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('red', string, auto=auto)

    @classmethod
    def bgred(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bgred', string, auto=auto)

    @classmethod
    def green(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('green', string, auto=auto)

    @classmethod
    def bggreen(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bggreen', string, auto=auto)

    @classmethod
    def yellow(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('yellow', string, auto=auto)

    @classmethod
    def bgyellow(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bgyellow', string, auto=auto)

    @classmethod
    def blue(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('blue', string, auto=auto)

    @classmethod
    def bgblue(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bgblue', string, auto=auto)

    @classmethod
    def magenta(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('magenta', string, auto=auto)

    @classmethod
    def bgmagenta(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bgmagenta', string, auto=auto)

    @classmethod
    def cyan(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('cyan', string, auto=auto)

    @classmethod
    def bgcyan(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bgcyan', string, auto=auto)

    @classmethod
    def white(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('white', string, auto=auto)

    @classmethod
    def bgwhite(cls, string, auto=False):
        """Color-code entire string.

        :param str string: String to colorize.
        :param bool auto: Enable auto-color (dark/light terminal).

        :return: Class instance for colorized string.
        :rtype: Color
        """
        return cls.colorize('bgwhite', string, auto=auto)