Skip to content

Commit 4e40cae

Browse files
committed
Cleanup diagnostics.diag function. NFC
Now the ANSI colors are always used we can make this function much simpler. Followup to #25502.
1 parent d020153 commit 4e40cae

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

tools/diagnostics.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,29 @@ def reset_color():
5757
return '\033[0m'
5858

5959

60-
def diag(level, msg, *args):
61-
# Format output message as:
62-
# <tool>: <level>: msg
63-
# With the `<level>:` part being colored accordingly.
64-
sys.stderr.write(tool_name + ': ')
60+
def with_bold_color(color, string):
61+
if not color_enabled:
62+
return string
63+
return output_color(color) + bold() + string + reset_color()
6564

66-
if color_enabled:
67-
output = output_color(level_colors[level]) + bold()
68-
if output:
69-
sys.stderr.write(output)
7065

71-
sys.stderr.write(level_prefixes[level])
66+
def with_bold(string):
67+
if not color_enabled:
68+
return string
69+
return bold() + string + reset_color()
7270

73-
if color_enabled:
74-
output = reset_color() + bold()
75-
if output:
76-
sys.stderr.write(output)
7771

72+
def diag(level, msg, *args):
73+
# Format output message as:
74+
# <tool>: <level>: msg
75+
# With the `<level>:` part being colored accordingly, and the message itself in bold.
76+
prefix = level_prefixes[level]
77+
color = level_colors[level]
7878
if args:
7979
msg = msg % args
80-
sys.stderr.write(str(msg))
81-
sys.stderr.write('\n')
8280

83-
if color_enabled:
84-
output = reset_color()
85-
if output:
86-
sys.stderr.write(output)
81+
full_msg = f'{tool_name}: {with_bold_color(color, prefix)}{with_bold(msg)}\n'
82+
sys.stderr.write(full_msg)
8783

8884

8985
def error(msg, *args):

0 commit comments

Comments
 (0)