Skip to content

Commit

Permalink
switch to rich
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfleischer committed Jan 8, 2024
1 parent e31f86b commit 9d2a1a3
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 60 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: cloudmesh-common
dependencies:
- colorama
- rich
- humanize
- oyaml
- psutil
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ classifiers = [

dependencies = [
"python-dateutil",
"colorama",
"rich",
"humanize",
"oyaml",
"pyfiglet",
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
python-dateutil
colorama
humanize
oyaml
pyfiglet
Expand All @@ -12,4 +11,5 @@ pyyaml
psutil
pywin32; platform_system == "Windows"
pyuac; platform_system == "Windows"
tzlocal
tzlocal
rich
147 changes: 93 additions & 54 deletions src/cloudmesh/common/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import textwrap
import traceback

import colorama
from colorama import Fore, Back, Style
# import colorama
# from colorama import Fore, Back, Style

from rich.console import Console as RichConsole
# from cloudmesh.common.variables import Variables

colorama.init()
# colorama.init()

RichConsole = RichConsole()

def is_powershell():
# this function is better than the one in util
Expand Down Expand Up @@ -62,24 +64,47 @@ class Console(object):
color = True
debug = True

# old_theme_color = {
# "HEADER": Fore.MAGENTA,
# "BLACK": Fore.BLACK,
# "CYAN": Fore.CYAN,
# "WHITE": Fore.WHITE,
# "BLUE": Fore.BLUE,
# "OKBLUE": Fore.BLUE,
# "OKGREEN": Fore.GREEN,
# "GREEN": Fore.GREEN,
# "FAIL": Fore.RED,
# "WARNING": Fore.MAGENTA,
# "RED": Fore.RED,
# "ENDC": Style.RESET_ALL,
# "BOLD": Style.BRIGHT,
# "NORMAL": Style.NORMAL
# # 'ENDC': '\033[0m',
# # 'BOLD': "\033[1m",
# }

theme_color = {
"HEADER": Fore.MAGENTA,
"BLACK": Fore.BLACK,
"CYAN": Fore.CYAN,
"WHITE": Fore.WHITE,
"BLUE": Fore.BLUE,
"OKBLUE": Fore.BLUE,
"OKGREEN": Fore.GREEN,
"GREEN": Fore.GREEN,
"FAIL": Fore.RED,
"WARNING": Fore.MAGENTA,
"RED": Fore.RED,
"ENDC": Style.RESET_ALL,
"BOLD": Style.BRIGHT,
"NORMAL": Style.NORMAL
"HEADER": "magenta",
"BLACK": "black",
"CYAN": "cyan",
"WHITE": "white",
"BLUE": "blue",
"OKBLUE": "blue",
"OKGREEN": "green",
"GREEN": "green",
"FAIL": "red",
"WARNING": "magenta",
"RED": "red",
"ENDC": "default",
"BOLD": "bold",
"NORMAL": "default"
# 'ENDC': '\033[0m',
# 'BOLD': "\033[1m",
}
if is_powershell():
for key in theme_color:
theme_color[key] = "bold " + theme_color[key]
theme_color["BLUE"] = "blue on white"

theme_bw = {
"HEADER": "",
Expand Down Expand Up @@ -108,6 +133,7 @@ class Console(object):
columns = 79
lines = 24

@classmethod
def line(self, c="="):
try:
size = os.get_terminal_size()
Expand All @@ -121,20 +147,23 @@ def line(self, c="="):

# noinspection PyPep8Naming
def red(msg):
print(Fore.RED + msg + Style.RESET_ALL)
# print(Fore.RED + msg + Style.RESET_ALL)
RichConsole.print(msg, style="red")

# noinspection PyPep8Naming
def green(msg):
print(Fore.GREEN + msg + Style.RESET_ALL)
# print(Fore.GREEN + msg + Style.RESET_ALL)
RichConsole.print(msg, style="green")

# noinspection PyPep8Naming
def blue(msg):
print(Fore.BLUE + msg + Style.RESET_ALL)
# print(Fore.BLUE + msg + Style.RESET_ALL)
RichConsole.print(msg, style="blue")

@staticmethod
def init():
"""initializes the Console"""
colorama.init()
# colorama.init()

@staticmethod
def terminate():
Expand Down Expand Up @@ -242,12 +271,12 @@ def error(cls, message, prefix=True, traceflag=False):
else:
text = ""
if cls.color:
if is_powershell():
print(
Fore.RED + Back.WHITE + text + message + Console.theme_color["ENDC"]
)
else:
cls.cprint("FAIL", text, str(message))
# if is_powershell():
# print(
# Fore.RED + Back.WHITE + text + message + Console.theme_color["ENDC"]
# )
# else:
cls.cprint(cls.theme_color["FAIL"], text, str(message))
else:
print(cls.txt_msg(text + str(message)))

Expand Down Expand Up @@ -279,7 +308,7 @@ def TODO(message, prefix=True, traceflag=True):
if Console.color:
Console.cprint("FAIL", text, str(message))
else:
print(Console.msg(text + str(message)))
Console.msg(text + str(message))

trace = traceback.format_exc().strip()

Expand All @@ -302,7 +331,7 @@ def debug_msg(message):
if Console.color:
Console.cprint("RED", "DEBUG: ", message)
else:
print(Console.msg("DEBUG: " + message))
Console.msg("DEBUG: " + message)

@staticmethod
def info(message):
Expand All @@ -316,9 +345,10 @@ def info(message):
"""
message = message or ""
if Console.color:
Console.cprint("OKBLUE", "INFO: ", message)
# Console.cprint("OKBLUE", "INFO: ", message)
RichConsole.print(f"INFO: {message}", style=Console.theme_color["BLUE"])
else:
print(Console.msg("INFO: " + message))
Console.msg("INFO: " + message)

@staticmethod
def warning(message):
Expand All @@ -332,35 +362,25 @@ def warning(message):
"""
message = message or ""
if Console.color:
if is_powershell():
# fixes powershell problem https://github.com/nodejs/node/issues/14243
print(
Fore.MAGENTA
+ Style.BRIGHT
+ "WARNING: "
+ message
+ Console.theme_color["ENDC"]
)
else:
Console.cprint("WARNING", "WARNING: ", message)
Console.cprint(Console.theme_color["WARNING"], "WARNING: ", message)
else:
print(Console.msg("WARNING: " + message))
Console.msg("WARNING: " + message)

@staticmethod
def ok(message):
"""prints an ok message
Args:
message: the message<
message: the message
Returns:
"""
message = message or ""
if Console.color:
Console.cprint("OKGREEN", "", message)
Console.cprint(Console.theme_color["OKGREEN"], "", message)
else:
print(Console.msg(message))
Console.msg(message)

@staticmethod
def cprint(color="BLUE", prefix="", message=""):
Expand All @@ -377,9 +397,11 @@ def cprint(color="BLUE", prefix="", message=""):
message = message or ""
prefix = prefix or ""

print(
Console.theme_color[color] + prefix + message + Console.theme_color["ENDC"]
)
# print(
# Console.theme_color[color] + prefix + message + Console.theme_color["ENDC"]
# )

RichConsole.print(prefix + message, style=color.lower())

@staticmethod
def text(color="RED", prefix=None, message=None):
Expand All @@ -396,6 +418,20 @@ def text(color="RED", prefix=None, message=None):
message = message or ""
prefix = prefix or ""
return Console.theme[color] + prefix + message + Console.theme["ENDC"]

@staticmethod
def background(msg, background_color="white", text_color="black"):
"""prints a message in a given background color
Args:
msg: the message
background_color: the background color
text_color: the text color
Returns:
"""
RichConsole.print(msg, style=f"{text_color} on {background_color}")


#
Expand All @@ -419,9 +455,12 @@ def text(color="RED", prefix=None, message=None):
print(Console.color)
Console.error("Error")

print(Fore.RED + "some red text")
print(Back.GREEN + "and with a green background")
print(Style.DIM + "and in dim text")
print(Fore.RESET + Back.RESET + Style.RESET_ALL)
print("back to normal now")
# print(Fore.RED + "some red text")
Console.red("some red text")
# print(Back.GREEN + "and with a green background")
Console.background("and with a green background", background_color="green")
RichConsole.print("and in dim text", style="dim")
# print(Fore.RESET + Back.RESET + Style.RESET_ALL)
# print("back to normal now")
Console.msg("back to normal now")
Console.line()
4 changes: 2 additions & 2 deletions tests/test_shell_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def test_shell_head(self):
r = Shell.head(file)
Benchmark.Stop()
assert 'tqdm' in r
assert 'colorama' in r
assert 'rich' in r
assert 'tabulate' in r
r = Shell.head('requirements.txt', lines=1)
assert 'cloudmesh-sys' not in r
Expand All @@ -175,7 +175,7 @@ def test_shell_cat(self):
r = Shell.cat(file)
Benchmark.Stop()
assert 'tqdm' in r
assert 'colorama' in r
assert 'rich' in r
assert 'tabulate' in r

@pytest.mark.skipif(github_action, reason='GitHub Runner uses Azure and Azure disables ping. :( Too bad!')
Expand Down

0 comments on commit 9d2a1a3

Please sign in to comment.