summaryrefslogtreecommitdiff
path: root/Lib/cmd.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2002-12-02 13:08:53 +0000
committerGuido van Rossum <guido@python.org>2002-12-02 13:08:53 +0000
commitf4dc3877cc58bcae3935d43f8968845860d7b4b5 (patch)
tree1b343b195cdaf4e62b6ce40de0bbbd3baa2c0b01 /Lib/cmd.py
parentf9de32d368f978aa2c7246d66c6211830273ca5c (diff)
downloadcpython-f4dc3877cc58bcae3935d43f8968845860d7b4b5.tar.gz
Add a better columnizer to print_topics().
Diffstat (limited to 'Lib/cmd.py')
-rw-r--r--Lib/cmd.py65
1 files changed, 58 insertions, 7 deletions
diff --git a/Lib/cmd.py b/Lib/cmd.py
index 6d9efff080..bd4e3cb06e 100644
--- a/Lib/cmd.py
+++ b/Lib/cmd.py
@@ -319,10 +319,61 @@ class Cmd:
print header
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"
+ 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)