##// 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 232 [LazyEvaluate(cwd_filt, x) for x in range(1,6)],
233 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 240 class PromptManager(Configurable):
@@ -260,7 +263,7 b' class PromptManager(Configurable):'
260 263 help="Continuation prompt.")
261 264 out_template = Unicode('Out[\\#]: ', config=True,
262 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 267 help="Rewrite prompt. When inputs are transformed, the rewritten input will follow this.")
265 268
266 269 justify = Bool(True, config=True, help="""
@@ -274,6 +277,7 b' class PromptManager(Configurable):'
274 277 # The number of characters in the last prompt rendered, not including
275 278 # colour characters.
276 279 width = Int()
280 txtwidth = Int() # Not including right-justification
277 281
278 282 # The number of characters in each prompt which don't contribute to width
279 283 invisible_chars = Dict()
@@ -306,35 +310,16 b' class PromptManager(Configurable):'
306 310 """
307 311 if new_template is not None:
308 312 self.templates[name] = multiple_replace(prompt_abbreviations, new_template)
309 invis_chars = len(self.render(name, color=True, just=False)) - \
310 len(self.render(name, color=False, just=False))
313 invis_chars = len(self._render(name, color=True)) - \
314 len(self._render(name, color=False))
311 315 self.invisible_chars[name] = invis_chars
312 316
313 317 def _update_prompt_trait(self, traitname, new_template):
314 318 name = traitname[:-9] # Cut off '_template'
315 319 self.update_prompt(name, new_template)
316 320
317 def render(self, name, color=True, just=None, **kwargs):
318 """
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.
321 def _render(self, name, color=True, **kwargs):
322 """Render but don't justify, or update the width or txtwidth attributes.
338 323 """
339 324 if color:
340 325 scheme = self.color_scheme_table.active_colors
@@ -362,18 +347,45 b' class PromptManager(Configurable):'
362 347 count = self.shell.execution_count # Shorthand
363 348 # Build the dictionary to be passed to string formatting
364 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 352 fmtargs.update(self.lazy_evaluate_fields)
353 fmtargs.update(misc_fields)
367 354 fmtargs.update(kwargs)
368 355
369 356 # Prepare the prompt
370 357 prompt = colors.prompt + self.templates[name] + colors.normal
371 358
372 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 386 # Handle justification of prompt
376 387 invis_chars = self.invisible_chars[name] if color else 0
388 self.txtwidth = len(res) - invis_chars
377 389 just = self.justify if (just is None) else just
378 390 if just:
379 391 res = res.rjust(self.width + invis_chars)
General Comments 0
You need to be logged in to leave comments. Login now