Skip to content

Commit 5355c9b

Browse files
committed
More flexibility with colors.
1 parent 9cb7037 commit 5355c9b

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

‎usr/lib/python3/dist-packages/linuxmusterTools/common/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .color_shell import *
55

66
Validator = NameChecker()
7-
lprint = ColorShell()
7+
lprint = PrintShell()
88
spinner = Spinner()
99

1010
try:

‎usr/lib/python3/dist-packages/linuxmusterTools/common/color_shell.py‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,59 @@
1616

1717
class ColorShell:
1818

19+
def _color(self, color, text):
20+
return f'{color}{text}{SHELL_COLOR_ENDC}'
21+
22+
def red(self, text):
23+
return self._color(text, SHELL_COLOR_DANGER)
24+
25+
def orange(self, text):
26+
return self._color(text, SHELL_COLOR_ALERT)
27+
28+
def yellow(self, text):
29+
return self._color(text, SHELL_COLOR_WARNING)
30+
31+
def blue(self, text):
32+
return self._color(text, SHELL_COLOR_INFO)
33+
34+
def green(self, text):
35+
return self._color(text, SHELL_COLOR_SUCCESS)
36+
37+
def lmn(self, text):
38+
return self._color(text, SHELL_COLOR_LINUXMUSTER)
39+
40+
41+
class PrintShell:
42+
43+
def __init__(self):
44+
self.color_shell = ColorShell()
45+
self._color = self.color_shell._color
46+
47+
def printsh(self, color, text, end="\n"):
48+
"""
49+
Method for not so common colors. But color should be a valid ANSI control sequence.
50+
"""
51+
52+
print(self._color(color, text), end=end)
53+
1954
def danger(self, result, end="\n"):
20-
print(f'{SHELL_COLOR_DANGER}{result}{SHELL_COLOR_ENDC}', end=end)
55+
self.printsh(SHELL_COLOR_DANGER, result, end=end)
2156

2257
def alert(self, result, end="\n"):
23-
print(f'{SHELL_COLOR_ALERT}{result}{SHELL_COLOR_ENDC}', end=end)
58+
self.printsh(SHELL_COLOR_ALERT, result, end=end)
2459

2560
def warning(self, result, end="\n"):
26-
print(f'{SHELL_COLOR_WARNING}{result}{SHELL_COLOR_ENDC}', end=end)
61+
self.printsh(SHELL_COLOR_WARNING, result, end=end)
2762

2863
def info(self, result, end="\n"):
29-
print(f'{SHELL_COLOR_INFO}{result}{SHELL_COLOR_ENDC}', end=end)
64+
self.printsh(SHELL_COLOR_INFO, result, end=end)
3065

3166
def success(self, result, end="\n"):
32-
print(f'{SHELL_COLOR_SUCCESS}{result}{SHELL_COLOR_ENDC}', end=end)
67+
self.printsh(SHELL_COLOR_SUCCESS, result, end=end)
3368

3469
def lmn(self, result, end="\n"):
35-
print(f'{SHELL_COLOR_LINUXMUSTER}{result}{SHELL_COLOR_ENDC}', end=end)
36-
37-
def printsh(self, text, color, end="\n"):
38-
"""
39-
Method for not so common colors. But color should be a valid ANSI control sequence
40-
"""
41-
70+
self.printsh(SHELL_COLOR_LINUXMUSTER, result, end=end)
4271

43-
print(f'{color}{text}{SHELL_COLOR_ENDC}', end=end)
4472

4573
# Mostly copied from https://github.com/not-kennethreitz/blindspin
4674
class Spinner(object):

0 commit comments

Comments
 (0)