##// END OF EJS Templates
adopt traitlets 4.2 API...
adopt traitlets 4.2 API - `.tag(config=True)` - `@default` - `@observe`

File last commit:

r22340:04d48146
r22340:04d48146
Show More
prompts.py
409 lines | 15.4 KiB | text/x-python | PythonLexer
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # -*- coding: utf-8 -*-
Min RK
adopt traitlets 4.2 API...
r22340 """Classes for handling input/output prompts."""
Brian Granger
Refactor of prompts and the displayhook....
r2781
Min RK
adopt traitlets 4.2 API...
r22340 # Copyright (c) 2001-2007 Fernando Perez <fperez@colorado.edu>
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
fperez
Cosmetic cleanups: put all imports in a single line, and sort them...
r52 import os
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 import re
fperez
Cosmetic cleanups: put all imports in a single line, and sort them...
r52 import socket
import sys
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 import time
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
MinRK
allow access to user_ns in prompt_manager...
r5724 from string import Formatter
Min RK
update dependency imports...
r21253 from traitlets.config.configurable import Configurable
Brian Granger
Release.py => core/release.py and imports updated.
r2043 from IPython.core import release
MinRK
support unicode in LazyEvaluate...
r7571 from IPython.utils import coloransi, py3compat
Min RK
adopt traitlets 4.2 API...
r22340 from traitlets import Unicode, Instance, Dict, Bool, Int, observe, default
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Matthias Bussonnier
Consolidate color scheme localisations.
r21778 from IPython.utils.PyColorize import LightBGColors, LinuxColors, NoColor
Brian Granger
Refactor of prompts and the displayhook....
r2781 #-----------------------------------------------------------------------------
# Color schemes for prompts
#-----------------------------------------------------------------------------
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Brian Granger
ColorANSI.py -> utils/coloransi.py and all imports updated.
r2010 InputColors = coloransi.InputTermColors # just a shorthand
Colors = coloransi.TermColors # just a shorthand
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 color_lists = dict(normal=Colors(), inp=InputColors(), nocolor=coloransi.NoColors())
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 #-----------------------------------------------------------------------------
Brian Granger
Refactor of prompts and the displayhook....
r2781 # Utilities
#-----------------------------------------------------------------------------
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 class LazyEvaluate(object):
"""This is used for formatting strings with values that need to be updated
Thomas Kluyver
Further cleanup of prompts code - docstrings, etc.
r5496 at that time, such as the current time or working directory."""
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 def __init__(self, func, *args, **kwargs):
self.func = func
self.args = args
self.kwargs = kwargs
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 def __call__(self, **kwargs):
self.kwargs.update(kwargs)
return self.func(*self.args, **self.kwargs)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 def __str__(self):
MinRK
use cleaner, less safe, unicode/str in LazyEvaluate
r7581 return str(self())
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
MinRK
support unicode in LazyEvaluate...
r7571 def __unicode__(self):
Thomas Kluyver
Replace references to unicode and basestring
r13353 return py3compat.unicode_type(self())
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
MinRK
support unicode in LazyEvaluate...
r7571 def __format__(self, format_spec):
MinRK
fix format of LazyEvaluate based on *actual* review
r7578 return format(self(), format_spec)
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 def multiple_replace(dict, text):
Matthias Bussonnier
Consolidate color scheme localisations.
r21778 """ Replace in 'text' all occurrences of any key in the given
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 dictionary by its corresponding value. Returns the new string."""
# Function by Xavier Defrang, originally found at:
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
#-----------------------------------------------------------------------------
# Special characters that can be used in prompt templates, mainly bash-like
Brian Granger
Refactor of prompts and the displayhook....
r2781 #-----------------------------------------------------------------------------
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
# If $HOME isn't defined (Windows), make it an absurd string so that it can
# never be expanded out into '~'. Basically anything which can never be a
# reasonable directory name will do, we just want the $HOME -> '~' operation
# to become a no-op. We pre-compute $HOME here so it's not done on every
# prompt call.
# FIXME:
# - This should be turned into a class which does proper namespace management,
# since the prompt specials need to be evaluated in a certain namespace.
# Currently it's just globals, which need to be managed manually by code
# below.
# - I also need to split up the color schemes from the prompt specials
# somehow. I don't have a clean design for that quite yet.
MinRK
cast all env in prompts to unicode
r7582 HOME = py3compat.str_to_unicode(os.environ.get("HOME","//////:::::ZZZZZ,,,~~~"))
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Paul Ivanov
fix another FreeBSD $HOME symlink issue
r7602 # This is needed on FreeBSD, and maybe other systems which symlink /home to
# /usr/home, but retain the $HOME variable as pointing to /home
HOME = os.path.realpath(HOME)
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # We precompute a few more strings here for the prompt_specials, which are
# fixed once ipython starts. This reduces the runtime overhead of computing
# prompt strings.
MinRK
USER in prompts default to '' instead of None
r7585 USER = py3compat.str_to_unicode(os.environ.get("USER",''))
MinRK
cast all env in prompts to unicode
r7582 HOSTNAME = py3compat.str_to_unicode(socket.gethostname())
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 HOSTNAME_SHORT = HOSTNAME.split(".")[0]
Doug Blank
Minimal changes to import IPython from IronPython
r15154
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829 # IronPython doesn't currently have os.getuid() even if
Doug Blank
Summary of changes:...
r15208 # os.name == 'posix'; 2/8/2014
Doug Blank
Minimal changes to import IPython from IronPython
r15154 ROOT_SYMBOL = "#" if (os.name=='nt' or sys.platform=='cli' or os.getuid()==0) else "$"
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 prompt_abbreviations = {
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Prompt/history count
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 '%n' : '{color.number}' '{count}' '{color.prompt}',
r'\#': '{color.number}' '{count}' '{color.prompt}',
fperez
- Add \N escape for the actual prompt number, without any coloring.
r579 # Just the prompt counter number, WITHOUT any coloring wrappers, so users
# can get numbers displayed in whatever color they want.
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 r'\N': '{count}',
Fernando Perez
Add tests to ensure that %run does not modify __builtins__...
r1953
Wouter Bolsterlee
Add \S prompt char for whitespace as wide as the count (similar to \D)
r21752 # Prompt/history count, with the actual digits replaced by dots or
# spaces. Used mainly in continuation prompts (prompt_in2).
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 r'\D': '{dots}',
Wouter Bolsterlee
Add \S prompt char for whitespace as wide as the count (similar to \D)
r21752 r'\S': '{spaces}',
Fernando Perez
Ensure that __builtin__ is always available and compute prompts from it....
r2485
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Current time
Thomas Kluyver
Use \T for timestamp in templates to distinguish it from \t = <tab>
r5497 r'\T' : '{time}',
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # Current working directory
r'\w': '{cwd}',
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Basename of current working directory.
# (use os.sep to make this portable across OSes)
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 r'\W' : '{cwd_last}',
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # These X<N> are an extension to the normal bash prompts. They return
# N terms of the path, after replacing $HOME with '~'
Thomas Kluyver
Fix bugs in calling cwd_filt and cwd_filt2 for prompts.
r5498 r'\X0': '{cwd_x[0]}',
r'\X1': '{cwd_x[1]}',
r'\X2': '{cwd_x[2]}',
r'\X3': '{cwd_x[3]}',
r'\X4': '{cwd_x[4]}',
r'\X5': '{cwd_x[5]}',
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Y<N> are similar to X<N>, but they show '~' if it's the directory
# N+1 in the list. Somewhat like %cN in tcsh.
Thomas Kluyver
Fix bugs in calling cwd_filt and cwd_filt2 for prompts.
r5498 r'\Y0': '{cwd_y[0]}',
r'\Y1': '{cwd_y[1]}',
r'\Y2': '{cwd_y[2]}',
r'\Y3': '{cwd_y[3]}',
r'\Y4': '{cwd_y[4]}',
r'\Y5': '{cwd_y[5]}',
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Hostname up to first .
fperez
- Add \N escape for the actual prompt number, without any coloring.
r579 r'\h': HOSTNAME_SHORT,
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Full hostname
fperez
- Add \N escape for the actual prompt number, without any coloring.
r579 r'\H': HOSTNAME,
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Username of current user
fperez
- Add \N escape for the actual prompt number, without any coloring.
r579 r'\u': USER,
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Escaped '\'
'\\\\': '\\',
# Newline
fperez
- Add \N escape for the actual prompt number, without any coloring.
r579 r'\n': '\n',
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Carriage return
fperez
- Add \N escape for the actual prompt number, without any coloring.
r579 r'\r': '\r',
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Release version
Brian Granger
Release.py => core/release.py and imports updated.
r2043 r'\v': release.version,
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 # Root symbol ($ or #)
fperez
- Add \N escape for the actual prompt number, without any coloring.
r579 r'\$': ROOT_SYMBOL,
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 }
#-----------------------------------------------------------------------------
Brian Granger
Refactor of prompts and the displayhook....
r2781 # More utilities
#-----------------------------------------------------------------------------
Thomas Kluyver
Fix bugs in calling cwd_filt and cwd_filt2 for prompts.
r5498 def cwd_filt(depth):
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 """Return the last depth elements of the current working directory.
fperez
- new doctest_mode magic to toggle doctest pasting/prompts....
r763
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 $HOME is always replaced with '~'.
If depth==0, the full path is returned."""
fperez
- new doctest_mode magic to toggle doctest pasting/prompts....
r763
Thomas Kluyver
Python 3 compatibility for os.getcwdu()
r13447 cwd = py3compat.getcwd().replace(HOME,"~")
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 out = os.sep.join(cwd.split(os.sep)[-depth:])
return out or os.sep
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Thomas Kluyver
Fix bugs in calling cwd_filt and cwd_filt2 for prompts.
r5498 def cwd_filt2(depth):
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 """Return the last depth elements of the current working directory.
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 $HOME is always replaced with '~'.
If depth==0, the full path is returned."""
fperez
- new doctest_mode magic to toggle doctest pasting/prompts....
r763
Thomas Kluyver
Python 3 compatibility for os.getcwdu()
r13447 full_cwd = py3compat.getcwd()
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 cwd = full_cwd.replace(HOME,"~").split(os.sep)
if '~' in cwd and len(cwd) == depth+1:
depth += 1
drivepart = ''
if sys.platform == 'win32' and len(cwd) > depth:
drivepart = os.path.splitdrive(full_cwd)[0]
out = drivepart + '/'.join(cwd[-depth:])
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 return out or os.sep
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 #-----------------------------------------------------------------------------
# Prompt classes
#-----------------------------------------------------------------------------
vivainio
prompt and set_term_title now include drive letter and / characters on win32
r794
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 lazily_evaluate = {'time': LazyEvaluate(time.strftime, "%H:%M:%S"),
Thomas Kluyver
Python 3 compatibility for os.getcwdu()
r13447 'cwd': LazyEvaluate(py3compat.getcwd),
'cwd_last': LazyEvaluate(lambda: py3compat.getcwd().split(os.sep)[-1]),
'cwd_x': [LazyEvaluate(lambda: py3compat.getcwd().replace(HOME,"~"))] +\
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 [LazyEvaluate(cwd_filt, x) for x in range(1,6)],
'cwd_y': [LazyEvaluate(cwd_filt2, x) for x in range(6)]
}
Thomas Kluyver
Fix for prompts containing newlines....
r5657
def _lenlastline(s):
"""Get the length of the last line. More intelligent than
len(s.splitlines()[-1]).
"""
if not s or s.endswith(('\n', '\r')):
return 0
return len(s.splitlines()[-1])
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495
MinRK
allow access to user_ns in prompt_manager...
r5724
Aaron Meurer
Be a little smarter about invisible characters in terminal prompts...
r21605 invisible_chars_re = re.compile('\001[^\001\002]*\002')
def _invisible_characters(s):
"""
Get the number of invisible ANSI characters in s. Invisible characters
must be delimited by \001 and \002.
"""
return _lenlastline(s) - _lenlastline(invisible_chars_re.sub('', s))
MinRK
allow access to user_ns in prompt_manager...
r5724 class UserNSFormatter(Formatter):
"""A Formatter that falls back on a shell's user_ns and __builtins__ for name resolution"""
def __init__(self, shell):
self.shell = shell
def get_value(self, key, args, kwargs):
# try regular formatting first:
try:
return Formatter.get_value(self, key, args, kwargs)
except Exception:
pass
# next, look in user_ns and builtins:
for container in (self.shell.user_ns, __builtins__):
if key in container:
return container[key]
# nothing found, put error message in its place
return "<ERROR: '%s' not found>" % key
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 class PromptManager(Configurable):
Thomas Kluyver
Further cleanup of prompts code - docstrings, etc.
r5496 """This is the primary interface for producing IPython's prompts."""
Sylvain Corlay
allow_none=False by default for Type and Instance
r20940 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Sylvain Corlay
allow_none=False by default for Type and Instance
r20940 color_scheme_table = Instance(coloransi.ColorSchemeTable, allow_none=True)
Matthias Bussonnier
Update IPython/core/prompts.py to traitlets 4.2 API
r22323 color_scheme = Unicode('Linux').tag(config=True)
@observe('color_scheme')
def _color_scheme_changed(self, change):
self.color_scheme_table.set_active_scheme(change['new'])
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 for pname in ['in', 'in2', 'out', 'rewrite']:
# We need to recalculate the number of invisible characters
self.update_prompt(pname)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Further cleanup of prompts code - docstrings, etc.
r5496 lazy_evaluate_fields = Dict(help="""
This maps field names used in the prompt templates to functions which
will be called when the prompt is rendered. This allows us to include
things like the current time in the prompts. Functions are only called
if they are used in the prompt.
""")
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Matthias Bussonnier
Fix Traitlets API 42. Help is in constructor, not tag.
r22337 in_template = Unicode('In [\\#]: ',
help="Input prompt. '\\#' will be transformed to the prompt number"
).tag(config=True)
in2_template = Unicode(' .\\D.: ',
help="Continuation prompt.").tag(config=True)
out_template = Unicode('Out[\\#]: ',
help="Output prompt. '\\#' will be transformed to the prompt number"
).tag(config=True)
Min RK
adopt traitlets 4.2 API...
r22340 @default('lazy_evaluate_fields')
def _lazy_evaluate_fields_default(self):
return lazily_evaluate.copy()
Matthias Bussonnier
Fix Traitlets API 42. Help is in constructor, not tag.
r22337 justify = Bool(True, help="""
Thomas Kluyver
Further cleanup of prompts code - docstrings, etc.
r5496 If True (default), each prompt will be right-aligned with the
preceding one.
Matthias Bussonnier
Fix Traitlets API 42. Help is in constructor, not tag.
r22337 """).tag(config=True)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # We actually store the expanded templates here:
templates = Dict()
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # The number of characters in the last prompt rendered, not including
# colour characters.
width = Int()
Thomas Kluyver
Make size of rewrite prompt flexible.
r5552 txtwidth = Int() # Not including right-justification
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # The number of characters in each prompt which don't contribute to width
invisible_chars = Dict()
Min RK
adopt traitlets 4.2 API...
r22340 @default('invisible_chars')
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 def _invisible_chars_default(self):
Thomas Kluyver
Make rewrite prompt non-configurable.
r5555 return {'in': 0, 'in2': 0, 'out': 0, 'rewrite':0}
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
MinRK
use `parent=self` throughout IPython...
r11064 def __init__(self, shell, **kwargs):
super(PromptManager, self).__init__(shell=shell, **kwargs)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # Prepare colour scheme table
Matthias Bussonnier
Consolidate color scheme localisations.
r21778 self.color_scheme_table = coloransi.ColorSchemeTable([NoColor,
LinuxColors, LightBGColors], self.color_scheme)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
MinRK
allow access to user_ns in prompt_manager...
r5724 self._formatter = UserNSFormatter(shell)
Thomas Kluyver
Make rewrite prompt non-configurable.
r5555 # Prepare templates & numbers of invisible characters
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 self.update_prompt('in', self.in_template)
self.update_prompt('in2', self.in2_template)
self.update_prompt('out', self.out_template)
Thomas Kluyver
Make rewrite prompt non-configurable.
r5555 self.update_prompt('rewrite')
Min RK
adopt traitlets 4.2 API...
r22340 self.observe(self._update_prompt_trait,
names=['in_template', 'in2_template', 'out_template'])
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 def update_prompt(self, name, new_template=None):
Thomas Kluyver
Further cleanup of prompts code - docstrings, etc.
r5496 """This is called when a prompt template is updated. It processes
abbreviations used in the prompt template (like \#) and calculates how
many invisible characters (ANSI colour escapes) the resulting prompt
contains.
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Further cleanup of prompts code - docstrings, etc.
r5496 It is also called for each prompt on changing the colour scheme. In both
cases, traitlets should take care of calling this automatically.
"""
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 if new_template is not None:
self.templates[name] = multiple_replace(prompt_abbreviations, new_template)
Thomas Kluyver
Fix for prompts containing newlines....
r5657 # We count invisible characters (colour escapes) on the last line of the
# prompt, to calculate the width for lining up subsequent prompts.
Aaron Meurer
Be a little smarter about invisible characters in terminal prompts...
r21605 invis_chars = _invisible_characters(self._render(name, color=True))
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 self.invisible_chars[name] = invis_chars
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 def _update_prompt_trait(self, traitname, new_template):
name = traitname[:-9] # Cut off '_template'
self.update_prompt(name, new_template)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Make size of rewrite prompt flexible.
r5552 def _render(self, name, color=True, **kwargs):
"""Render but don't justify, or update the width or txtwidth attributes.
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 """
Thomas Kluyver
Make rewrite prompt non-configurable.
r5555 if name == 'rewrite':
return self._render_rewrite(color=color)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 if color:
scheme = self.color_scheme_table.active_colors
if name=='out':
colors = color_lists['normal']
colors.number, colors.prompt, colors.normal = \
scheme.out_number, scheme.out_prompt, scheme.normal
else:
colors = color_lists['inp']
colors.number, colors.prompt, colors.normal = \
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829 scheme.in_number, scheme.in_prompt, scheme.in_normal
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 if name=='in2':
colors.prompt = scheme.in_prompt2
fperez
Reorganized the directory for ipython/ to have its own dir, which is a bit...
r0 else:
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # No color
colors = color_lists['nocolor']
colors.number, colors.prompt, colors.normal = '', '', ''
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 count = self.shell.execution_count # Shorthand
# Build the dictionary to be passed to string formatting
fmtargs = dict(color=colors, count=count,
Wouter Bolsterlee
Add \S prompt char for whitespace as wide as the count (similar to \D)
r21752 dots="."*len(str(count)), spaces=" "*len(str(count)),
width=self.width, txtwidth=self.txtwidth)
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 fmtargs.update(self.lazy_evaluate_fields)
fmtargs.update(kwargs)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # Prepare the prompt
prompt = colors.prompt + self.templates[name] + colors.normal
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # Fill in required fields
MinRK
allow access to user_ns in prompt_manager...
r5724 return self._formatter.format(prompt, **fmtargs)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Make rewrite prompt non-configurable.
r5555 def _render_rewrite(self, color=True):
"""Render the ---> rewrite prompt."""
if color:
scheme = self.color_scheme_table.active_colors
# We need a non-input version of these escapes
color_prompt = scheme.in_prompt.replace("\001","").replace("\002","")
color_normal = scheme.normal
else:
color_prompt, color_normal = '', ''
return color_prompt + "-> ".rjust(self.txtwidth, "-") + color_normal
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Make size of rewrite prompt flexible.
r5552 def render(self, name, color=True, just=None, **kwargs):
"""
Render the selected prompt.
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Make size of rewrite prompt flexible.
r5552 Parameters
----------
name : str
Which prompt to render. One of 'in', 'in2', 'out', 'rewrite'
color : bool
If True (default), include ANSI escape sequences for a coloured prompt.
just : bool
If True, justify the prompt to the width of the last prompt. The
default is stored in self.justify.
**kwargs :
Additional arguments will be passed to the string formatting operation,
so they can override the values that would otherwise fill in the
template.
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Make size of rewrite prompt flexible.
r5552 Returns
-------
A string containing the rendered prompt.
"""
res = self._render(name, color=color, **kwargs)
Tayfun Sen
Fixes tests by using in_normal colors for input.
r21829
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 # Handle justification of prompt
invis_chars = self.invisible_chars[name] if color else 0
Thomas Kluyver
Fix for prompts containing newlines....
r5657 self.txtwidth = _lenlastline(res) - invis_chars
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 just = self.justify if (just is None) else just
Thomas Kluyver
Fix for prompts containing newlines....
r5657 # If the prompt spans more than one line, don't try to justify it:
André Matos
Avoid justifying of 'in' prompt...
r5804 if just and name != 'in' and ('\n' not in res) and ('\r' not in res):
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 res = res.rjust(self.width + invis_chars)
Thomas Kluyver
Fix for prompts containing newlines....
r5657 self.width = _lenlastline(res) - invis_chars
Thomas Kluyver
Refactor prompt handling into new prompt manager.
r5495 return res