Resolved Issue 196

This commit is contained in:
BJ Dierkes 2013-06-07 16:53:48 -05:00
parent c57c13946b
commit 520d3c5981
3 changed files with 30 additions and 1 deletions

View File

@ -25,7 +25,7 @@ Bugs:
Features:
* None
* :issue:`196` - Added utils.misc.wrap
2.1.2 - Thu Nov 1st, 2012 (DEVELOPMENT)

View File

@ -2,6 +2,7 @@
import sys
import logging
from textwrap import TextWrapper
def init_defaults(*sections):
@ -86,3 +87,20 @@ def is_true(item):
return True
else:
return False
def wrap(text, width=77, indent=''):
"""
Wrap text for cleaner output (this is a simple wrapper around
`textwrap.TextWrapper` in the standard library).
:param text: The text to wrap
:param width: The max width of a line before breaking
:param indent: String to prefix subsequent lines after breaking
:returns: str(text)
"""
wrapper = TextWrapper(subsequent_indent=indent, width=width,
break_long_words=False,
break_on_hyphens=False)
return wrapper.fill(text)

View File

@ -19,3 +19,14 @@ class BackendTestCase(test.CementTestCase):
# set logging back to non-debug
misc.minimal_logger(__name__, debug=False)
pass
def test_wrap(self):
text = "aaaaa bbbbb ccccc"
new_text = misc.wrap(text, width=5)
parts = new_text.split('\n')
self.eq(len(parts), 3)
self.eq(parts[1], 'bbbbb')
new_text = misc.wrap(text, width=5, indent='***')
parts = new_text.split('\n')
self.eq(parts[2], '***ccccc')