Package wsatools :: Module Utilities :: Class WordWrapper
[hide private]

Class WordWrapper

source code


Formats long strings so that they neatly fit within a certain width without words being split across lines. This is a singleton class, invoke as:

   from wsatools.Utilities import WordWrapper
   for line in lines:
       print(WordWrapper.wrap(line))

To Do: Instead of not wrapping pre-formatted lines, handle the case of a new-line character when calculating the overlap.

Instance Methods [hide private]

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods [hide private]
 
resetWrapWidth()
Resets the wrap width to the user default value
source code
str
wrap(line)
Returns supplied line with neatly placed linebreaks to wrap the line within a certain width.
source code
str
_wrapLine(line)
Recursive calls to this function with the next line of text to be wrapped appends lines to WordWrapper._wrappedLines truncated to the width specified by WordWrapper.wrapWidth.
source code
Class Variables [hide private]
  indent = 0
Assume lines are indented by this many character columns.
  wrapWidth = 79
Required width of wrapped lines in number of character columns.
  _wrappedLines = []
List where fully wrapped lines are collated.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

wrap(line)
Static Method

source code 

Returns supplied line with neatly placed linebreaks to wrap the line within a certain width. If the line already contains line breaks or is within the width specified by WordWrapper.wrapWidth then it remains unaffected. If a WordWrapper.indent value is supplied then it is assumed that the first character in the line is located at the column number equal to the WordWrapper.indent value. Wrapped lines will then have a gap after each line-break to maintain the indentation.

Parameters:
  • line (str) - The line to be wrapped.
Returns: str
Wrapped line.

_wrapLine(line)
Static Method

source code 

Recursive calls to this function with the next line of text to be wrapped appends lines to WordWrapper._wrappedLines truncated to the width specified by WordWrapper.wrapWidth. The remaining text is used as the input to the function until there is no more overrun, and the fully wrapped text is returned.

Parameters:
  • line (str) - Full line of text that has not yet been wrapped.
Returns: str
Fully wrapped text.