[Python-checkins] python/dist/src/Lib textwrap.py,1.10,1.11

gward@users.sourceforge.net gward@users.sourceforge.net
Mon, 10 Jun 2002 14:37:14 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv4540

Modified Files:
	textwrap.py 
Log Message:
Took initial_tab and subsequent_tab away from the fill() method and
transformed them into the initial_indent and subsequent_indent instance
attributes.  Now they actually work as advertised, ie. they are
accounted for in the width of each output line.  Plus you can use them
with wrap() as well as fill(), and fill() went from simple-and-broken to
trivial-and-working.


Index: textwrap.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/textwrap.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** textwrap.py	10 Jun 2002 20:36:07 -0000	1.10
--- textwrap.py	10 Jun 2002 21:37:12 -0000	1.11
***************
*** 23,26 ****
--- 23,32 ----
          the maximum width of wrapped lines (unless break_long_words
          is false)
+       initial_indent (default: "")
+         string that will be prepended to the first line of wrapped
+         output.  Counts towards the line's width.
+       subsequent_indent (default: "")
+         string that will be prepended to all lines save the first
+         of wrapped output; also counts towards each line's width.
        expand_tabs (default: true)
          Expand tabs in input text to spaces before further processing.
***************
*** 64,67 ****
--- 70,75 ----
      def __init__ (self,
                    width=70,
+                   initial_indent="",
+                   subsequent_indent="",
                    expand_tabs=True,
                    replace_whitespace=True,
***************
*** 69,72 ****
--- 77,82 ----
                    break_long_words=True):
          self.width = width
+         self.initial_indent = initial_indent
+         self.subsequent_indent = subsequent_indent
          self.expand_tabs = expand_tabs
          self.replace_whitespace = replace_whitespace
***************
*** 125,137 ****
                  i += 1
  
!     def _handle_long_word(self, chunks, cur_line, cur_len):
          """_handle_long_word(chunks : [string],
                               cur_line : [string],
!                              cur_len : int)
  
          Handle a chunk of text (most likely a word, not whitespace) that
          is too long to fit in any line.
          """
!         space_left = self.width - cur_len
  
          # If we're allowed to break long words, then do so: put as much
--- 135,147 ----
                  i += 1
  
!     def _handle_long_word(self, chunks, cur_line, cur_len, width):
          """_handle_long_word(chunks : [string],
                               cur_line : [string],
!                              cur_len : int, width : int)
  
          Handle a chunk of text (most likely a word, not whitespace) that
          is too long to fit in any line.
          """
!         space_left = width - cur_len
  
          # If we're allowed to break long words, then do so: put as much
***************
*** 167,176 ****
          """
          lines = []
-         width = self.width
  
          while chunks:
  
!             cur_line = []                   # list of chunks (to-be-joined)
!             cur_len = 0                     # length of current line
  
              # First chunk on line is whitespace -- drop it.
--- 177,196 ----
          """
          lines = []
  
          while chunks:
  
!             # Start the list of chunks that will make up the current line.
!             # cur_len is just the length of all the chunks in cur_line.
!             cur_line = []
!             cur_len = 0
! 
!             # Figure out which static string will prefix this line.
!             if lines:
!                 indent = self.subsequent_indent
!             else:
!                 indent = self.initial_indent
! 
!             # Maximum width for this line.
!             width = self.width - len(indent)
  
              # First chunk on line is whitespace -- drop it.
***************
*** 193,197 ****
              # fit on *any* line (not just this one).  
              if chunks and len(chunks[0]) > width:
!                 self._handle_long_word(chunks, cur_line, cur_len)
  
              # If the last chunk on this line is all whitespace, drop it.
--- 213,217 ----
              # fit on *any* line (not just this one).  
              if chunks and len(chunks[0]) > width:
!                 self._handle_long_word(chunks, cur_line, cur_len, width)
  
              # If the last chunk on this line is all whitespace, drop it.
***************
*** 202,206 ****
              # of all lines (return value).
              if cur_line:
!                 lines.append(''.join(cur_line))
  
          return lines
--- 222,226 ----
              # of all lines (return value).
              if cur_line:
!                 lines.append(indent + ''.join(cur_line))
  
          return lines
***************
*** 226,244 ****
          return self._wrap_chunks(chunks)
  
!     def fill(self, text, initial_tab="", subsequent_tab=""):
!         """fill(text : string,
!                 initial_tab : string = "",
!                 subsequent_tab : string = "")
!            -> string
  
          Reformat the paragraph in 'text' to fit in lines of no more than
!         'width' columns.  The first line is prefixed with 'initial_tab',
!         and subsequent lines are prefixed with 'subsequent_tab'; the
!         lengths of the tab strings are accounted for when wrapping lines
!         to fit in 'width' columns.
          """
!         lines = self.wrap(text)
!         sep = "\n" + subsequent_tab
!         return initial_tab + sep.join(lines)
  
  
--- 246,256 ----
          return self._wrap_chunks(chunks)
  
!     def fill(self, text):
!         """fill(text : string) -> string
  
          Reformat the paragraph in 'text' to fit in lines of no more than
!         'width' columns.
          """
!         return "\n".join(self.wrap(text))
  
  
***************
*** 249,253 ****
      return w.wrap(text)
  
! def fill(text, width=70, initial_tab="", subsequent_tab="", **kwargs):
      w = TextWrapper(width=width, **kwargs)
!     return w.fill(text, initial_tab, subsequent_tab)
--- 261,265 ----
      return w.wrap(text)
  
! def fill(text, width=70, **kwargs):
      w = TextWrapper(width=width, **kwargs)
!     return w.fill(text)