[FEATURE] Add ability to suppress colors via CLI arg (#1215)
Not all terminals/logs displayed colors correctly. Adds the ability to suppress any color via `--suppress-colors` cli arg.
Thanks @drewski3420 for the contribution 🎉
This commit is contained in:
parent
0c0f05e4d0
commit
d5e8157b6c
4 changed files with 40 additions and 27 deletions
|
|
@ -271,6 +271,6 @@ def main() -> List[Subscription]:
|
|||
transaction_log_file_path=args.transaction_log,
|
||||
)
|
||||
|
||||
output_summary(subscriptions)
|
||||
output_summary(subscriptions, suppress_colors=args.suppress_colors)
|
||||
|
||||
return subscriptions
|
||||
|
|
|
|||
|
|
@ -8,16 +8,16 @@ from ytdl_sub.utils.logger import Logger
|
|||
logger = Logger.get()
|
||||
|
||||
|
||||
def _green(value: str) -> str:
|
||||
return Fore.GREEN + value + Fore.RESET
|
||||
def _green(value: str, suppress_colors: bool = False) -> str:
|
||||
return value if suppress_colors else Fore.GREEN + value + Fore.RESET
|
||||
|
||||
|
||||
def _red(value: str) -> str:
|
||||
return Fore.RED + value + Fore.RESET
|
||||
def _red(value: str, suppress_colors: bool = False) -> str:
|
||||
return value if suppress_colors else Fore.RED + value + Fore.RESET
|
||||
|
||||
|
||||
def _no_color(value: str) -> str:
|
||||
return Fore.RESET + value + Fore.RESET
|
||||
def _no_color(value: str, suppress_colors: bool = False) -> str:
|
||||
return value if suppress_colors else Fore.RESET + value + Fore.RESET
|
||||
|
||||
|
||||
def _str_int(value: int) -> str:
|
||||
|
|
@ -26,21 +26,23 @@ def _str_int(value: int) -> str:
|
|||
return str(value)
|
||||
|
||||
|
||||
def _color_int(value: int) -> str:
|
||||
def _color_int(value: int, suppress_colors: bool = False) -> str:
|
||||
str_int = _str_int(value)
|
||||
if value > 0:
|
||||
return _green(str_int)
|
||||
return _green(str_int, suppress_colors)
|
||||
if value < 0:
|
||||
return _red(str_int)
|
||||
return _no_color(str_int)
|
||||
return _red(str_int, suppress_colors)
|
||||
return _no_color(str_int, suppress_colors)
|
||||
|
||||
|
||||
def output_summary(subscriptions: List[Subscription]) -> None:
|
||||
def output_summary(subscriptions: List[Subscription], suppress_colors: bool) -> None:
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
subscriptions
|
||||
Processed subscriptions
|
||||
suppress_colors
|
||||
Whether to have color or not
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
|
@ -65,21 +67,21 @@ def output_summary(subscriptions: List[Subscription]) -> None:
|
|||
|
||||
# Initialize widths to 0
|
||||
width_sub_name: int = max(len(sub.name) for sub in subscriptions) + 4 # aesthetics
|
||||
width_num_entries_added: int = len(_color_int(total_added))
|
||||
width_num_entries_modified: int = len(_color_int(total_modified))
|
||||
width_num_entries_removed: int = len(_color_int(total_removed))
|
||||
width_num_entries_added: int = len(_color_int(total_added, suppress_colors))
|
||||
width_num_entries_modified: int = len(_color_int(total_modified, suppress_colors))
|
||||
width_num_entries_removed: int = len(_color_int(total_removed, suppress_colors))
|
||||
width_num_entries: int = len(str(total_entries)) + 4 # aesthetics
|
||||
|
||||
# Build the summary
|
||||
for subscription in subscriptions:
|
||||
num_entries_added = _color_int(subscription.num_entries_added)
|
||||
num_entries_modified = _color_int(subscription.num_entries_modified)
|
||||
num_entries_removed = _color_int(subscription.num_entries_removed * -1)
|
||||
num_entries_added = _color_int(subscription.num_entries_added, suppress_colors)
|
||||
num_entries_modified = _color_int(subscription.num_entries_modified, suppress_colors)
|
||||
num_entries_removed = _color_int(subscription.num_entries_removed * -1, suppress_colors)
|
||||
num_entries = str(subscription.num_entries)
|
||||
status = (
|
||||
_red(subscription.exception.__class__.__name__)
|
||||
_red(subscription.exception.__class__.__name__, suppress_colors)
|
||||
if subscription.exception
|
||||
else _green("✔")
|
||||
else _green("✔", suppress_colors)
|
||||
)
|
||||
|
||||
summary.append(
|
||||
|
|
@ -92,14 +94,16 @@ def output_summary(subscriptions: List[Subscription]) -> None:
|
|||
)
|
||||
|
||||
total_errors_str = (
|
||||
_green("Success") if total_errors == 0 else _red(f"Error{'s' if total_errors > 1 else ''}")
|
||||
_green("Success", suppress_colors)
|
||||
if total_errors == 0
|
||||
else _red(f"Error{'s' if total_errors > 1 else ''}", suppress_colors)
|
||||
)
|
||||
|
||||
summary.append(
|
||||
f"{total_subs_str:<{width_sub_name}} "
|
||||
f"{_color_int(total_added):>{width_num_entries_added}} "
|
||||
f"{_color_int(total_modified):>{width_num_entries_modified}} "
|
||||
f"{_color_int(total_removed * -1):>{width_num_entries_removed}} "
|
||||
f"{_color_int(total_added, suppress_colors):>{width_num_entries_added}} "
|
||||
f"{_color_int(total_modified, suppress_colors):>{width_num_entries_modified}} "
|
||||
f"{_color_int(total_removed * -1, suppress_colors):>{width_num_entries_removed}} "
|
||||
f"{total_entries:>{width_num_entries}} "
|
||||
f"{total_errors_str}"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ class MainArguments:
|
|||
short="-m",
|
||||
long="--match",
|
||||
)
|
||||
SUPPRESS_COLORS = CLIArgument(short="-nc", long="--suppress-colors")
|
||||
|
||||
@classmethod
|
||||
def all(cls) -> List[CLIArgument]:
|
||||
|
|
@ -59,6 +60,7 @@ class MainArguments:
|
|||
cls.TRANSACTION_LOG,
|
||||
cls.SUPPRESS_TRANSACTION_LOG,
|
||||
cls.MATCH,
|
||||
cls.SUPPRESS_COLORS,
|
||||
]
|
||||
|
||||
@classmethod
|
||||
|
|
@ -129,6 +131,13 @@ def _add_shared_arguments(arg_parser: argparse.ArgumentParser, suppress_defaults
|
|||
help="do not output transaction logs to console or file",
|
||||
default=argparse.SUPPRESS if suppress_defaults else False,
|
||||
)
|
||||
arg_parser.add_argument(
|
||||
MainArguments.SUPPRESS_COLORS.short,
|
||||
MainArguments.SUPPRESS_COLORS.long,
|
||||
action="store_true",
|
||||
help="do not use colors in ytdl-sub output",
|
||||
default=argparse.SUPPRESS if suppress_defaults else False,
|
||||
)
|
||||
arg_parser.add_argument(
|
||||
MainArguments.MATCH.short,
|
||||
MainArguments.MATCH.long,
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ def test_output_summary_no_errors():
|
|||
]
|
||||
)
|
||||
|
||||
output_summary(subscriptions=mock_subscriptions)
|
||||
output_summary(subscriptions=mock_subscriptions, suppress_colors=False)
|
||||
|
||||
|
||||
def test_output_summary_one_error():
|
||||
|
|
@ -50,7 +50,7 @@ def test_output_summary_one_error():
|
|||
]
|
||||
)
|
||||
|
||||
output_summary(subscriptions=mock_subscriptions)
|
||||
output_summary(subscriptions=mock_subscriptions, suppress_colors=False)
|
||||
|
||||
|
||||
def test_output_summary_multiple_errors():
|
||||
|
|
@ -64,4 +64,4 @@ def test_output_summary_multiple_errors():
|
|||
]
|
||||
)
|
||||
|
||||
output_summary(subscriptions=mock_subscriptions)
|
||||
output_summary(subscriptions=mock_subscriptions, suppress_colors=True)
|
||||
|
|
|
|||
Loading…
Reference in a new issue