Skip to content

Commit 3126cac

Browse files
committed
Make output of colcon colorful
1 parent ae8cf1b commit 3126cac

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

colcon_core/event_handler/console_start_end.py

Lines changed: 26 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,33 @@ 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+
msg_template = (colorama.Style.BRIGHT + colorama.Fore.BLACK +
57+
'Finished ' + colorama.Fore.GREEN + '<<<'
58+
+ colorama.Style.RESET_ALL + colorama.Fore.CYAN
59+
+ ' {data.identifier}' + colorama.Fore.RESET
60+
+ ' [' + colorama.Fore.YELLOW +
61+
'{duration_string}' + colorama.Fore.RESET + ']')
62+
msg = msg_template.format_map(locals())
5363
job = event[1]
5464
if job in self._with_test_failures:
5565
msg += '\t[ with test failures ]'
5666
writable = sys.stdout
5767

5868
elif data.rc == SIGINT_RESULT:
59-
msg = 'Aborted <<< {data.identifier} [{duration_string}]' \
60-
.format_map(locals())
69+
msg_template = (colorama.Style.BRIGHT + colorama.Fore.RED +
70+
'Aborted ' + colorama.Style.NORMAL + '<<<'
71+
+ colorama.Fore.CYAN + ' {data.identifier}'
72+
+ colorama.Fore.RESET)
73+
msg = msg_template.format_map(locals())
6174
writable = sys.stdout
62-
6375
else:
64-
msg = 'Failed <<< {data.identifier} ' \
65-
'[{duration_string}, exited with code {data.rc}]' \
66-
.format_map(locals())
76+
msg_template = (colorama.Style.BRIGHT + colorama.Fore.RED +
77+
'Failed ' + colorama.Style.NORMAL + '<<<' +
78+
colorama.Fore.CYAN + ' {data.identifier}' +
79+
colorama.Fore.RESET + ' [' + colorama.Fore.RED +
80+
'Exited with code {data.rc}' +
81+
colorama.Fore.RESET + ']')
82+
msg = msg_template.format_map(locals())
6783
writable = sys.stderr
6884

6985
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)