##// END OF EJS Templates
Make size of rewrite prompt flexible.
Thomas Kluyver -
Show More
@@ -232,6 +232,9 b' lazily_evaluate = {\'time\': LazyEvaluate(time.strftime, "%H:%M:%S"),'
232 [LazyEvaluate(cwd_filt, x) for x in range(1,6)],
232 [LazyEvaluate(cwd_filt, x) for x in range(1,6)],
233 'cwd_y': [LazyEvaluate(cwd_filt2, x) for x in range(6)]
233 'cwd_y': [LazyEvaluate(cwd_filt2, x) for x in range(6)]
234 }
234 }
235
236 misc_fields = {'rarrow': "-> "
237 }
235
238
236
239
237 class PromptManager(Configurable):
240 class PromptManager(Configurable):
@@ -260,7 +263,7 b' class PromptManager(Configurable):'
260 help="Continuation prompt.")
263 help="Continuation prompt.")
261 out_template = Unicode('Out[\\#]: ', config=True,
264 out_template = Unicode('Out[\\#]: ', config=True,
262 help="Output prompt. '\\#' will be transformed to the prompt number")
265 help="Output prompt. '\\#' will be transformed to the prompt number")
263 rewrite_template = Unicode("------> ", config=True,
266 rewrite_template = Unicode("{rarrow:->{txtwidth}}", config=True,
264 help="Rewrite prompt. When inputs are transformed, the rewritten input will follow this.")
267 help="Rewrite prompt. When inputs are transformed, the rewritten input will follow this.")
265
268
266 justify = Bool(True, config=True, help="""
269 justify = Bool(True, config=True, help="""
@@ -274,6 +277,7 b' class PromptManager(Configurable):'
274 # The number of characters in the last prompt rendered, not including
277 # The number of characters in the last prompt rendered, not including
275 # colour characters.
278 # colour characters.
276 width = Int()
279 width = Int()
280 txtwidth = Int() # Not including right-justification
277
281
278 # The number of characters in each prompt which don't contribute to width
282 # The number of characters in each prompt which don't contribute to width
279 invisible_chars = Dict()
283 invisible_chars = Dict()
@@ -306,35 +310,16 b' class PromptManager(Configurable):'
306 """
310 """
307 if new_template is not None:
311 if new_template is not None:
308 self.templates[name] = multiple_replace(prompt_abbreviations, new_template)
312 self.templates[name] = multiple_replace(prompt_abbreviations, new_template)
309 invis_chars = len(self.render(name, color=True, just=False)) - \
313 invis_chars = len(self._render(name, color=True)) - \
310 len(self.render(name, color=False, just=False))
314 len(self._render(name, color=False))
311 self.invisible_chars[name] = invis_chars
315 self.invisible_chars[name] = invis_chars
312
316
313 def _update_prompt_trait(self, traitname, new_template):
317 def _update_prompt_trait(self, traitname, new_template):
314 name = traitname[:-9] # Cut off '_template'
318 name = traitname[:-9] # Cut off '_template'
315 self.update_prompt(name, new_template)
319 self.update_prompt(name, new_template)
316
320
317 def render(self, name, color=True, just=None, **kwargs):
321 def _render(self, name, color=True, **kwargs):
318 """
322 """Render but don't justify, or update the width or txtwidth attributes.
319 Render the selected prompt.
320
321 Parameters
322 ----------
323 name : str
324 Which prompt to render. One of 'in', 'in2', 'out', 'rewrite'
325 color : bool
326 If True (default), include ANSI escape sequences for a coloured prompt.
327 just : bool
328 If True, justify the prompt to the width of the last prompt. The
329 default is stored in self.justify.
330 **kwargs :
331 Additional arguments will be passed to the string formatting operation,
332 so they can override the values that would otherwise fill in the
333 template.
334
335 Returns
336 -------
337 A string containing the rendered prompt.
338 """
323 """
339 if color:
324 if color:
340 scheme = self.color_scheme_table.active_colors
325 scheme = self.color_scheme_table.active_colors
@@ -362,18 +347,45 b' class PromptManager(Configurable):'
362 count = self.shell.execution_count # Shorthand
347 count = self.shell.execution_count # Shorthand
363 # Build the dictionary to be passed to string formatting
348 # Build the dictionary to be passed to string formatting
364 fmtargs = dict(color=colors, count=count,
349 fmtargs = dict(color=colors, count=count,
365 dots="."*len(str(count)) )
350 dots="."*len(str(count)),
351 width=self.width, txtwidth=self.txtwidth )
366 fmtargs.update(self.lazy_evaluate_fields)
352 fmtargs.update(self.lazy_evaluate_fields)
353 fmtargs.update(misc_fields)
367 fmtargs.update(kwargs)
354 fmtargs.update(kwargs)
368
355
369 # Prepare the prompt
356 # Prepare the prompt
370 prompt = colors.prompt + self.templates[name] + colors.normal
357 prompt = colors.prompt + self.templates[name] + colors.normal
371
358
372 # Fill in required fields
359 # Fill in required fields
373 res = prompt.format(**fmtargs)
360 return prompt.format(**fmtargs)
361
362 def render(self, name, color=True, just=None, **kwargs):
363 """
364 Render the selected prompt.
365
366 Parameters
367 ----------
368 name : str
369 Which prompt to render. One of 'in', 'in2', 'out', 'rewrite'
370 color : bool
371 If True (default), include ANSI escape sequences for a coloured prompt.
372 just : bool
373 If True, justify the prompt to the width of the last prompt. The
374 default is stored in self.justify.
375 **kwargs :
376 Additional arguments will be passed to the string formatting operation,
377 so they can override the values that would otherwise fill in the
378 template.
379
380 Returns
381 -------
382 A string containing the rendered prompt.
383 """
384 res = self._render(name, color=color, **kwargs)
374
385
375 # Handle justification of prompt
386 # Handle justification of prompt
376 invis_chars = self.invisible_chars[name] if color else 0
387 invis_chars = self.invisible_chars[name] if color else 0
388 self.txtwidth = len(res) - invis_chars
377 just = self.justify if (just is None) else just
389 just = self.justify if (just is None) else just
378 if just:
390 if just:
379 res = res.rjust(self.width + invis_chars)
391 res = res.rjust(self.width + invis_chars)
General Comments 0
You need to be logged in to leave comments. Login now