File: terminal.py

package info (click to toggle)
python-qmix 1.0.6-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,460 kB
  • sloc: python: 4,312; makefile: 215
file content (198 lines) | stat: -rw-r--r-- 5,509 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
""" This sub-module contains functions for printing numbers and text to the 
terminal.

"""

import os


def print_intro():  # pragma: no cover
    """ Print a quick intro to the terminal.

    """

    os.system('clear')
    print("\nWelcome to")
    art = """
      ,ad8888ba,    88b           d88  88
     d8"'    `"8b   888b         d888  ""
    d8'        `8b  88`8b       d8'88
    88          88  88 `8b     d8' 88  88  8b,     ,d8
    88          88  88  `8b   d8'  88  88   `Y8, ,8P'
    Y8,    "88,,8P  88   `8b d8'   88  88     )888(
     Y8a.    Y88P   88    `888'    88  88   ,d8" "8b,
      `"Y8888Y"Y8a  88     `8'     88  88  8P'     `Y8
    """
    intro = """
            *** SIS Mixer Analysis Software ***
    """
    cprint(art, color='MAGENTA')
    print(intro)


def cprint(text, color='HEADER'):  # pragma: no cover
    """ Print colored text to the terminal.

    Args:
        text (str): Text to print
        color (str, optional): Color/style to print in, default is "HEADER"

    """

    if color is None:
        print(text)
    else:
        try:
            tcolour = _terminal_colors[color.upper()]
            print((tcolour + text + _terminal_colors['ENDC']))
        except KeyError:
            print("\'color\' must be one of:")
            print((sorted(_terminal_colors.keys())))
            print(text, color)
            return


# Titles ---------------------------------------------------------------------

def title(title_string, color=None, total_len=60):  # pragma: no cover
    """ Print a nice title to the terminal.

    Args:
        title_string (str): title to print
        color (str, optional): Color to print in, default is None
        total_len (int, optional): Total length of title string (including
            stars), default is 60

    """

    minus_title = total_len - len(title_string) - 2
    if minus_title % 2 == 0:
        left = minus_title // 2
        right = minus_title // 2
    else:
        left = minus_title // 2
        right = minus_title // 2 + 1

    title_string = " " + title_string + " "
    title_string = "*" * left + title_string + "*" * right + "\n"

    cprint(title_string, color)


def header(header_string, color=None):  # pragma: no cover
    """ Print a nice header to the terminal.

    Args:
        header_string (str): Header title to print
        color (str, optional): Color to print in, default is None

    """

    cprint("\t" + header_string + ":", color)
    cprint("\t" + "-" * 50)


# Print numbers to the terminal in a nice way --------------------------------

def pvalf(name, val, units='', comment='', color=None):  # pragma: no cover
    """ Print name, value as float, and units to terminal.

    Args:
        name (str): variable name
        val (float): variable value
        units (str, optional): variable units, default is ""
        comment (str, optional): comment, default is ""
        color (str, optional): color, default is None

    """

    if units != '':
        units = '\t[' + units + ']'
    if comment != '':
        comment = "  # {0}".format(comment)

    if isinstance(val, complex):
        re = val.real
        im = val.imag
        if val.imag < 0:
            str_tmp = "\t{0:15s} = {1:7.3f} - j{2:7.3f}{3:15s}{4}"
        else:
            str_tmp = "\t{0:15s} = {1:7.3f} + j{2:7.3f}{3:15s}{4}"
        cprint(str_tmp.format(name, re, abs(im), units, comment), color)

    else:
        str_tmp = "\t{0:15s} = {1:7.3f}\t{2:15s}{3}"
        cprint(str_tmp.format(name, val, units, comment), color)


def pvale(name, val, units='', comment='', color=None):  # pragma: no cover
    """ Print name, value in scientific notation and units to terminal.

    Args:
        name (str): variable name
        val (float): variable value
        units (str, optional): variable units, default is ""
        comment (str, optional): comment, default is ""
        color (str, optional): color, default is None

    """

    if units != '':
        if isinstance(val, complex):
            units = '\t[' + units + ']'
        else:
            units = '\t\t[' + units + ']'
    if comment != '':
        comment = "\t\t# {0}".format(comment)

    str_tmp = "\t{0:15s} = {1:7.1e}{2:15s}{3}"
    cprint(str_tmp.format(name, val, units, comment), color)


# Color dictionary for the terminal ------------------------------------------

_terminal_colors = {
    # colours
    'CYAN': '\033[36m',
    'MAGENTA': '\033[35m',
    'PINK': '\033[95m',
    'BLUE': '\033[94m',
    'GREEN': '\033[92m',
    'YELLOW': '\033[93m',
    'RED': '\033[91m',
    # structured colours
    'HEADER': '\033[95m',  # pink
    'HEADER1': '\033[95m',  # pink
    'HEADER2': '\033[36m',  # cyan
    'HEADER3': '\033[92m',  # green
    'OKBLUE': '\033[94m',  # blue
    'OKGREEN': '\033[92m',  # green
    'WARNING': '\033[93m',  # yellow
    'FAIL': '\033[91m',  # red
    # other
    'BOLD': '\033[1m',
    'UNDERLINE': '\033[4m',
    'INVERSE': '\033[7m',
    'BLINK': '\033[5m',
    # end of color
    'ENDC': '\033[0m'
}


# Print complex --------------------------------------------------------------

def printc(complex_number):  # pragma: no cover
    """Print a complex number to the terminal.

    Args:
        complex_number (complex): number to print

    """

    if complex_number.imag >= 0:
        sign = '+'
    else:
        sign = '-'

    return str("{:+6.2f} {} j{:5.2f}".format(complex_number.real, sign,
                                             complex_number.imag))