Skip to content

Commit

Permalink
Make output of colcon colorful
Browse files Browse the repository at this point in the history
  • Loading branch information
timonegk committed Feb 26, 2022
1 parent ae8cf1b commit 3126cac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
36 changes: 26 additions & 10 deletions colcon_core/event_handler/console_start_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import time

import colorama

from colcon_core.event.job import JobEnded
from colcon_core.event.job import JobStarted
from colcon_core.event.test import TestFailure
Expand All @@ -25,6 +27,7 @@ class ConsoleStartEndEventHandler(EventHandlerExtensionPoint):

def __init__(self): # noqa: D107
super().__init__()
colorama.init()
satisfies_version(
EventHandlerExtensionPoint.EXTENSION_POINT_VERSION, '^1.0')
self._start_times = {}
Expand All @@ -34,8 +37,10 @@ def __call__(self, event): # noqa: D102
data = event[0]

if isinstance(data, JobStarted):
print(
'Starting >>> {data.identifier}'.format_map(locals()),
msg_template = ('Starting ' + colorama.Fore.GREEN +
colorama.Style.BRIGHT + '>>>' + colorama.Fore.CYAN +
' {data.identifier}' + colorama.Style.RESET_ALL)
print(msg_template.format_map(locals()),
flush=True)
self._start_times[data.identifier] = time.monotonic()

Expand All @@ -48,22 +53,33 @@ def __call__(self, event): # noqa: D102
time.monotonic() - self._start_times[data.identifier]
duration_string = format_duration(duration)
if not data.rc:
msg = 'Finished <<< {data.identifier} [{duration_string}]' \
.format_map(locals())
msg_template = (colorama.Style.BRIGHT + colorama.Fore.BLACK +
'Finished ' + colorama.Fore.GREEN + '<<<'
+ colorama.Style.RESET_ALL + colorama.Fore.CYAN
+ ' {data.identifier}' + colorama.Fore.RESET
+ ' [' + colorama.Fore.YELLOW +
'{duration_string}' + colorama.Fore.RESET + ']')
msg = msg_template.format_map(locals())
job = event[1]
if job in self._with_test_failures:
msg += '\t[ with test failures ]'
writable = sys.stdout

elif data.rc == SIGINT_RESULT:
msg = 'Aborted <<< {data.identifier} [{duration_string}]' \
.format_map(locals())
msg_template = (colorama.Style.BRIGHT + colorama.Fore.RED +
'Aborted ' + colorama.Style.NORMAL + '<<<'
+ colorama.Fore.CYAN + ' {data.identifier}'
+ colorama.Fore.RESET)
msg = msg_template.format_map(locals())
writable = sys.stdout

else:
msg = 'Failed <<< {data.identifier} ' \
'[{duration_string}, exited with code {data.rc}]' \
.format_map(locals())
msg_template = (colorama.Style.BRIGHT + colorama.Fore.RED +
'Failed ' + colorama.Style.NORMAL + '<<<' +
colorama.Fore.CYAN + ' {data.identifier}' +
colorama.Fore.RESET + ' [' + colorama.Fore.RED +
'Exited with code {data.rc}' +
colorama.Fore.RESET + ']')
msg = msg_template.format_map(locals())
writable = sys.stderr

print(msg, file=writable, flush=True)
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ keywords = colcon
[options]
python_requires = >=3.5
install_requires =
colorama
coloredlogs; sys_platform == 'win32'
distlib
EmPy
Expand Down
2 changes: 1 addition & 1 deletion stdeb.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[colcon-core]
No-Python2:
Depends3: python3-distlib, python3-empy, python3-pytest, python3-pytest-cov, python3-setuptools
Depends3: python3-colorama, python3-distlib, python3-empy, python3-pytest, python3-pytest-cov, python3-setuptools
Suite: bionic focal jammy stretch buster bullseye
X-Python3-Version: >= 3.5

0 comments on commit 3126cac

Please sign in to comment.