diff --git a/nf_core/__main__.py b/nf_core/__main__.py index 6eebbe8815..ec3c1aa2a6 100755 --- a/nf_core/__main__.py +++ b/nf_core/__main__.py @@ -34,7 +34,7 @@ def run_nf_core(): rich.traceback.install(width=200, word_wrap=True) # Print nf-core header to STDERR - stderr = rich.console.Console(file=sys.stderr) + stderr = rich.console.Console(file=sys.stderr, force_terminal=nf_core.utils.rich_force_colors()) stderr.print("\n[green]{},--.[grey39]/[green],-.".format(" " * 42), highlight=False) stderr.print("[blue] ___ __ __ __ ___ [green]/,-._.--~\\", highlight=False) stderr.print("[blue] |\ | |__ __ / ` / \ |__) |__ [yellow] } {", highlight=False) @@ -114,7 +114,7 @@ def nf_core_cli(verbose, log_file): log.addHandler( rich.logging.RichHandler( level=logging.DEBUG if verbose else logging.INFO, - console=rich.console.Console(file=sys.stderr), + console=rich.console.Console(file=sys.stderr, force_terminal=nf_core.utils.rich_force_colors()), show_time=False, markup=True, ) diff --git a/nf_core/launch.py b/nf_core/launch.py index 488d19c224..162cf5062c 100644 --- a/nf_core/launch.py +++ b/nf_core/launch.py @@ -640,7 +640,7 @@ def validate_pattern(val): def print_param_header(self, param_id, param_obj): if "description" not in param_obj and "help_text" not in param_obj: return - console = Console() + console = Console(nf_core.utils.rich_force_colors()) console.print("\n") console.print(param_obj.get("title", param_id), style="bold") if "description" in param_obj: diff --git a/nf_core/lint.py b/nf_core/lint.py index 9b62da3209..1fb0aeac69 100755 --- a/nf_core/lint.py +++ b/nf_core/lint.py @@ -1307,7 +1307,7 @@ def check_schema_params(self): def print_results(self, show_passed=False): log.debug("Printing final results") - console = Console() + console = Console(force_terminal=nf_core.utils.rich_force_colors()) # Helper function to format test links nicely def format_result(test_results, table): diff --git a/nf_core/utils.py b/nf_core/utils.py index c0eb461e33..f09c4bd3cb 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -43,6 +43,15 @@ def check_if_outdated(current_version=None, remote_version=None, source_url="htt return (is_outdated, current_version, remote_version) +def rich_force_colors(): + """ + Check if any environment variables are set to force Rich to use coloured output + """ + if os.getenv("GITHUB_ACTIONS") or os.getenv("FORCE_COLOR") or os.getenv("PY_COLORS"): + return True + return None + + def fetch_wf_config(wf_path): """Uses Nextflow to retrieve the the configuration variables from a Nextflow workflow. diff --git a/tests/test_utils.py b/tests/test_utils.py index 9c73919ad3..b533abb7a1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,6 +4,7 @@ import nf_core.utils +import os import unittest @@ -39,3 +40,15 @@ def test_check_if_outdated_5(self): remote_version = "1.11" is_outdated, current, remote = nf_core.utils.check_if_outdated(current_version, remote_version) assert is_outdated + + def test_rich_force_colours_false(self): + os.environ.pop("GITHUB_ACTIONS", None) + os.environ.pop("FORCE_COLOR", None) + os.environ.pop("PY_COLORS", None) + assert nf_core.utils.rich_force_colors() is None + + def test_rich_force_colours_true(self): + os.environ["GITHUB_ACTIONS"] = "1" + os.environ.pop("FORCE_COLOR", None) + os.environ.pop("PY_COLORS", None) + assert nf_core.utils.rich_force_colors() is True