[Python-checkins] python/dist/src/Lib cmd.py,1.31,1.32

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Mon, 02 Dec 2002 05:08:56 -0800


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

Modified Files:
	cmd.py 
Log Message:
Add a better columnizer to print_topics().


Index: cmd.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/cmd.py,v
retrieving revision 1.31
retrieving revision 1.32
diff -C2 -d -r1.31 -r1.32
*** cmd.py	30 Jun 2002 03:39:14 -0000	1.31
--- cmd.py	2 Dec 2002 13:08:53 -0000	1.32
***************
*** 320,328 ****
              if self.ruler:
                  print self.ruler * len(header)
!             (cmds_per_line,junk)=divmod(maxcol,cmdlen)
!             col=cmds_per_line
!             for cmd in cmds:
!                 if col==0: print
!                 print (("%-"+`cmdlen`+"s") % cmd),
!                 col = (col+1) % cmds_per_line
!             print "\n"
--- 320,379 ----
              if self.ruler:
                  print self.ruler * len(header)
!             self.columnize(cmds, maxcol-1)
!             print
! 
!     def columnize(self, list, displaywidth=80):
!         """Display a list of strings as a compact set of columns.
! 
!         Each column is only as wide as necessary.
!         Columns are separated by two spaces (one was not legible enough).
!         """
!         if not list:
!             print "<empty>"
!             return
!         nonstrings = [i for i in range(len(list))
!                         if not isinstance(list[i], str)]
!         if nonstrings:
!             raise TypeError, ("list[i] not a string for i in %s" %
!                               ", ".join(map(str, nonstrings)))
!         size = len(list)
!         if size == 1:
!             print list[0]
!             return
!         # Try every row count from 1 upwards
!         for nrows in range(1, len(list)):
!             ncols = (size+nrows-1) // nrows
!             colwidths = []
!             totwidth = -2
!             for col in range(ncols):
!                 colwidth = 0
!                 for row in range(nrows):
!                     i = row + nrows*col
!                     if i >= size:
!                         break
!                     x = list[i]
!                     colwidth = max(colwidth, len(x))
!                 colwidths.append(colwidth)
!                 totwidth += colwidth + 2
!                 if totwidth > displaywidth:
!                     break
!             if totwidth <= displaywidth:
!                 break
!         else:
!             nrows = len(list)
!             ncols = 1
!             colwidths = [0]
!         for row in range(nrows):
!             texts = []
!             for col in range(ncols):
!                 i = row + nrows*col
!                 if i >= size:
!                     x = ""
!                 else:
!                     x = list[i]
!                 texts.append(x)
!             while texts and not texts[-1]:
!                 del texts[-1]
!             for col in range(len(texts)):
!                 texts[col] = texts[col].ljust(colwidths[col])
!             print "  ".join(texts)