Skip to content

Commit 4b7328d

Browse files
committed
make *all* colours not be hardcoded, now we can have dark/light alternatives
for all colours in a centralised place. also make the background forced (i.e. white or black for dark/light colour scheme)
1 parent 2073f4f commit 4b7328d

File tree

1 file changed

+81
-25
lines changed

1 file changed

+81
-25
lines changed

bpython/cli.py

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,48 @@ def DEBUG(s):
137137
def make_colours():
138138
"""Init all the colours in curses and bang them into a dictionary"""
139139

140+
OPTS.light = {
141+
'listwin': 'b',
142+
'args': 'g',
143+
'kwvalue': 'b',
144+
'self': 'r',
145+
'*args': 'g',
146+
'**kwargs': 'g',
147+
'punctuation': 'k',
148+
'paren': 'y',
149+
'function': 'r',
150+
'more': 'g',
151+
'prompt': 'r',
152+
'output': 'k',
153+
'statusbar': 'r',
154+
}
155+
OPTS.dark = {
156+
'listwin': 'c',
157+
'args': 'g',
158+
'kwvalue': 'b',
159+
'self': 'r',
160+
'*args': 'g',
161+
'**kwargs': 'g',
162+
'punctuation': 'm',
163+
'paren': 'y',
164+
'function': 'r',
165+
'more': 'y',
166+
'prompt': 'g',
167+
'output': 'w',
168+
'statusbar': 'c',
169+
}
170+
if OPTS.color_scheme == 'light':
171+
bg = 7
172+
OPTS.extras = OPTS.light
173+
else:
174+
bg = 0
175+
OPTS.extras = OPTS.dark
176+
140177
for i in range(63):
141178
if i > 7:
142179
j = i / 8
143180
else:
144-
j = -1
181+
j = bg
145182
curses.init_pair(i+1, i % 8, j)
146183

147184
# blacK, Red, Green, Yellow, Blue, Magenta, Cyan, White, Default:
@@ -156,6 +193,7 @@ def make_colours():
156193
'w' : 7,
157194
'd' : -1,
158195
}
196+
c.update((k, c[v]) for k, v in OPTS.extras.iteritems())
159197
return c
160198

161199

@@ -305,7 +343,7 @@ def __init__(self, scr, interp, statusbar=None, idle=None):
305343
# side-effects (__getattr__/__getattribute__)
306344
self.completer._callable_postfix = self._callable_postfix
307345
self.statusbar = statusbar
308-
self.list_win = curses.newwin(1, 1, 1, 1)
346+
self.list_win = newwin(1, 1, 1, 1)
309347
self.idle = idle
310348
self.f_string = ''
311349
self.matches = []
@@ -676,7 +714,7 @@ def lsize():
676714
padding = (wl - len(i)) * ' '
677715
self.list_win.addstr(
678716
i + padding,
679-
curses.color_pair(self._C["c"]+1))
717+
curses.color_pair(self._C["listwin"]+1))
680718
if ((cols == 1 or (ix and not (ix+1) % cols))
681719
and ix + 1 < len(v_items)):
682720
self.list_win.addstr('\n ')
@@ -721,8 +759,8 @@ def mkargspec(self, topline, down):
721759

722760
self.list_win.addstr('\n ')
723761
self.list_win.addstr(fn,
724-
curses.color_pair(self._C["b"]+1) | curses.A_BOLD)
725-
self.list_win.addstr(': (', curses.color_pair(self._C["y"]+1))
762+
curses.color_pair(self._C["function"]+1) | curses.A_BOLD)
763+
self.list_win.addstr(': (', curses.color_pair(self._C["paren"]+1))
726764
maxh = self.scr.getmaxyx()[0]
727765

728766
for k, i in enumerate(args):
@@ -748,31 +786,33 @@ def mkargspec(self, topline, down):
748786
self.list_win.addstr('\n\t')
749787

750788
if str(i) == 'self' and k == 0:
751-
color = self._C["r"]
789+
color = self._C["self"]
752790
else:
753-
color = self._C["g"]
791+
color = self._C["args"]
754792

755793
self.list_win.addstr(str(i),
756794
curses.color_pair(color + 1) | curses.A_BOLD)
757795
if kw:
758-
self.list_win.addstr('=', curses.color_pair(self._C["c"]+1))
759-
self.list_win.addstr(kw, curses.color_pair(self._C["g"]+1))
796+
self.list_win.addstr('=',
797+
curses.color_pair(self._C["punctuation"]+1))
798+
self.list_win.addstr(kw, curses.color_pair(self._C["kwvalue"]+1))
760799
if k != len(args) -1:
761-
self.list_win.addstr(', ', curses.color_pair(self._C["g"]+1))
800+
self.list_win.addstr(', ',
801+
curses.color_pair(self._C["punctuation"]+1))
762802

763803
if _args:
764804
if args:
765805
self.list_win.addstr(', ',
766-
curses.color_pair(self._C["g"]+1))
806+
curses.color_pair(self._C["args"]+1))
767807
self.list_win.addstr('*%s' % (_args, ),
768-
curses.color_pair(self._C["m"]+1))
808+
curses.color_pair(self._C["*args"]+1))
769809
if _kwargs:
770810
if args or _args:
771811
self.list_win.addstr(', ',
772-
curses.color_pair(self._C["g"]+1))
812+
curses.color_pair(self._C["punctuation"]+1))
773813
self.list_win.addstr('**%s' % (_kwargs, ),
774-
curses.color_pair(self._C["m"]+1))
775-
self.list_win.addstr(')', curses.color_pair(self._C["y"]+1))
814+
curses.color_pair(self._C["**kwargs"]+1))
815+
self.list_win.addstr(')', curses.color_pair(self._C["paren"]+1))
776816

