Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update colors to PrintColors and made enum and updated uses of it #1214

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions mil_common/utils/mil_tools/mil_misc_tools/text_effects.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from enum import Enum
from typing import Callable, Optional


class Colors:
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
purple = "\033[35m"
cyan = "\033[36m"
white = "\033[37m"
class PrintColors(Enum):
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
PURPLE = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"

underline = "\033[2m"
bold = "\033[1m"
negative = "\033[3m"
UNDERLINE = "\033[2m"
BOLD = "\033[1m"
NEGATIVE = "\033[3m"

reset = "\033[0m"
RESET = "\033[0m"

def __getattr__(self, arg):
# If we get a non existent color, return the reset color
return self.reset
def get(cls, name, default=None):
return cls.__members__.get(name.upper(), default)


class Printer:
Expand All @@ -30,30 +30,30 @@ def __init__(self, string: Optional[str] = "", autospace: bool = False):
self.text = lambda text: self + text

# Colors
self.red = lambda text: self + (Colors.red + str(text) + Colors.reset)
self.green = lambda text: self + (Colors.green + str(text) + Colors.reset)
self.yellow = lambda text: self + (Colors.yellow + str(text) + Colors.reset)
self.blue = lambda text: self + (Colors.blue + str(text) + Colors.reset)
self.purple = lambda text: self + (Colors.purple + str(text) + Colors.reset)
self.cyan = lambda text: self + (Colors.cyan + str(text) + Colors.reset)
self.white = lambda text: self + (Colors.white + str(text) + Colors.reset)
self.red = lambda text: self + (PrintColors.RED.value + str(text) + PrintColors.RESET.value)
self.green = lambda text: self + (PrintColors.GREEN.value + str(text) + PrintColors.RESET.value)
self.yellow = lambda text: self + (PrintColors.YELLOW.value + str(text) + PrintColors.RESET.value)
self.blue = lambda text: self + (PrintColors.BLUE.value + str(text) + PrintColors.RESET.value)
self.purple = lambda text: self + (PrintColors.PURPLE.value + str(text) + PrintColors.RESET.value)
self.cyan = lambda text: self + (PrintColors.CYAN.value + str(text) + PrintColors.RESET.value)
self.white = lambda text: self + (PrintColors.WHITE.value + str(text) + PrintColors.RESET.value)

# Text effects
self.underline = lambda text: Printer(
self._string + Colors.underline + str(text) + Colors.reset,
self._string + PrintColors.UNDERLINE.value + str(text) + PrintColors.RESET.value,
)
self.bold = lambda text: Printer(
self._string + Colors.bold + str(text) + Colors.reset,
self._string + PrintColors.BOLD.value + str(text) + PrintColors.RESET.value,
)
self.negative = lambda text: Printer(
self._string + Colors.negative + str(text) + Colors.reset,
self._string + PrintColors.NEGATIVE.value + str(text) + PrintColors.RESET.value,
)

# For passing in custom formatting
self.custom = lambda text, effect: self + (effect + str(text) + Colors.reset)
self.custom = lambda text, effect: self + (effect + str(text) + PrintColors.RESET.value)

def __repr__(self):
return self._string + Colors.reset
return self._string + PrintColors.RESET.value

__str__ = __repr__

Expand All @@ -63,35 +63,35 @@ def __add__(self, other):

@property
def set_red(self):
return Printer(self._string + Colors.red)
return Printer(self._string + PrintColors.RED.value)

@property
def set_green(self):
return Printer(self._string + Colors.green)
return Printer(self._string + PrintColors.GREEN.value)

@property
def set_yellow(self):
return Printer(self._string + Colors.yellow)
return Printer(self._string + PrintColors.YELLOW.value)

@property
def set_blue(self):
return Printer(self._string + Colors.blue)
return Printer(self._string + PrintColors.BLUE.value)

@property
def set_purple(self):
return Printer(self._string + Colors.purple)
return Printer(self._string + PrintColors.PURPLE.value)

@property
def set_cyan(self):
return Printer(self._string + Colors.cyan)
return Printer(self._string + PrintColors.CYAN.value)

@property
def set_white(self):
return Printer(self._string + Colors.white)
return Printer(self._string + PrintColors.WHITE.value)

@property
def reset(self):
return Printer(self._string + Colors.reset)
return Printer(self._string + PrintColors.RESET.value)

def space(self, count=1):
return Printer(self._string + " " * count)
Expand Down Expand Up @@ -189,7 +189,7 @@ def fprint(self, text: str, **kwargs):
text = str(self.printer.bold(text))

if msg_color is not None:
message = message.custom(text, getattr(Colors, msg_color))
message = message.custom(text, getattr(PrintColors, msg_color))
else:
message = message.text(text)

Expand Down
Loading