Skip to content

Commit 5df2517

Browse files
colored numbers
1 parent 7e9987c commit 5df2517

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

tests/conftest.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# under the Apache License Version 2.0, see <https://www.apache.org/licenses/>
33
import pickle
44
import shutil
5+
import re
56
from logging import warning
67
from pathlib import Path
78
from shutil import rmtree
@@ -85,22 +86,26 @@ def finalize_fixture_store(request, fixture_store):
8586
global harvested_fixture_data
8687
harvested_fixture_data = dict(fixture_store)
8788

89+
def strip_ansi_escape_codes(text):
90+
ansi_escape = re.compile(r'\x1b\[.*?m')
91+
return ansi_escape.sub('', text)
92+
93+
94+
# Function to center text with ANSI colors, adjusting for escape codes
95+
def center_colored_text(text, width):
96+
visible_length = len(strip_ansi_escape_codes(text))
97+
padding = max(0, (width - visible_length) // 2)
98+
return " " * padding + text + " " * (width - visible_length - padding)
99+
88100

89101
def pytest_terminal_summary(terminalreporter, exitstatus, config):
90102
"""
91-
Custom pytest terminal summary to display mini SBIBM results with relative coloring
92-
per task.
103+
Custom pytest terminal summary to display mini SBIBM results with relative coloring per task.
93104
94105
This function is called after the test session ends and generates a summary
95106
of the results if the `--bm` option is specified. It displays the results
96107
in a formatted table with methods as rows and tasks as columns, applying
97108
relative coloring to metrics based on their performance within each task.
98-
99-
Args:
100-
terminalreporter (TerminalReporter): The terminal reporter object for writing
101-
output.
102-
exitstatus (int): The exit status of the test session.
103-
config (Config): The pytest config object.
104109
"""
105110
if config.getoption("--bm"):
106111
terminal_width = shutil.get_terminal_size().columns
@@ -174,15 +179,26 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
174179
)
175180

176181
# Determine color based on normalized value
177-
if normalized_val == 1.0:
182+
if normalized_val == 0.0:
178183
color = "\033[92m" # Green for best
179-
elif normalized_val == 0.0:
184+
elif normalized_val == 1.0:
180185
color = "\033[91m" # Red for worst
181186
else:
182187
color = f"\033[9{int(2 + normalized_val * 3)}m"
183188

184189
val_str = format(val, ".3f")
185-
row += f"{color}{val_str}\033[0m".center(task_col_widths[t] + 2)
190+
colored_val_str = f"{color}{val_str}\033[0m"
191+
192+
# Correct spacing by adjusting for visible length
193+
# padding = (
194+
# task_col_widths[t]
195+
# + 2
196+
# - len(strip_ansi_escape_codes(colored_val_str))
197+
# )
198+
row += center_colored_text(
199+
colored_val_str, task_col_widths[t] + 2
200+
)
201+
186202
terminalreporter.write_line(row)
187203
else:
188204
terminalreporter.write_line("No harvested fixture data found yet.")

0 commit comments

Comments
 (0)