Skip to content

Commit 56ad44e

Browse files
authored
Merge branch 'main' into feature/atracsys-visualizer-holohub
2 parents 10c37fe + 09cfd82 commit 56ad44e

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ repos:
2929

3030
# Python: ruff
3131
- repo: https://github.com/astral-sh/ruff-pre-commit
32-
rev: v0.15.10
32+
rev: v0.15.11
3333
hooks:
3434
- id: ruff
3535
args: [--fix, --ignore, E712]

utilities/cli/util.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@
5555

5656

5757
class Color:
58-
"""Utility class for terminal color formatting"""
58+
"""Utility class for terminal color formatting.
59+
60+
ANSI codes are emitted based on the destination stream and environment:
61+
- NO_COLOR=<non-empty> always strip colors (no-color.org)
62+
- FORCE_COLOR=<non-empty> always emit colors
63+
- otherwise: emit only when the destination stream is a TTY
64+
65+
Callers that write to stderr should pass stream=sys.stderr so the TTY
66+
check targets the correct destination.
67+
"""
5968

6069
# ANSI color codes
6170
RED = "\033[31m"
@@ -70,8 +79,23 @@ class Color:
7079
RESET = "\033[0m"
7180

7281
@staticmethod
73-
def format(text: str, color: str, bold: bool = False) -> str:
74-
"""Format text with color and optional bold attribute"""
82+
def _should_color(stream=None) -> bool:
83+
if os.environ.get("NO_COLOR"):
84+
return False
85+
if os.environ.get("FORCE_COLOR"):
86+
return True
87+
stream = stream or sys.stdout
88+
return hasattr(stream, "isatty") and stream.isatty()
89+
90+
@staticmethod
91+
def format(text: str, color: str, bold: bool = False, stream=None) -> str:
92+
"""Format text with color and optional bold attribute.
93+
94+
Returns plain text (no ANSI codes) when the destination stream is not
95+
a TTY, or when NO_COLOR is set. Set FORCE_COLOR to override.
96+
"""
97+
if not Color._should_color(stream):
98+
return text
7599
result = color
76100
if bold:
77101
result += Color.BOLD
@@ -81,8 +105,8 @@ def format(text: str, color: str, bold: bool = False) -> str:
81105
def _create_color_method(color_code: str):
82106
"""Create a color method for the given color code"""
83107

84-
def color_method(text: str, bold: bool = False) -> str:
85-
return Color.format(text, color_code, bold)
108+
def color_method(text: str, bold: bool = False, stream=None) -> str:
109+
return Color.format(text, color_code, bold, stream=stream)
86110

87111
return color_method
88112

@@ -148,16 +172,19 @@ def check_skip_builds(args) -> Tuple[bool, bool]:
148172

149173
def fatal(message: str) -> None:
150174
"""Print fatal error and exit with backtrace"""
175+
err = sys.stderr
151176
print(
152-
f"{Color.red(get_timestamp())} {Color.red('[FATAL]', bold=True)} {message}", file=sys.stderr
177+
f"{Color.red(get_timestamp(), stream=err)} "
178+
f"{Color.red('[FATAL]', bold=True, stream=err)} {message}",
179+
file=err,
153180
)
154-
print("\nBacktrace: ...", file=sys.stderr)
155-
traceback.print_list(traceback.extract_stack()[-3:], file=sys.stderr)
181+
print("\nBacktrace: ...", file=err)
182+
traceback.print_list(traceback.extract_stack()[-3:], file=err)
156183
sys.exit(1)
157184

158185

159186
def warn(message: str) -> None:
160-
print(f"{Color.yellow('WARNING:')} {message}", file=sys.stderr)
187+
print(f"{Color.yellow('WARNING:', stream=sys.stderr)} {message}", file=sys.stderr)
161188

162189

163190
def _get_holohub_root() -> Path:

utilities/requirements.lint.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ codespell==2.4.2
44
cpplint==2.0.2
55
isort==8.0.1
66
mypy==1.20.1
7-
ruff==0.15.10
8-
clang-format==22.*
7+
ruff==0.15.11
8+
clang-format==22.1.*

0 commit comments

Comments
 (0)