Skip to content

Commit 9919725

Browse files
committed
make Repl.write allow \x04 as a delimiter so multiple colours can be passed in
one line. also provide a fallback if colour init fails
1 parent ce6117f commit 9919725

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

bpython/cli.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,12 @@ def resize(self):
10341034

10351035
def write(self, s):
10361036
"""For overriding stdout defaults"""
1037+
if '\x04' in s:
1038+
for block in s.split('\x04'):
1039+
self.write(block)
1040+
return
10371041
if s.rstrip() and '\x03' in s:
1038-
t = s.split('\x03')[1]
1042+
t = s.split('\x03')[1]
10391043
else:
10401044
t = s
10411045

@@ -1764,6 +1768,14 @@ def loadrc():
17641768
if hasattr(OPTS, k):
17651769
setattr(OPTS, k, v)
17661770

1771+
class FakeDict(object):
1772+
"""Very simple dict-alike that returns a constant value for any key -
1773+
used as a hacky solution to using a colours dict containing colour codes if
1774+
colour initialisation fails."""
1775+
def __init__(self, val):
1776+
self._val = val
1777+
def __getitem__(self, k):
1778+
return self._val
17671779

17681780
def main_curses(scr):
17691781
"""main function for the curses convenience wrapper
@@ -1781,9 +1793,12 @@ def main_curses(scr):
17811793
signal.signal(signal.SIGWINCH, lambda *_: sigwinch(scr))
17821794
loadrc()
17831795
stdscr = scr
1784-
curses.start_color()
1785-
curses.use_default_colors()
1786-
cols = make_colours()
1796+
try:
1797+
curses.start_color()
1798+
curses.use_default_colors()
1799+
cols = make_colours()
1800+
except curses.error:
1801+
cols = FakeDict(-1)
17871802

17881803
scr.timeout(300)
17891804

0 commit comments

Comments
 (0)