Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2019 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of puffotter.
6puffotter is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11puffotter is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with puffotter. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20import sys
21from typing import IO, Optional
22from colorama import Style, Fore, Back
25def pprint(
26 *objects: str,
27 sep: str = " ",
28 end: str = "\n",
29 file: IO = sys.stdout,
30 flush: bool = False,
31 fg: Optional[str] = None,
32 bg: Optional[str] = None
33):
34 """
35 Function that extends the print function
36 :return: None
37 """
38 code = ""
39 if fg is not None:
40 code += __convert_color_to_code(fg, "fore")
41 if bg is not None:
42 code += __convert_color_to_code(bg, "back")
44 if code != "":
45 object_list = []
46 for i in range(0, len(objects)):
47 object_list.append(code + str(objects[i]) + Style.RESET_ALL)
48 else:
49 object_list = list(objects)
51 print(*object_list, sep=sep, end=end, file=file, flush=flush)
54def __convert_color_to_code(color_string: str, mode: str) -> str:
55 """
56 Converts a color string into the correct ANSI code
57 If the provided string does not match one of the predefined color strings,
58 the strng will be returned as is.
59 :param color_string: The string to convert
60 :param mode: The mode, either 'fore' or 'back'.
61 Otherwise a ValueError will be raised.
62 :return: The ANSI code
63 """
64 color_chart = {
65 "lred": "LIGHTRED_EX",
66 "red": "RED",
67 "lyellow": "LIGHTYELLOW_EX",
68 "yellow": "YELLOW",
69 "lblue": "LIGHTBLUE_EX",
70 "blue": "BLUE",
71 "lgreen": "LIGHTGREEN_EX",
72 "green": "GREEN",
73 "lmagenta": "LIGHTMAGENTA_EX",
74 "magenta": "MAGENTA",
75 "lcyan": "LIGHTCYAN_EX",
76 "cyan": "CYAN",
77 "lwhite": "LIGHTWHITE_EX",
78 "white": "WHITE",
79 "lblack": "LIGHTBLACK_EX",
80 "black": "BLACK"
81 }
82 code = color_chart.get(color_string.lower())
83 if code is None:
84 return color_string
85 elif mode == "fore":
86 return getattr(Fore, code)
87 elif mode == "back":
88 return getattr(Back, code)
89 else:
90 raise ValueError("Invalid Mode")