Skip to content

Commit 3d478ca

Browse files
committed
Make output of colcon colorful
1 parent ae8cf1b commit 3d478ca

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

colcon_core/event_handler/console_start_end.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import sys
55
import time
66

7+
import colorama
8+
79
from colcon_core.event.job import JobEnded
810
from colcon_core.event.job import JobStarted
911
from colcon_core.event.test import TestFailure
@@ -25,6 +27,7 @@ class ConsoleStartEndEventHandler(EventHandlerExtensionPoint):
2527

2628
def __init__(self): # noqa: D107
2729
super().__init__()
30+
colorama.init()
2831
satisfies_version(
2932
EventHandlerExtensionPoint.EXTENSION_POINT_VERSION, '^1.0')
3033
self._start_times = {}
@@ -34,8 +37,10 @@ def __call__(self, event): # noqa: D102
3437
data = event[0]
3538

3639
if isinstance(data, JobStarted):
37-
print(
38-
'Starting >>> {data.identifier}'.format_map(locals()),
40+
msg_template = ('Starting ' + colorama.Fore.GREEN +
41+
colorama.Style.BRIGHT + '>>>' + colorama.Fore.CYAN +
42+
' {data.identifier}' + colorama.Style.RESET_ALL)
43+
print(msg_template.format_map(locals()),
3944
flush=True)
4045
self._start_times[data.identifier] = time.monotonic()
4146

@@ -48,22 +53,35 @@ def __call__(self, event): # noqa: D102
4853
time.monotonic() - self._start_times[data.identifier]
4954
duration_string = format_duration(duration)
5055
if not data.rc:
51-
msg = 'Finished <<< {data.identifier} [{duration_string}]' \
52-
.format_map(locals())
56+
duration = time.time() - self._start_times[data.identifier]
57+
duration_string = format_duration(duration)
58+
msg_template = (colorama.Style.BRIGHT + colorama.Fore.BLACK +
59+
'Finished ' + colorama.Fore.GREEN + '<<<'
60+
+ colorama.Style.RESET_ALL + colorama.Fore.CYAN
61+
+ ' {data.identifier}' + colorama.Fore.RESET
62+
+ ' [' + colorama.Fore.YELLOW +
63+
'{duration_string}' + colorama.Fore.RESET + ']')
64+
msg = msg_template.format_map(locals())
5365
job = event[1]
5466
if job in self._with_test_failures:
5567
msg += '\t[ with test failures ]'
5668
writable = sys.stdout
5769

5870
elif data.rc == SIGINT_RESULT:
59-
msg = 'Aborted <<< {data.identifier} [{duration_string}]' \
60-
.format_map(locals())
71+
msg_template = (colorama.Style.BRIGHT + colorama.Fore.RED +
72+
'Aborted ' + colorama.Style.NORMAL + '<<<'
73+
+ colorama.Fore.CYAN + ' {data.identifier}'
74+
+ colorama.Fore.RESET)
75+
msg = msg_template.format_map(locals())
6176
writable = sys.stdout
62-
6377
else:
64-
msg = 'Failed <<< {data.identifier} ' \
65-
'[{duration_string}, exited with code {data.rc}]' \
66-
.format_map(locals())
78+
msg_template = (colorama.Style.BRIGHT + colorama.Fore.RED +
79+
'Failed ' + colorama.Style.NORMAL + '<<<' +
80+
colorama.Fore.CYAN + ' {data.identifier}' +
81+
colorama.Fore.RESET + ' [' + colorama.Fore.RED +
82+
'Exited with code {data.rc}' +
83+
colorama.Fore.RESET + ']')
84+
msg = msg_template.format_map(locals())
6785
writable = sys.stderr
6886

6987
print(msg, file=writable, flush=True)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ keywords = colcon
2727
[options]
2828
python_requires = >=3.5
2929
install_requires =
30+
colorama
3031
coloredlogs; sys_platform == 'win32'
3132
distlib
3233
EmPy

stdeb.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[colcon-core]
22
No-Python2:
3-
Depends3: python3-distlib, python3-empy, python3-pytest, python3-pytest-cov, python3-setuptools
3+
Depends3: python3-colorama, python3-distlib, python3-empy, python3-pytest, python3-pytest-cov, python3-setuptools
44
Suite: bionic focal jammy stretch buster bullseye
55
X-Python3-Version: >= 3.5

0 commit comments

Comments
 (0)