Permalink
Browse files

allow utils.text.indent to [optionally] flatten existing indentation.

  • Loading branch information...
1 parent 49730bf commit 6d2acd8b339de23b8904321c89761fd243597a32 @minrk committed May 18, 2011
Showing with 30 additions and 7 deletions.
  1. +2 −2 IPython/config/application.py
  2. +1 −2 IPython/config/configurable.py
  3. +27 −3 IPython/utils/text.py
View
4 IPython/config/application.py
@@ -161,7 +161,7 @@ def print_alias_help(self):
help = trait.get_metadata('help')
print alias, "(%s)"%longname, ':', trait.__class__.__name__
if help:
- print indent(help)
+ print indent(help, flatten=True)
print
def print_flag_help(self):
@@ -176,7 +176,7 @@ def print_flag_help(self):
for m, (cfg,help) in self.flags.iteritems():
print '--'+m
- print indent(help)
+ print indent(help, flatten=True)
print
def print_help(self):
View
3 IPython/config/configurable.py
@@ -44,7 +44,6 @@ class MultipleInstanceError(ConfigurableError):
# Configurable implementation
#-----------------------------------------------------------------------------
-
class Configurable(HasTraits):
config = Instance(Config,(),{})
@@ -151,7 +150,7 @@ def class_get_help(cls):
header = "%s.%s : %s" % (cls.__name__, k, v.__class__.__name__)
final_help.append(header)
if help is not None:
- final_help.append(indent(help))
+ final_help.append(indent(help, flatten=True))
return '\n'.join(final_help)
@classmethod
View
30 IPython/utils/text.py
@@ -391,15 +391,39 @@ def igrep(pat,list):
return grep(pat,list,case=0)
-def indent(str,nspaces=4,ntabs=0):
+def indent(instr,nspaces=4, ntabs=0, flatten=False):
"""Indent a string a given number of spaces or tabstops.
indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
+
+ Parameters
+ ----------
+
+ instr : basestring
+ The string to be indented.
+ nspaces : int (default: 4)
+ The number of spaces to be indented.
+ ntabs : int (default: 0)
+ The number of tabs to be indented.
+ flatten : bool (default: False)
+ Whether to scrub existing indentation. If True, all lines will be
+ aligned to the same indentation. If False, existing indentation will
+ be strictly increased.
+
+ Returns
+ -------
+
+ str|unicode : string indented by ntabs and nspaces.
+
"""
- if str is None:
+ if instr is None:
return
ind = '\t'*ntabs+' '*nspaces
- outstr = '%s%s' % (ind,str.replace(os.linesep,os.linesep+ind))
+ if flatten:
+ pat = re.compile(r'^\s*', re.MULTILINE)
+ else:
+ pat = re.compile(r'^', re.MULTILINE)
+ outstr = re.sub(pat, ind, instr)
if outstr.endswith(os.linesep+ind):
return outstr[:-len(ind)]
else:

0 comments on commit 6d2acd8

Please sign in to comment.