##// END OF EJS Templates
Further cleanup of prompts code - docstrings, etc.
Thomas Kluyver -
Show More
@@ -5,6 +5,7 b' Authors:'
5
5
6 * Fernando Perez
6 * Fernando Perez
7 * Brian Granger
7 * Brian Granger
8 * Thomas Kluyver
8 """
9 """
9
10
10 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
@@ -81,7 +82,7 b' PColLightBG.colors.update('
81
82
82 class LazyEvaluate(object):
83 class LazyEvaluate(object):
83 """This is used for formatting strings with values that need to be updated
84 """This is used for formatting strings with values that need to be updated
84 at that time, such as the current time or line number."""
85 at that time, such as the current time or working directory."""
85 def __init__(self, func, *args, **kwargs):
86 def __init__(self, func, *args, **kwargs):
86 self.func = func
87 self.func = func
87 self.args = args
88 self.args = args
@@ -193,26 +194,6 b' prompt_abbreviations = {'
193 # More utilities
194 # More utilities
194 #-----------------------------------------------------------------------------
195 #-----------------------------------------------------------------------------
195
196
196 def str_safe(arg):
197 """Convert to a string, without ever raising an exception.
198
199 If str(arg) fails, <ERROR: ... > is returned, where ... is the exception
200 error message."""
201
202 try:
203 out = str(arg)
204 except UnicodeError:
205 try:
206 out = arg.encode('utf_8','replace')
207 except Exception,msg:
208 # let's keep this little duplication here, so that the most common
209 # case doesn't suffer from a double try wrapping.
210 out = '<ERROR: %s>' % msg
211 except Exception,msg:
212 out = '<ERROR: %s>' % msg
213 #raise # dbg
214 return out
215
216 def cwd_filt(self, depth):
197 def cwd_filt(self, depth):
217 """Return the last depth elements of the current working directory.
198 """Return the last depth elements of the current working directory.
218
199
@@ -254,20 +235,23 b' lazily_evaluate = {\'time\': LazyEvaluate(time.strftime, "%H:%M:%S"),'
254
235
255
236
256 class PromptManager(Configurable):
237 class PromptManager(Configurable):
238 """This is the primary interface for producing IPython's prompts."""
257 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
239 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
258
240
259 color_scheme_table = Instance(coloransi.ColorSchemeTable)
241 color_scheme_table = Instance(coloransi.ColorSchemeTable)
260 color_scheme = Unicode('Linux')
242 color_scheme = Unicode('Linux', config=True)
261 def _color_scheme_changed(self, name, new_value):
243 def _color_scheme_changed(self, name, new_value):
262 self.color_scheme_table.set_active_scheme(new_value)
244 self.color_scheme_table.set_active_scheme(new_value)
263 for pname in ['in', 'in2', 'out', 'rewrite']:
245 for pname in ['in', 'in2', 'out', 'rewrite']:
264 # We need to recalculate the number of invisible characters
246 # We need to recalculate the number of invisible characters
265 self.update_prompt(pname)
247 self.update_prompt(pname)
266
248
267 # These fields can be referenced in prompt templates, and are evaluated
249 lazy_evaluate_fields = Dict(help="""
268 # when the prompt is generated - for things like timestamps. They are only
250 This maps field names used in the prompt templates to functions which
269 # evaluated if a prompt uses them.
251 will be called when the prompt is rendered. This allows us to include
270 lazy_evaluate_fields = Dict()
252 things like the current time in the prompts. Functions are only called
253 if they are used in the prompt.
254 """)
271 def _lazy_evaluate_fields_default(self): return lazily_evaluate.copy()
255 def _lazy_evaluate_fields_default(self): return lazily_evaluate.copy()
272
256
273 in_template = Unicode('In [\\#]: ', config=True)
257 in_template = Unicode('In [\\#]: ', config=True)
@@ -275,8 +259,10 b' class PromptManager(Configurable):'
275 out_template = Unicode('Out[\\#]: ', config=True)
259 out_template = Unicode('Out[\\#]: ', config=True)
276 rewrite_template = Unicode("------> ", config=True)
260 rewrite_template = Unicode("------> ", config=True)
277
261
278 # Justify prompts by default?
262 justify = Bool(True, config=True, help="""
279 justify = Bool(True)
263 If True (default), each prompt will be right-aligned with the
264 preceding one.
265 """)
280
266
281 # We actually store the expanded templates here:
267 # We actually store the expanded templates here:
282 templates = Dict()
268 templates = Dict()
@@ -306,6 +292,14 b' class PromptManager(Configurable):'
306 'in2_template', 'out_template', 'rewrite_template'])
292 'in2_template', 'out_template', 'rewrite_template'])
307
293
308 def update_prompt(self, name, new_template=None):
294 def update_prompt(self, name, new_template=None):
295 """This is called when a prompt template is updated. It processes
296 abbreviations used in the prompt template (like \#) and calculates how
297 many invisible characters (ANSI colour escapes) the resulting prompt
298 contains.
299
300 It is also called for each prompt on changing the colour scheme. In both
301 cases, traitlets should take care of calling this automatically.
302 """
309 if new_template is not None:
303 if new_template is not None:
310 self.templates[name] = multiple_replace(prompt_abbreviations, new_template)
304 self.templates[name] = multiple_replace(prompt_abbreviations, new_template)
311 invis_chars = len(self.render(name, color=True, just=False)) - \
305 invis_chars = len(self.render(name, color=True, just=False)) - \
@@ -344,6 +338,12 b' class PromptManager(Configurable):'
344 colors = color_lists['normal']
338 colors = color_lists['normal']
345 colors.number, colors.prompt, colors.normal = \
339 colors.number, colors.prompt, colors.normal = \
346 scheme.out_number, scheme.out_prompt, scheme.normal
340 scheme.out_number, scheme.out_prompt, scheme.normal
341 elif name=='rewrite':
342 colors = color_lists['normal']
343 # We need a non-input version of these escapes
344 colors.number = scheme.in_number.replace("\001","").replace("\002","")
345 colors.prompt = scheme.in_prompt.replace("\001","").replace("\002","")
346 colors.normal = scheme.normal
347 else:
347 else:
348 colors = color_lists['inp']
348 colors = color_lists['inp']
349 colors.number, colors.prompt, colors.normal = \
349 colors.number, colors.prompt, colors.normal = \
General Comments 0
You need to be logged in to leave comments. Login now