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

Logging - force colours if certain env vars are set. #760

Merged
merged 4 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions nf_core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion nf_core/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion nf_core/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions nf_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import nf_core.utils

import os
import unittest


Expand Down Expand Up @@ -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