From b00547cd9763a06f95f0ce457aef02428981298f Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Sun, 4 Dec 2022 13:51:56 +0000 Subject: [PATCH] Workaround the Github forgetting styles bug --- cibuildwheel/__main__.py | 7 ++++++- cibuildwheel/options.py | 4 ++++ cibuildwheel/util.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/cibuildwheel/__main__.py b/cibuildwheel/__main__.py index afe46d2e..99af21b4 100644 --- a/cibuildwheel/__main__.py +++ b/cibuildwheel/__main__.py @@ -22,9 +22,11 @@ from cibuildwheel.typing import PLATFORMS, PlatformName, assert_never from cibuildwheel.util import ( CIBW_CACHE_PATH, BuildSelector, + CIProvider, Unbuffered, chdir, detect_ci_provider, + fix_ansi_codes_for_github_actions, ) @@ -319,7 +321,10 @@ def print_preamble(platform: str, options: Options, identifiers: list[str]) -> N print("Build options:") print(f" platform: {platform}") - print(textwrap.indent(options.summary(identifiers), " ")) + options_summary = textwrap.indent(options.summary(identifiers), " ") + if detect_ci_provider() == CIProvider.github_actions: + options_summary = fix_ansi_codes_for_github_actions(options_summary) + print(options_summary) print() print(f"Cache folder: {CIBW_CACHE_PATH}") diff --git a/cibuildwheel/options.py b/cibuildwheel/options.py index a0894b17..0f2852e9 100644 --- a/cibuildwheel/options.py +++ b/cibuildwheel/options.py @@ -669,6 +669,10 @@ class Options: default_value: Any, overrides: dict[str, Any] | None = None, ) -> str: + """ + Return a summary of the option value, including any overrides, with + ANSI 'dim' color if it's the default. + """ value_str = self.option_summary_value(option_value) default_value_str = self.option_summary_value(default_value) overrides_value_strs = { diff --git a/cibuildwheel/util.py b/cibuildwheel/util.py index fa1af89b..ca83d964 100644 --- a/cibuildwheel/util.py +++ b/cibuildwheel/util.py @@ -672,3 +672,31 @@ def chdir(new_path: Path | str) -> Generator[None, None, None]: yield finally: os.chdir(cwd) + + +def fix_ansi_codes_for_github_actions(text: str) -> str: + """ + Github Actions forgets the current ANSI style on every new line. This + function repeats the current ANSI style on every new line. + """ + ansi_code_regex = re.compile(r"(\033\[[0-9;]*m)") + ansi_codes: list[str] = [] + output = "" + + for line in text.split("\n"): + # add the current ANSI codes to the beginning of the line + output += "".join(ansi_codes) + line + "\n" + + # split the line at each ANSI code + parts = ansi_code_regex.split(line) + # if there are any ANSI codes, save them + if len(parts) > 1: + # iterate over the ANSI codes in this line + for code in parts[1::2]: + if code == "\033[0m": + # reset the list of ANSI codes when the clear code is found + ansi_codes = [] + else: + ansi_codes.append(code) + + return output