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 Jul 2, 2024
1 parent aa80ae5 commit 27f4c6c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
29 changes: 23 additions & 6 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 @@ -35,7 +38,10 @@ def __call__(self, event): # noqa: D102

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

elif isinstance(data, TestFailure):
job = event[1]
Expand All @@ -46,19 +52,30 @@ def __call__(self, event): # noqa: D102
time.monotonic() - self._start_times[data.identifier]
duration_string = format_duration(duration)
if not data.rc:
msg = f'Finished <<< {data.identifier} [{duration_string}]'
msg = (colorama.Style.BRIGHT + colorama.Fore.BLACK +
'Finished ' + colorama.Fore.GREEN + '<<<'
+ colorama.Style.RESET_ALL + colorama.Fore.CYAN
+ f' {data.identifier}' + colorama.Fore.RESET
+ ' [' + colorama.Fore.YELLOW +
f'{duration_string}' + colorama.Fore.RESET + ']')
job = event[1]
if job in self._with_test_failures:
msg += '\t[ with test failures ]'
writable = sys.stdout

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

else:
msg = f'Failed <<< {data.identifier} ' \
f'[{duration_string}, exited with code {data.rc}]'
msg = (colorama.Style.BRIGHT + colorama.Fore.RED +
'Failed ' + colorama.Style.NORMAL + '<<<' +
colorama.Fore.CYAN + f' {data.identifier}' +
colorama.Fore.RESET + ' [' + colorama.Fore.RED +
f'Exited with code {data.rc}' +
colorama.Fore.RESET + ']')
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.6
install_requires =
colorama
coloredlogs; sys_platform == 'win32'
distlib
EmPy<4
Expand Down
2 changes: 1 addition & 1 deletion stdeb.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[colcon-core]
No-Python2:
Depends3: python3-distlib, python3-empy (<4), python3-packaging, python3-pytest, python3-setuptools, python3 (>= 3.8) | python3-importlib-metadata
Depends3: python3-colorama, python3-distlib, python3-empy (<4), python3-packaging, python3-pytest, python3-setuptools, python3 (>= 3.8) | python3-importlib-metadata
Recommends3: python3-pytest-cov
Suggests3: python3-pytest-repeat, python3-pytest-rerunfailures
Replaces3: colcon
Expand Down

0 comments on commit 27f4c6c

Please sign in to comment.