Skip to content

Commit 635d94a

Browse files
committed
restore output coloring without IPython
just needed a couple ANSI constants
1 parent d9670ba commit 635d94a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ipyparallel/client/client.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from ipyparallel import error, serialize, util
5050
from ipyparallel.serialize import PrePickled, Reference
5151
from ipyparallel.util import _OutputProducingThread as Thread
52+
from ipyparallel.util import _TermColors
5253

5354
from .asyncresult import AsyncHubResult, AsyncResult
5455
from .futures import MessageFuture, multi_future
@@ -161,11 +162,30 @@ def _plaintext(self) -> str:
161162
if not text_out:
162163
return ''
163164

165+
ip = get_ipython()
166+
if ip is None:
167+
colors = "NoColor"
168+
else:
169+
colors = ip.colors
170+
171+
if colors == "NoColor":
172+
out = normal = ""
173+
else:
174+
out = _TermColors.Red
175+
normal = _TermColors.Normal
176+
164177
if '\n' in text_out and not text_out.startswith('\n'):
165178
# add newline for multiline reprs
166179
text_out = '\n' + text_out
167180

168-
return f"Out[{self.metadata['engine_id']}:{self.execution_count}]: {text_out}"
181+
return ''.join(
182+
[
183+
out,
184+
f"Out[{self.metadata['engine_id']}:{self.execution_count}]: ",
185+
normal,
186+
text_out,
187+
]
188+
)
169189

170190
def _repr_pretty_(self, p, cycle):
171191
p.text(self._plaintext())

ipyparallel/util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import sys
1414
import warnings
1515
from datetime import datetime, timezone
16+
from enum import Enum
1617
from functools import lru_cache, partial
1718
from signal import SIGABRT, SIGINT, SIGTERM, signal
1819
from threading import Thread, current_thread
@@ -836,3 +837,10 @@ def __init__(self, target, **kwargs):
836837
def _wrapped_target(self, target, *args, **kwargs):
837838
_detach_thread_output(self.ident)
838839
return target(*args, **kwargs)
840+
841+
842+
# minimal subset of TermColors, removed from IPython
843+
# not for public consumption
844+
class _TermColors(Enum):
845+
Normal = '\033[0m'
846+
Red = '\033[0;31m'

0 commit comments

Comments
 (0)