777817
return r
778818

@@ -966,13 +1006,13 @@ def reevaluate(self):
9661006
def prompt(self, more):
9671007
"""Show the appropriate Python prompt"""
9681008
if not more:
969-
self.echo("\x01g\x03>>> ")
1009+
self.echo("\x01%s\x03>>> " % (OPTS.extras['prompt'],))
9701010
self.stdout_hist += '>>> '
971-
self.s_hist.append('\x01g\x03>>> \x04')
1011+
self.s_hist.append('\x01%s\x03>>> \x04' % (OPTS.extras['prompt'],))
9721012
else:
973-
self.echo("\x01r\x03... ")
1013+
self.echo("\x01%s\x03... " % (OPTS.extras['more'],))
9741014
self.stdout_hist += '... '
975-
self.s_hist.append('\x01r\x03... \x04')
1015+
self.s_hist.append('\x01%s\x03... \x04' % (OPTS.extras['more'],))
9761016

9771017
def repl(self):
9781018
"""Initialise the repl and jump into the loop. This method also has to
@@ -1094,7 +1134,7 @@ def echo(self, s, redraw=True):
10941134
if isinstance(s, unicode):
10951135
s = s.encode(getpreferredencoding())
10961136

1097-
a = curses.color_pair(0)
1137+
a = curses.color_pair(colors[OPTS.extras['output']]+1)
10981138
if '\x01' in s:
10991139
rx = re.search('\x01([a-z])([a-z]?)', s)
11001140
if rx:
@@ -1104,7 +1144,6 @@ def echo(self, s, redraw=True):
11041144

11051145
a = curses.color_pair(int(p) + 1)
11061146
s = re.sub('\x01[a-z][a-z]?', '', s)
1107-
11081147
if '\x02' in s:
11091148
a = a | curses.A_BOLD
11101149
s = s.replace('\x02', '')
@@ -1555,7 +1594,7 @@ class Statusbar(object):
15551594
def __init__(self, scr, pwin, s=None, c=None):
15561595
"""Initialise the statusbar and display the initial text (if any)"""
15571596
self.size()
1558-
self.win = curses.newwin(self.h, self.w, self.y, self.x)
1597+
self.win = newwin(self.h, self.w, self.y, self.x)
15591598

15601599
self.s = s or ''
15611600
self._s = self.s
@@ -1681,7 +1720,7 @@ def init_wins(scr, cols):
16811720

16821721
h, w = gethw()
16831722

1684-
main_win = curses.newwin(h-1, w, 0, 0)
1723+
main_win = newwin(h-1, w, 0, 0)
16851724
main_win.scrollok(True)
16861725
main_win.keypad(1)
16871726
# Thanks to Angus Gibson for pointing out this missing line which was causing
@@ -1693,7 +1732,7 @@ def init_wins(scr, cols):
16931732
#
16941733
statusbar = Statusbar(scr, main_win,
16951734
".:: <C-d> Exit <C-r> Rewind <F2> Save <F8> Pastebin ::.",
1696-
(cols["g"]) * cols["y"] + 1)
1735+
cols[OPTS.extras['statusbar']] + 1)
16971736

16981737
return main_win, statusbar
16991738

@@ -1850,6 +1889,19 @@ def __init__(self, val):
18501889
def __getitem__(self, k):
18511890
return self._val
18521891

1892+
def newwin(*args):
1893+
"""Wrapper for curses.newwin to automatically set background colour on any
1894+
newly created window."""
1895+
win = curses.newwin(*args)
1896+
if OPTS.color_scheme == 'light':
1897+
bg = colors['w']
1898+
else:
1899+
bg = colors['k']
1900+
colpair = curses.color_pair(bg)
1901+
win.bkgd(' ', colpair)
1902+
return win
1903+
1904+
18531905
def main_curses(scr):
18541906
"""main function for the curses convenience wrapper
18551907
@@ -1862,6 +1914,7 @@ def main_curses(scr):
18621914
"""
18631915
global stdscr
18641916
global DO_RESIZE
1917+
global colors
18651918
DO_RESIZE = False
18661919

18671920
signal.signal(signal.SIGWINCH, lambda *_: sigwinch(scr))
@@ -1874,11 +1927,13 @@ def main_curses(scr):
18741927
except curses.error:
18751928
cols = FakeDict(-1)
18761929

1930+
# FIXME: Gargh, bad design results in using globals without a refactor :(
1931+
colors = cols
1932+
18771933
scr.timeout(300)
18781934

18791935
main_win, statusbar = init_wins(scr, cols)
18801936

1881-
18821937
interpreter = Interpreter()
18831938

18841939
repl = Repl(main_win, interpreter, statusbar, idle)
@@ -1954,6 +2009,7 @@ def main(args=None):
19542009
sys.stdout.write(o)
19552010
sys.stdout.flush()
19562011

2012+
19572013
if __name__ == '__main__':
19582014
main()
19592015

0 commit comments

Comments
 (0)