File: core.py

package info (click to toggle)
python-colorful 0.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,124 kB
  • sloc: python: 1,343; sh: 8; makefile: 3
file content (551 lines) | stat: -rw-r--r-- 19,649 bytes parent folder | download
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# -*- coding: utf-8 -*-

"""
    colorful
    ~~~~~~~~

    Terminal string styling done right, in Python.

    :copyright: (c) 2017 by Timo Furrer <tuxtimo@gmail.com>
    :license: MIT, see LICENSE for more details.
"""

import os

from . import ansi
from . import colors
from . import styles
from . import terminal

#: Holds the name of the env variable which is
#  used as path to the default rgb.txt file
DEFAULT_RGB_TXT_PATH = os.environ.get(
    'COLORFUL_DEFAULT_COLOR_PALETTE',
    os.path.join(os.path.dirname(__file__), 'data', 'rgb.txt'))

#: Holds the color names mapped to RGB channels
COLOR_PALETTE = colors.parse_colors(path=DEFAULT_RGB_TXT_PATH)

#: Holds the path to the built-in `colornames` color palette file
COLORNAMES_COLORS_PATH = os.path.join(os.path.dirname(__file__), "data", "colornames.json")


class ColorfulError(Exception):
    """
    Exception which is raised for Colorful specific
    usage errors.
    """


class ColorfulAttributeError(AttributeError, ColorfulError):
    """
    Exception which is raised for Colorful specific
    usage errors raised during ``__getattr__`` calls.

    This is to ensure a correct ``__getattr__`` protocol implementation.
    """


def translate_rgb_to_ansi_code(red, green, blue, offset, colormode):
    """
    Translate the given RGB color into the appropriate ANSI escape code
    for the given color mode.
    The offset is used for the base color which is used.

    The ``colormode`` has to be one of:
        * 0: no colors / disabled
        * 8: use ANSI 8 colors
        * 16: use ANSI 16 colors (same as 8 but with brightness)
        * 256: use ANSI 256 colors
        * 0xFFFFFF / 16777215: use 16 Million true colors

    :param int red: the red channel value
    :param int green: the green channel value
    :param int blue: the blue channel value
    :param int offset: the offset to use for the base color
    :param int colormode: the color mode to use. See explanation above
    """
    if colormode == terminal.NO_COLORS:  # colors are disabled, thus return empty string
        return '', ''

    if colormode == terminal.ANSI_8_COLORS or colormode == terminal.ANSI_16_COLORS:
        color_code = ansi.rgb_to_ansi16(red, green, blue)
        start_code = ansi.ANSI_ESCAPE_CODE.format(
            code=color_code + offset - ansi.FOREGROUND_COLOR_OFFSET)
        end_code = ansi.ANSI_ESCAPE_CODE.format(code=offset + ansi.COLOR_CLOSE_OFFSET)
        return start_code, end_code

    if colormode == terminal.ANSI_256_COLORS:
        color_code = ansi.rgb_to_ansi256(red, green, blue)
        start_code = ansi.ANSI_ESCAPE_CODE.format(code='{base};5;{code}'.format(
            base=8 + offset, code=color_code))
        end_code = ansi.ANSI_ESCAPE_CODE.format(code=offset + ansi.COLOR_CLOSE_OFFSET)
        return start_code, end_code

    if colormode == terminal.TRUE_COLORS:
        start_code = ansi.ANSI_ESCAPE_CODE.format(code='{base};2;{red};{green};{blue}'.format(
            base=8 + offset, red=red, green=green, blue=blue))
        end_code = ansi.ANSI_ESCAPE_CODE.format(code=offset + ansi.COLOR_CLOSE_OFFSET)
        return start_code, end_code

    raise ColorfulAttributeError('invalid color mode "{0}"'.format(colormode))


def translate_colorname_to_ansi_code(colorname, offset, colormode, colorpalette):
    """
    Translate the given color name to a valid
    ANSI escape code.

    :parma str colorname: the name of the color to resolve
    :parma str offset: the offset for the color code
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
    :parma dict colorpalette: the color palette to use for the color name mapping

    :returns str: the color as ANSI escape code

    :raises ColorfulError: if the given color name is invalid
    """
    try:
        red, green, blue = colorpalette[colorname]
    except KeyError:
        raise ColorfulAttributeError('the color "{0}" is unknown. Use a color in your color palette (by default: X11 rgb.txt)'.format(  # noqa
            colorname))
    else:
        return translate_rgb_to_ansi_code(red, green, blue, offset, colormode)


