From f05336f45af8a0ae93977a9b3f15d622338fb9cb Mon Sep 17 00:00:00 2001 From: Jakub Frejlach Date: Mon, 13 Nov 2023 16:34:40 +0100 Subject: [PATCH] Implement Color/Style helper This helper is supposed to format text color/style in cases where rich cannot be used (exceptions for example) --- griffon/commands/queries.py | 21 +++++++++++------ griffon/helpers.py | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/griffon/commands/queries.py b/griffon/commands/queries.py index 78f2710..e7a5461 100644 --- a/griffon/commands/queries.py +++ b/griffon/commands/queries.py @@ -40,6 +40,7 @@ generate_entity_report, generate_license_report, ) +from griffon.helpers import Style from griffon.output import ( console, cprint, @@ -188,12 +189,12 @@ def retrieve_component_summary(ctx, component_name, strict_name_search): # @click.option( # "--cve-id", # "cve_id", -# help="Attach affects to this (\033[1mCVE-ID\033[0m).", +# help=f"Attach affects to this {Style.BOLD}CVE-ID{Style.RESET}.", # ) @click.option( "--sfm2-flaw-id", "sfm2_flaw_id", - help="Attach affects to this (\033[1msfm2 flaw id\033[0m).", + help=f"Attach affects to this {Style.BOLD}sfm2 flaw id{Style.RESET}.", ) @click.option( "--flaw-mode", @@ -207,28 +208,34 @@ def retrieve_component_summary(ctx, component_name, strict_name_search): "search_latest", is_flag=True, default=False, - help="Search root Components (\033[1menabled by default\033[0m).", + help=f"Search root Components {Style.BOLD}(enabled by default){Style.RESET}.", ) @click.option( "--search-provides", "search_provides", is_flag=True, default=False, - help="Search root Components by provides children(\033[1menabled by default\033[0m).", + help=( + f"Search root Components by provides children " + "{Style.BOLD}(enabled by default){Style.RESET}." + ), ) @click.option( "--search-upstreams", "search_upstreams", is_flag=True, default=False, - help="Search root Components by upstreams children (\033[1menabled by default\033[0m).", + help=( + f"Search root Components by upstreams children " + "{Style.BOLD}(enabled by default){Style.RESET}." + ), ) @click.option( "--search-related-url", "search_related_url", is_flag=True, default=False, - help="Search related url (\033[1menabled by default\033[0m).", + help=f"Search related url {Style.BOLD}(enabled by default){Style.RESET}.", ) @click.option( "--filter-rh-naming", @@ -357,7 +364,7 @@ def get_product_contain_component( if not purl and not component_name: click.echo(ctx.get_help()) click.echo("") - click.echo("\033[1mMust supply Component name or --purl.\033[0m") + click.echo(f"{Style.BOLD}Must supply Component name or --purl.{Style.RESET}") exit(0) if ( diff --git a/griffon/helpers.py b/griffon/helpers.py index f1822b6..c006976 100644 --- a/griffon/helpers.py +++ b/griffon/helpers.py @@ -2,6 +2,7 @@ Helpers for direct usage or debbuging """ import json +from enum import Enum from typing import Callable, Optional, Type, Union from component_registry_bindings.bindings.python_client.types import ( @@ -61,3 +62,47 @@ def debug_data_load( else: data = json_data return data + + +class Color(Enum): + """ + Helper enum for text color formatting for anything which + cannot be rendered using rich + """ + + BLACK = "\033[30m" + RED = "\033[31m" + GREEN = "\033[32m" + YELLOW = "\033[33m" + BLUE = "\033[34m" + MAGENTA = "\033[35m" + CYAN = "\033[36m" + WHITE = "\033[37m" + GREY = "\033[90m" + BRIGHT_RED = "\033[91m" + BRIGHT_GREEN = "\033[92m" + BRIGHT_YELLOW = "\033[93m" + BRIGHT_BLUE = "\033[94m" + BRIGHT_MAGENTA = "\033[95m" + BRIGHT_CYAN = "\033[96m" + BRIGHT_WHITE = "\033[97m" + RESET = "\033[0m" # Reset to default color and style + + def __str__(self): + return str(self.value) + + +class Style(Enum): + """ + Helper enum for text style formatting for anything which + cannot be rendered using rich + """ + + BOLD = "\033[1m" + ITALIC = "\033[3m" + UNDERLINE = "\033[4m" + STRIKE = "\033[9m" + RESET = "\033[0m" # Reset to default style + + def __str__(self): + return str(self.value)