summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-11-14 13:33:42 -0700
committerJason Gerard DeRose <jderose@redhat.com>2008-11-14 13:33:42 -0700
commitc974451edf7895bcac9531a62c5d49c7c4141978 (patch)
tree769f17c03883878c5fd24cb69b4d2ba8c1c2b09c /ipalib
parent6d1ec6360cd5a7c2b5ad4a6089a1fe98c585036d (diff)
downloadfreeipa-c974451edf7895bcac9531a62c5d49c7c4141978.tar.gz
freeipa-c974451edf7895bcac9531a62c5d49c7c4141978.tar.xz
freeipa-c974451edf7895bcac9531a62c5d49c7c4141978.zip
Added print_plain() and print_paragraph() methods to textui plugin and cleaned up the order of its methods
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/cli.py119
1 files changed, 77 insertions, 42 deletions
diff --git a/ipalib/cli.py b/ipalib/cli.py
index 3b365cdba..69a3c28fe 100644
--- a/ipalib/cli.py
+++ b/ipalib/cli.py
@@ -97,30 +97,16 @@ class textui(backend.Backend):
return max(len(row) for row in rows)
return max(len(row[col]) for row in rows)
- def print_dashed(self, string, above=True, below=True):
- """
- Print a string with with a dashed line above and/or below.
-
- For example:
+ def choose_number(self, n, singular, plural=None):
+ if n == 1 or plural is None:
+ return singular % n
+ return plural % n
- >>> ui = textui()
- >>> ui.print_dashed('Dashed above and below.')
- -----------------------
- Dashed above and below.
- -----------------------
- >>> ui.print_dashed('Only dashed below.', above=False)
- Only dashed below.
- ------------------
- >>> ui.print_dashed('Only dashed above.', below=False)
- ------------------
- Only dashed above.
+ def print_plain(self, string):
+ """
+ Print exactly like ``print`` statement would.
"""
- dashes = '-' * len(string)
- if above:
- print dashes
print string
- if below:
- print dashes
def print_line(self, text, width=None):
"""
@@ -144,6 +130,35 @@ class textui(backend.Backend):
text = text[:width - 3] + '...'
print text
+ def print_paragraph(self, text, width=None):
+ """
+ Print a paragraph, automatically word-wrapping to tty width.
+
+ For example:
+
+ >>> text = '''
+ ... Python is a dynamic object-oriented programming language that can
+ ... be used for many kinds of software development.
+ ... '''
+ >>> ui = textui()
+ >>> ui.print_paragraph(text, width=45)
+ Python is a dynamic object-oriented
+ programming language that can be used for
+ many kinds of software development.
+
+ The above example aside, you normally should not specify the
+ ``width``. When you don't, it is automatically determined by calling
+ `textui.get_tty_width()`.
+
+ The word-wrapping is done using the Python ``textwrap`` module. See:
+
+ http://docs.python.org/library/textwrap.html
+ """
+ if width is None:
+ width = self.get_tty_width()
+ for line in textwrap.wrap(text.strip(), width):
+ print line
+
def print_indented(self, text, indent=1):
"""
Print at specified indentation level.
@@ -160,22 +175,6 @@ class textui(backend.Backend):
"""
print (CLI_TAB * indent + text)
- def print_name(self, name):
- """
- Print a command name.
-
- The typical use for this is to mark the start of output from a
- command. For example, a hypothetical ``show_status`` command would
- output something like this:
-
- >>> ui = textui()
- >>> ui.print_name('show_status')
- ------------
- show-status:
- ------------
- """
- self.print_dashed('%s:' % to_cli(name))
-
def print_keyval(self, rows, indent=1):
"""
Print (key = value) pairs, one pair per line.
@@ -199,6 +198,47 @@ class textui(backend.Backend):
for row in rows:
self.print_indented('%s = %r' % row, indent)
+ def print_dashed(self, string, above=True, below=True):
+ """
+ Print a string with a dashed line above and/or below.
+
+ For example:
+
+ >>> ui = textui()
+ >>> ui.print_dashed('Dashed above and below.')
+ -----------------------
+ Dashed above and below.
+ -----------------------
+ >>> ui.print_dashed('Only dashed below.', above=False)
+ Only dashed below.
+ ------------------
+ >>> ui.print_dashed('Only dashed above.', below=False)
+ ------------------
+ Only dashed above.
+ """
+ dashes = '-' * len(string)
+ if above:
+ print dashes
+ print string
+ if below:
+ print dashes
+
+ def print_name(self, name):
+ """
+ Print a command name.
+
+ The typical use for this is to mark the start of output from a
+ command. For example, a hypothetical ``show_status`` command would
+ output something like this:
+
+ >>> ui = textui()
+ >>> ui.print_name('show_status')
+ ------------
+ show-status:
+ ------------
+ """
+ self.print_dashed('%s:' % to_cli(name))
+
def print_count(self, count, singular, plural=None):
"""
Print a summary count.
@@ -230,11 +270,6 @@ class textui(backend.Backend):
self.choose_number(count, singular, plural)
)
- def choose_number(self, n, singular, plural=None):
- if n == 1 or plural is None:
- return singular % n
- return plural % n
-
def prompt(self, label, default=None, get_values=None):
"""
Prompt user for input.