def resolve_modifier_to_ansi_code(modifiername, colormode):
    """
    Resolve the given modifier name to a valid
    ANSI escape code.

    :param str modifiername: the name of the modifier to resolve
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns str: the ANSI escape code for the modifier

    :raises ColorfulError: if the given modifier name is invalid
    """
    if colormode == terminal.NO_COLORS:  # return empty string if colors are disabled
        return '', ''

    try:
        start_code, end_code = ansi.MODIFIERS[modifiername]
    except KeyError:
        raise ColorfulAttributeError('the modifier "{0}" is unknown. Use one of: {1}'.format(
            modifiername, ansi.MODIFIERS.keys()))
    else:
        return ansi.ANSI_ESCAPE_CODE.format(
            code=start_code), ansi.ANSI_ESCAPE_CODE.format(
                code=end_code)


def translate_style(style, colormode, colorpalette):
    """
    Translate the given style to an ANSI escape code
    sequence.

    ``style`` examples are:

    * green
    * bold
    * red_on_black
    * bold_green
    * italic_yellow_on_cyan

    :param str style: the style to translate
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
    :parma dict colorpalette: the color palette to use for the color name mapping
    """
    style_parts = iter(style.split('_'))

    ansi_start_sequence = []
    ansi_end_sequence = []

    try:
        # consume all modifiers
        part = None
        for mod_part in style_parts:
            part = mod_part
            if part not in ansi.MODIFIERS:
                break  # all modifiers have been consumed

            mod_start_code, mod_end_code = resolve_modifier_to_ansi_code(part, colormode)
            ansi_start_sequence.append(mod_start_code)
            ansi_end_sequence.append(mod_end_code)
        else:  # we've consumed all parts, thus we can exit
            raise StopIteration()

        # next part has to be a foreground color or the 'on' keyword
        # which means we have to consume background colors
        if part != 'on':
            ansi_start_code, ansi_end_code = translate_colorname_to_ansi_code(
                part, ansi.FOREGROUND_COLOR_OFFSET, colormode, colorpalette)
            ansi_start_sequence.append(ansi_start_code)
            ansi_end_sequence.append(ansi_end_code)
            # consume the required 'on' keyword after the foreground color
            next(style_parts)

        # next part has to be the background color
        part = next(style_parts)
        ansi_start_code, ansi_end_code = translate_colorname_to_ansi_code(
            part, ansi.BACKGROUND_COLOR_OFFSET, colormode, colorpalette)
        ansi_start_sequence.append(ansi_start_code)
        ansi_end_sequence.append(ansi_end_code)
    except StopIteration:  # we've consumed all parts of the styling string
        pass

    # construct and return ANSI escape code sequence
    return ''.join(ansi_start_sequence), ''.join(ansi_end_sequence)


def style_string(string, ansi_style, colormode, nested=False):
    """
    Style the given string according to the given
    ANSI style string.

    :param str string: the string to style
    :param tuple ansi_style: the styling string returned by ``translate_style``
    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``

    :returns: a string containing proper ANSI sequence
    """
    ansi_start_code, ansi_end_code = ansi_style

    # replace nest placeholders with the current begin style
    string = str(string).replace(ansi.NEST_PLACEHOLDER, ansi_start_code)

    return '{start_code}{string}{end_code}{nest_ph}'.format(
            start_code=ansi_start_code,
            string=string,
            end_code=ansi_end_code,
            nest_ph=ansi.NEST_PLACEHOLDER if nested else '')


class ColorfulString(object):
    """
    Represents a colored string
    """
    def __init__(self, orig_string, styled_string, colorful_ctx):
        self.orig_string = str(orig_string)
        self.styled_string = str(styled_string)
        self.colorful_ctx = colorful_ctx

    def __str__(self):
        if self.colorful_ctx.colormode == terminal.NO_COLORS:
            return self.orig_string
        else:
            return self.styled_string

    def __len__(self):
        return len(self.orig_string)

    def __iter__(self):
        return iter(self.styled_string)

    def __add__(self, other):
        if isinstance(other, ColorfulString):
            return ColorfulString(
                self.orig_string + other.orig_string,
                self.styled_string + other.styled_string,
                self.colorful_ctx)

        return ColorfulString(
            self.orig_string + other,
            self.styled_string + other,
            self.colorful_ctx)

    def __iadd__(self, other):
        if isinstance(other, ColorfulString):
            self.orig_string += other.orig_string
            self.styled_string += other.styled_string
        else:
            self.orig_string += other
            self.styled_string += other

        return self

    def __radd__(self, other):
        if isinstance(other, ColorfulString):
            return ColorfulString(
                other.orig_string + self.orig_string,
                other.styled_string + self.styled_string,
                self.colorful_ctx)

        # we return handover the conversion to the
        # object on the left side
        return other + self.styled_string

    def __mul__(self, other):
        return ColorfulString(
            self.orig_string * other,
            self.styled_string * other,
            self.colorful_ctx)

    def __format__(self, format_spec):
        if self.colorful_ctx.colormode == terminal.NO_COLORS:
            return self.orig_string.__format__(format_spec)

        # append nested placeholder to styled string in order to continue the
        # previous styles. If the string already ends with the nest placeholder
        # we don't have to append it again.
        if self.styled_string.endswith(ansi.NEST_PLACEHOLDER):
            styled_string = self.styled_string
        else:
            styled_string = '{orig_str}{nest_ph}'.format(
                orig_str=self.styled_string, nest_ph=ansi.NEST_PLACEHOLDER)
        return styled_string.__format__(format_spec)

    def __getattr__(self, name):
        str_method = getattr(self.styled_string, name)
        return str_method


class Colorful(object):
    """
    Provides methods to style strings for terminal
    output.

    :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
    """
    # re-expose the color modes from ``colorful.terminal``
    # on a package level.
    NO_COLORS = terminal.NO_COLORS
    ANSI_8_COLORS = terminal.ANSI_8_COLORS
    ANSI_16_COLORS = terminal.ANSI_16_COLORS
    ANSI_256_COLORS = terminal.ANSI_256_COLORS
    TRUE_COLORS = terminal.TRUE_COLORS

    # expose the `colornames` color palette
    COLORNAMES_COLORS = COLORNAMES_COLORS_PATH

    # expose ANSI escape codes to close colors
    # this is especially useful when using ``str.format()``.
    close_fg_color = ansi.ANSI_ESCAPE_CODE.format(
        code=ansi.FOREGROUND_COLOR_OFFSET + ansi.COLOR_CLOSE_OFFSET)
    close_bg_color = ansi.ANSI_ESCAPE_CODE.format(
        code=ansi.BACKGROUND_COLOR_OFFSET + ansi.COLOR_CLOSE_OFFSET)

    # expose ANSI escape codes to close modifiers
    no_bold = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['bold'][1])
    no_dimmed = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['dimmed'][1])
    no_italic = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['italic'][1])
    no_underlined = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['underlined'][1])
    no_blinkslow = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['blinkslow'][1])
    no_blinkrapid = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['blinkrapid'][1])
    no_inversed = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['inversed'][1])
    no_concealed = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['concealed'][1])
    no_struckthrough = ansi.ANSI_ESCAPE_CODE.format(code=ansi.MODIFIERS['struckthrough'][1])

    def __init__(self, colormode=None, colorpalette=None):
        if colormode is None:  # try to auto-detect color mode
            colormode = terminal.detect_color_support(env=os.environ)

        if colorpalette is None:  # load default color palette
            colorpalette = COLOR_PALETTE
        elif isinstance(colorpalette, str):  # we assume it's a path to a color file
            colorpalette = colors.parse_colors(colorpalette)

        #: Holds the color mode to use for this Colorful object.
        self.colormode = colormode

        #: Holds the color palette to use for this Colorful object.
        self._colorpalette = None
        self.colorpalette = colorpalette

    @property
    def colorpalette(self):
        """
        Get the current used color palette
        """
        return self._colorpalette

    @colorpalette.setter
    def colorpalette(self, colorpalette):
        """
        Set the colorpalette which should be used
        """
        if isinstance(colorpalette, str):  # we assume it's a path to a color file
            colorpalette = colors.parse_colors(colorpalette)

        self._colorpalette = colors.sanitize_color_palette(colorpalette)

    def setup(self, colormode=None, colorpalette=None, extend_colors=False):
        """
        Setup this colorful object by setting a ``colormode`` and
        the ``colorpalette`. The ``extend_colors`` flag is used
        to extend the currently active color palette instead of
        replacing it.

        :param int colormode: the color mode to use. See ``translate_rgb_to_ansi_code``
        :parma dict colorpalette: the colorpalette to use. This ``dict`` should map
                                  color names to it's corresponding RGB value
        :param bool extend_colors: extend the active color palette instead of replacing it
        """
        if colormode is not None:
            self.colormode = colormode

        if colorpalette:
            if extend_colors:
                self.update_palette(colorpalette)
            else:
                self.colorpalette = colorpalette

    def disable(self):
        """
        Disable all colors and styles
        """
        self.colormode = terminal.NO_COLORS

    def use_8_ansi_colors(self):
        """
        Use 8 ANSI colors for this colorful object
        """
        self.colormode = terminal.ANSI_8_COLORS

    def use_16_ansi_colors(self):
        """
        Use 16 ANSI colors for this colorful object
        """
        self.colormode = terminal.ANSI_16_COLORS

    def use_256_ansi_colors(self):
        """
        Use 256 ANSI colors for this colorful object
        """
        self.colormode = terminal.ANSI_256_COLORS

    def use_true_colors(self):
        """
        Use true colors for this colorful object
        """
        self.colormode = terminal.TRUE_COLORS

    def use_palette(self, colorpalette):
        """
        Use the given color palette
        """
        self.colorpalette = colorpalette

    def update_palette(self, colorpalette):
        """
        Update the currently active color palette
        with the given color palette
        """
        self.colorpalette.update(colors.sanitize_color_palette(colorpalette))

    def use_style(self, style_name):
        """
        Use a predefined style as color palette

        :param str style_name: the name of the style
        """
        try:
            style = getattr(styles, style_name.upper())
        except AttributeError:
            raise ColorfulError('the style "{0}" is undefined'.format(
                style_name))
        else:
            self.colorpalette = style

    def format(self, string, *args, **kwargs):
        """
        Format the given string with the given ``args`` and ``kwargs``.
        The string can contain references to ``c`` which is provided by
        this colorful object.

        :param str string: the string to format
        """
        return string.format(c=self, *args, **kwargs)

    def str(self, string):
        """
        Create a new ColorfulString instance of the given
        unstyled string.

        This method should be used to create a ColorfulString
        which is actually not styled yet but can safely be concatinated
        with other ColorfulStrings like:

        >>> s = colorful.str('Hello ')
        >>> s =+ colorful.black('World')
        >>> str(s)
        'Hello \033[30mWorld\033[39m'

        :param str string: the string to use for the ColorfulString
        """
        return ColorfulString(string, string, self)

    def print(self, *objects, sep=' ', end='\n', file=None, flush=False):
        """
        Print the given objects to the given file stream.
        See https://docs.python.org/3/library/functions.html#print

        The only difference to the ``print()`` built-in is that
        ``Colorful.print()`` formats the string with ``c=self``.
        With that stylings are possible

        :param str sep: the seperater between the objects
        :param str end: the ending delimiter after all objects
        :param file: the file stream to write to
        :param bool flush: if the stream should be flushed
        """
        styled_objects = [self.format(o) for o in objects]
        print(*styled_objects, sep=sep, end=end, file=file, flush=flush)

    class ColorfulStyle(object):
        """
        Represents a colorful style
        """
        def __init__(self, style, colormode, colorful_ctx):
            self.style = style
            self.colormode = colormode
            self.colorful_ctx = colorful_ctx

        def evaluate(self, string, nested=False):
            """
            Evaluate the style on the given string.

            :parma str string: the string to style
            :param bool nested: if the string is part of another styled string
                                (=> nested in another style)
            """
            return ColorfulString(
                string,
                style_string(string, self.style, self.colormode, nested),
                self.colorful_ctx)

        def __str__(self):
            return self.style[0]

        def __and__(self, other):
            new_style = (
                self.style[0] + other.style[0],
                self.style[1] + other.style[1]
            )
            return Colorful.ColorfulStyle(new_style, self.colormode, self.colorful_ctx)

        def __call__(self, string, nested=False):
            return self.evaluate(string, nested)

        def __or__(self, other):
            return self.evaluate(other)

        def __eq__(self, other):
            if not isinstance(other, Colorful.ColorfulStyle):
                return False

            return (
                self.style == other.style and
                self.colormode == other.colormode and
                self.colorful_ctx == other.colorful_ctx
            )

        def __hash__(self):
            return hash((self.style, self.colormode, self.colorful_ctx))

    def __getattr__(self, name):
        # translate the given name into an ANSI escape code sequence
        style = translate_style(name, self.colormode, self.colorpalette)
        style_wrapper = self.ColorfulStyle(style, self.colormode, self)
        return style_wrapper