##// END OF EJS Templates
better style handling + cleanup
Laurent Dufrechou -
Show More
@@ -25,6 +25,8 b' import wx.stc as stc'
25 25 from wx.py import editwindow
26 26 import time
27 27 import sys
28 import string
29
28 30 LINESEP = '\n'
29 31 if sys.platform == 'win32':
30 32 LINESEP = '\n\r'
@@ -45,6 +47,7 b" if sys.platform == 'darwin':"
45 47 _DEFAULT_SIZE = 12
46 48
47 49 _DEFAULT_STYLE = {
50 #background definition
48 51 'stdout' : 'fore:#0000FF',
49 52 'stderr' : 'fore:#007f00',
50 53 'trace' : 'fore:#FF0000',
@@ -118,15 +121,6 b' class ConsoleWidget(editwindow.EditWindow):'
118 121 '1;34': [12, 'LIGHT BLUE'], '1;35': [13, 'MEDIUM VIOLET RED'],
119 122 '1;36': [14, 'LIGHT STEEL BLUE'], '1;37': [15, 'YELLOW']}
120 123
121 # The color of the carret (call _apply_style() after setting)
122 carret_color = 'BLACK'
123
124 _COMPLETE_BUFFER_BG = '#FAFAF1' # Nice green
125 _INPUT_BUFFER_BG = '#FDFFD3' # Nice yellow
126 _ERROR_BG = '#FFF1F1' # Nice red
127
128 background_color = 'WHITE'
129
130 124 #we define platform specific fonts
131 125 if wx.Platform == '__WXMSW__':
132 126 faces = { 'times': 'Times New Roman',
@@ -163,7 +157,7 b' class ConsoleWidget(editwindow.EditWindow):'
163 157 def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
164 158 size=wx.DefaultSize, style=wx.WANTS_CHARS, ):
165 159 editwindow.EditWindow.__init__(self, parent, id, pos, size, style)
166 self._configure_scintilla()
160 self.configure_scintilla()
167 161 self.enter_catched = False #this var track if 'enter' key as ever been processed
168 162 #thus it will only be reallowed until key goes up
169 163 self.current_prompt_pos = 0
@@ -270,24 +264,36 b' class ConsoleWidget(editwindow.EditWindow):'
270 264 # Styling API
271 265 #--------------------------------------------------------------------------
272 266
273 def set_new_style(self):
274 """ call this method with new style and ansi_style to change colors of the console """
275 self._configure_scintilla()
276
277 #--------------------------------------------------------------------------
278 # Private API
279 #--------------------------------------------------------------------------
280
281 def _configure_scintilla(self):
267 def configure_scintilla(self):
268
269 p = self.style
270
271 #First we define the special background colors
272 if '_COMPLETE_BUFFER_BG' in p:
273 _COMPLETE_BUFFER_BG = p['_COMPLETE_BUFFER_BG']
274 else:
275 _COMPLETE_BUFFER_BG = '#FAFAF1' # Nice green
276
277 if '_INPUT_BUFFER_BG' in p:
278 _INPUT_BUFFER_BG = p['_INPUT_BUFFER_BG']
279 else:
280 _INPUT_BUFFER_BG = '#FDFFD3' # Nice yellow
281
282 if '_ERROR_BG' in p:
283 _ERROR_BG = p['_ERROR_BG']
284 else:
285 _ERROR_BG = '#FFF1F1' # Nice red
286
282 287 # Marker for complete buffer.
283 288 self.MarkerDefine(_COMPLETE_BUFFER_MARKER, stc.STC_MARK_BACKGROUND,
284 background = self._COMPLETE_BUFFER_BG)
289 background = _COMPLETE_BUFFER_BG)
290
285 291 # Marker for current input buffer.
286 292 self.MarkerDefine(_INPUT_MARKER, stc.STC_MARK_BACKGROUND,
287 background = self._INPUT_BUFFER_BG)
293 background = _INPUT_BUFFER_BG)
288 294 # Marker for tracebacks.
289 295 self.MarkerDefine(_ERROR_MARKER, stc.STC_MARK_BACKGROUND,
290 background = self._ERROR_BG)
296 background = _ERROR_BG)
291 297
292 298 self.SetEOLMode(stc.STC_EOL_LF)
293 299
@@ -331,25 +337,34 b' class ConsoleWidget(editwindow.EditWindow):'
331 337 self.SetMarginWidth(1, 0)
332 338 self.SetMarginWidth(2, 0)
333 339
334 #self._apply_style()
335 self.SetCaretForeground(self.carret_color)
336
337 340 # Xterm escape sequences
338 341 self.color_pat = re.compile('\x01?\x1b\[(.*?)m\x02?')
339 342 self.title_pat = re.compile('\x1b]0;(.*?)\x07')
340 343
341 #self.SetEdgeMode(stc.STC_EDGE_LINE)
342 #self.SetEdgeColumn(80)
343
344
345 344 # styles
346 p = self.style
347 345
346 if 'carret_color' in p:
347 self.SetCaretForeground(p['carret_color'])
348 else:
349 self.SetCaretForeground('BLACK')
350
351 if 'background_color' in p:
352 background_color = p['background_color']
353 else:
354 background_color = 'WHITE'
355
348 356 if 'default' in p:
357 if 'back' not in p['default']:
358 p['default']+=',back:%s' % background_color
359 if 'size' not in p['default']:
360 p['default']+=',size:%s' % self.faces['size']
361 if 'face' not in p['default']:
362 p['default']+=',face:%s' % self.faces['mono']
363
349 364 self.StyleSetSpec(stc.STC_STYLE_DEFAULT, p['default'])
350 365 else:
351 366 self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "fore:%s,back:%s,size:%d,face:%s"
352 % (self.ANSI_STYLES['0;30'][1], self.background_color,
367 % (self.ANSI_STYLES['0;30'][1], background_color,
353 368 self.faces['size'], self.faces['mono']))
354 369
355 370 #all styles = default one
@@ -359,6 +374,22 b' class ConsoleWidget(editwindow.EditWindow):'
359 374 #for style in self.ANSI_STYLES.values():
360 375 # self.StyleSetSpec(style[0], "bold,fore:%s" % style[1])
361 376
377 #prompt definition
378 if 'prompt_in1' in p:
379 self.prompt_in1 = p['prompt_in1']
380 else:
381 self.prompt_in1 = \
382 '\n\x01\x1b[0;34m\x02In [\x01\x1b[1;34m\x02$number\x01\x1b[0;34m\x02]: \x01\x1b[0m\x02'
383
384 if 'prompt_out' in p:
385 self.prompt_out = p['prompt_out']
386 else:
387 self.prompt_out = \
388 '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02$number\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02'
389
390 self.output_prompt_template = string.Template(self.prompt_out)
391 self.input_prompt_template = string.Template(self.prompt_in1)
392
362 393 if 'stdout' in p:
363 394 self.StyleSetSpec(_STDOUT_STYLE, p['stdout'])
364 395 if 'stderr' in p:
@@ -394,6 +425,10 b' class ConsoleWidget(editwindow.EditWindow):'
394 425 if 'comment' in p:
395 426 self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, p['comment'])
396 427
428 #--------------------------------------------------------------------------
429 # Private API
430 #--------------------------------------------------------------------------
431
397 432 def _on_key_down(self, event, skip=True):
398 433 """ Key press callback used for correcting behavior for
399 434 console-like interfaces: the cursor is constraint to be after
@@ -432,7 +467,7 b' class ConsoleWidget(editwindow.EditWindow):'
432 467 if event.KeyCode in (13, wx.WXK_NUMPAD_ENTER) and \
433 468 event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN):
434 469 catched = True
435 if(not self.enter_catched):
470 if not self.enter_catched:
436 471 self.CallTipCancel()
437 472 self.write('\n', refresh=False)
438 473 # Under windows scintilla seems to be doing funny stuff to the
@@ -28,56 +28,17 b' class IPythonXController(WxController):'
28 28
29 29 def __init__(self, *args, **kwargs):
30 30
31 if kwargs['colorset'] == 'black':
32 self.prompt_in1 = \
33 '\n\x01\x1b[0;30m\x02In [\x01\x1b[1;34m\x02$number\x01\x1b[0;30m\x02]: \x01\x1b[0m\x02'
34
35 self.prompt_out = \
36 '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02$number\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02'
37
38 31 WxController.__init__(self, *args, **kwargs)
39 32 self.ipython0.ask_exit = self.do_exit
40 33
41 if kwargs['colorset'] == 'black':
42
43 self.carret_color = 'WHITE'
44 self.background_color = 'BLACK'
34 if kwargs['styledef'] is not None:
35 self.style = kwargs['styledef']
45 36
37 #we add a vertical line to console widget
46 38 self.SetEdgeMode(stc.STC_EDGE_LINE)
47 39 self.SetEdgeColumn(88)
48
49 self.style = {
50 #'stdout' : '',#fore:#0000FF',
51 #'stderr' : '',#fore:#007f00',
52 #'trace' : '',#fore:#FF0000',
53
54 #'bracegood' : 'fore:#0000FF,back:#0000FF,bold',
55 #'bracebad' : 'fore:#FF0000,back:#0000FF,bold',
56 'default' : "fore:%s,back:%s,size:%d,face:%s,bold"
57 % ("#EEEEEE", self.background_color,
58 self.faces['size'], self.faces['mono']),
59
60 # properties for the various Python lexer styles
61 'comment' : 'fore:#BBBBBB,italic',
62 'number' : 'fore:#FF9692',
63 'string' : 'fore:#ed9d13,italic',
64 'char' : 'fore:#FFFFFF,italic',
65 'keyword' : 'fore:#6AB825,bold',
66 'triple' : 'fore:#FF7BDD',
67 'tripledouble' : 'fore:#FF7BDD',
68 'class' : 'fore:#FF00FF,bold,underline',
69 'def' : 'fore:#FFFF00,bold',
70 'operator' : 'bold'
71 }
72
73 #we define the background of old inputs
74 self._COMPLETE_BUFFER_BG = '#000000' # RRGGBB: Black
75 #we define the background of current input
76 self._INPUT_BUFFER_BG = '#444444' # RRGGBB: Light black
77 #we define the background when an error is reported
78 self._ERROR_BG = '#800000' #'#d22323' #'#AE0021' # RRGGBB: Black
79
80 self.set_new_style()
40
41 self.configure_scintilla()
81 42
82 43 # Scroll to top
83 44 maxrange = self.GetScrollRange(wx.VERTICAL)
@@ -124,10 +85,10 b' class IPythonX(wx.Frame):'
124 85 """ Main frame of the IPythonX app.
125 86 """
126 87
127 def __init__(self, parent, id, title, debug=False, colorset='white'):
88 def __init__(self, parent, id, title, debug=False, style=None):
128 89 wx.Frame.__init__(self, parent, id, title, size=(300,250))
129 90 self._sizer = wx.BoxSizer(wx.VERTICAL)
130 self.shell = IPythonXController(self, debug=debug, colorset=colorset)
91 self.shell = IPythonXController(self, debug=debug, styledef=style)
131 92 self._sizer.Add(self.shell, 1, wx.EXPAND)
132 93 self.SetSizer(self._sizer)
133 94 self.SetAutoLayout(1)
@@ -163,8 +124,51 b' Simple graphical frontend to IPython, using WxWidgets."""'
163 124 import sys
164 125 sys.argv = sys.argv[:1]
165 126
127 style = None
128
129 if options.colorset == 'black':
130 style = {
131 'carret_color' : 'WHITE',
132 'background_color' : 'BLACK',
133
134 #prompt
135 'prompt_in1' : \
136 '\n\x01\x1b[0;30m\x02In [\x01\x1b[1;34m\x02$number\x01\x1b[0;30m\x02]: \x01\x1b[0m\x02',
137
138 'prompt_out' : \
139 '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02$number\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02',
140
141 #we define the background of old inputs
142 '_COMPLETE_BUFFER_BG' : '#000000', # RRGGBB: Black
143 #we define the background of current input
144 '_INPUT_BUFFER_BG' : '#444444', # RRGGBB: Light black
145 #we define the background when an error is reported
146 '_ERROR_BG' : '#800000', # RRGGBB: Light Red
147
148 #'stdout' : '',#fore:#0000FF',
149 #'stderr' : '',#fore:#007f00',
150 #'trace' : '',#fore:#FF0000',
151
152 #'bracegood' : 'fore:#0000FF,back:#0000FF,bold',
153 #'bracebad' : 'fore:#FF0000,back:#0000FF,bold',
154 'default' : "fore:%s,bold" % ("#EEEEEE"),
155
156 # properties for the various Python lexer styles
157 'comment' : 'fore:#BBBBBB,italic',
158 'number' : 'fore:#FF9692',
159 'string' : 'fore:#ed9d13,italic',
160 'char' : 'fore:#FFFFFF,italic',
161 'keyword' : 'fore:#6AB825,bold',
162 'triple' : 'fore:#FF7BDD',
163 'tripledouble' : 'fore:#FF7BDD',
164 'class' : 'fore:#FF00FF,bold,underline',
165 'def' : 'fore:#FFFF00,bold',
166 'operator' : 'bold'
167 }
168
169
166 170 app = wx.PySimpleApp()
167 frame = IPythonX(None, wx.ID_ANY, 'IPythonX', debug=options.debug, colorset=options.colorset)
171 frame = IPythonX(None, wx.ID_ANY, 'IPythonX', debug=options.debug, style=style)
168 172 frame.shell.SetFocus()
169 173 frame.shell.app = app
170 174 frame.SetSize((680, 460))
@@ -55,12 +55,7 b' class WxController(ConsoleWidget, PrefilterFrontEnd):'
55 55 This class inherits from ConsoleWidget, that provides a console-like
56 56 widget to provide a text-rendering widget suitable for a terminal.
57 57 """
58 prompt_in1 = \
59 '\n\x01\x1b[0;34m\x02In [\x01\x1b[1;34m\x02$number\x01\x1b[0;34m\x02]: \x01\x1b[0m\x02'
60
61 prompt_out = \
62 '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02$number\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02'
63
58
64 59 # Print debug info on what is happening to the console.
65 60 debug = False
66 61
@@ -131,8 +126,6 b' class WxController(ConsoleWidget, PrefilterFrontEnd):'
131 126 *args, **kwds):
132 127 """ Create Shell instance.
133 128 """
134 self.load_prompt()
135
136 129 ConsoleWidget.__init__(self, parent, id, pos, size, style)
137 130 PrefilterFrontEnd.__init__(self, **kwds)
138 131
@@ -153,12 +146,6 b' class WxController(ConsoleWidget, PrefilterFrontEnd):'
153 146 self.shell.user_ns['self'] = self
154 147 # Inject our own raw_input in namespace
155 148 self.shell.user_ns['raw_input'] = self.raw_input
156
157 def load_prompt(self):
158 self.output_prompt_template = string.Template(self.prompt_out)
159
160 self.input_prompt_template = string.Template(self.prompt_in1)
161
162 149
163 150 def raw_input(self, prompt=''):
164 151 """ A replacement from python's raw_input.
General Comments 0
You need to be logged in to leave comments. Login now