diff -r 842147eb1b26 Lib/textwrap.py --- a/Lib/textwrap.py Mon Jul 04 19:11:14 2011 -0700 +++ b/Lib/textwrap.py Mon Jul 04 22:59:22 2011 -0400 @@ -33,6 +33,9 @@ width (default: 70) the maximum width of wrapped lines (unless break_long_words is false) + width_func (default: len) + function to use to identify the length of a string of text. + Overridden if the font being used has different character widths. initial_indent (default: "") string that will be prepended to the first line of wrapped output. Counts towards the line's width. @@ -95,6 +98,7 @@ def __init__(self, width=70, + width_func=len, initial_indent="", subsequent_indent="", expand_tabs=True, @@ -104,6 +108,7 @@ drop_whitespace=True, break_on_hyphens=True): self.width = width + self.width_func = width_func self.initial_indent = initial_indent self.subsequent_indent = subsequent_indent self.expand_tabs = expand_tabs @@ -239,7 +244,7 @@ indent = self.initial_indent # Maximum width for this line. - width = self.width - len(indent) + width = self.width - self.width_func(indent) # First chunk on line is whitespace -- drop it, unless this # is the very beginning of the text (ie. no lines started yet). @@ -247,7 +252,7 @@ del chunks[-1] while chunks: - l = len(chunks[-1]) + l = self.width_func(chunks[-1]) # Can at least squeeze this chunk onto the current line. if cur_len + l <= width: @@ -260,7 +265,7 @@ # The current line is full, and the next chunk is too big to # fit on *any* line (not just this one). - if chunks and len(chunks[-1]) > width: + if chunks and self.width_func(chunks[-1]) > width: self._handle_long_word(chunks, cur_line, cur_len, width) # If the last chunk on this line is all whitespace, drop it.