MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / columnize

Method columnize

tools/python-3.11.9-amd64/Lib/cmd.py:346–401  ·  view source on GitHub ↗

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).

(self, list, displaywidth=80)

Source from the content-addressed store, hash-verified

344 self.stdout.write("\n")
345
346 def columnize(self, list, displaywidth=80):
347 """Display a list of strings as a compact set of columns.
348
349 Each column is only as wide as necessary.
350 Columns are separated by two spaces (one was not legible enough).
351 """
352 if not list:
353 self.stdout.write("<empty>\n")
354 return
355
356 nonstrings = [i for i in range(len(list))
357 if not isinstance(list[i], str)]
358 if nonstrings:
359 raise TypeError("list[i] not a string for i in %s"
360 % ", ".join(map(str, nonstrings)))
361 size = len(list)
362 if size == 1:
363 self.stdout.write('%s\n'%str(list[0]))
364 return
365 # Try every row count from 1 upwards
366 for nrows in range(1, len(list)):
367 ncols = (size+nrows-1) // nrows
368 colwidths = []
369 totwidth = -2
370 for col in range(ncols):
371 colwidth = 0
372 for row in range(nrows):
373 i = row + nrows*col
374 if i >= size:
375 break
376 x = list[i]
377 colwidth = max(colwidth, len(x))
378 colwidths.append(colwidth)
379 totwidth += colwidth + 2
380 if totwidth > displaywidth:
381 break
382 if totwidth <= displaywidth:
383 break
384 else:
385 nrows = len(list)
386 ncols = 1
387 colwidths = [0]
388 for row in range(nrows):
389 texts = []
390 for col in range(ncols):
391 i = row + nrows*col
392 if i >= size:
393 x = ""
394 else:
395 x = list[i]
396 texts.append(x)
397 while texts and not texts[-1]:
398 del texts[-1]
399 for col in range(len(texts)):
400 texts[col] = texts[col].ljust(colwidths[col])
401 self.stdout.write("%s\n"%str(" ".join(texts)))

Callers 1

print_topicsMethod · 0.95

Calls 7

strFunction · 0.85
maxFunction · 0.85
ljustMethod · 0.80
mapFunction · 0.50
writeMethod · 0.45
joinMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected