aboutsummaryrefslogtreecommitdiff
blob: 6be26ae8998ab97adf4a30339439b8c0a43c6a89 (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
# -*- coding: utf-8 -*-
# Copyright 2004-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import sys
import os

class OutputFormatter:
    codes = {
              'H': '\x1b[01m',        # Bold
              'U': '\x1b[04m',        # Underline
              'I': '\x1b[07m',        # Inverse
              'b': '\x1b[34;01m',     # Blue
              'B': '\x1b[34;06m',     # Dark Blue
              'c': '\x1b[36;01m',     # Cyan
              'C': '\x1b[36;06m',     # Dark Cyan
              'g': '\x1b[32;01m',     # Green
              'G': '\x1b[32;06m',     # Dark Green
              'm': '\x1b[35;01m',     # Magenta
              'M': '\x1b[35;06m',     # Dark Magenta
              'r': '\x1b[31;01m',     # Red
              'R': '\x1b[31;06m',     # Dark Red
              'y': '\x1b[33;01m',     # Yellow
              'Y': '\x1b[33;06m',     # Dark Yellow
              '$': '\x1b[0m',         # Reset
              '%': '%'                # Percent
             }

    def __init__(self, displayColor=True, displayTitle=True, autoIndent=True):
        self.colorOutput = displayColor
        self.autoIndent = autoIndent

    def __call__(self, displayColor=True, displayTitle=True, autoIndent=True):
        return self

    def setColorOutputStatus(self, status):
        self.colorOutput = status

    def __indent(self, prefix, message):
        if self.autoIndent is True:
            num = len(prefix)
            return prefix + message.replace('\n', '\n'+' '*num)

        else:
            return prefix + message

    def __parseColor(self, message):
        colored = ''
        striped = ''
        replace = 0

        for char in str(message):
            if replace:
                if char == ' ':
                    colored += self.codes['%'] + ' '
                    striped += self.codes['%'] + ' '
                elif char == '%':
                    colored += self.codes[char]
                    striped += self.codes[char]
                else:
                    colored += self.codes[char]
                replace = 0
            elif char == '%':
                replace = 1
            else:
                colored += char
                striped += char

        if self.colorOutput:
            return colored
        else:
            return striped

    def write(self, message):
        print(self.__parseColor(message.strip()))

    def _print(self, message):
        print(self.__parseColor(message))

    def _printError(self, message):
        message = "%H%R" + self.__indent("!!! ERROR: ", message) + "%$"
        sys.stderr.write(self.__parseColor(message) + '\n')

    def _printWarning(self, message):
        message = "%H%Y" + self.__indent("!!! WARNING: ", message) + "%$"
        sys.stderr.write(self.__parseColor(message) + '\n')

    def _printAlert(self, message):
        message = "%H%C" + self.__indent("!!! ALERT: ", message) +  "%$"
        sys.stderr.write(self.__parseColor(message) + '\n')


OutputFormatter = OutputFormatter()
# vim:set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap: