Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think there's a general, cross-platform way to "get the width of the terminal" -- <strong>most definitely NOT</strong> "look at the COLUMNS environment variable" (see my comment on the question). On Linux and Mac OS X (and I expect all modern Unix versions),</p> <pre><code>curses.wrapper(lambda _: curses.tigetnum('cols')) </code></pre> <p>returns the number of columns; but I don't know if <a href="http://code.google.com/p/wcurses/" rel="nofollow noreferrer">wcurses</a> supports this in Windows.</p> <p>Once you do have (from os.environ['COLUMNS'] if you insist, or via curses, or from an oracle, or defaulted to 80, or any other way you like) the desired output width, the rest is quite feasible. It's finnicky work, with many chances for off-by-one kinds of errors, and very vulnerable to a lot of detailed specs that you don't make entirely clear, such as: which column gets cut to avoid wrapping -- it it always the last one, or...? How come you're showing 3 columns in the sample output when according to your question only two are passed in...? what is supposed to happen if not all rows have the same number of columns? must all entries in table be strings? and many, many other mysteries of this ilk.</p> <p>So, taking somewhat-arbitrary guesses for all the specs that you don't express, one approach might be something like...:</p> <pre><code>import sys def colprint(totwidth, table): numcols = max(len(row) for row in table) # ensure all rows have &gt;= numcols columns, maybe empty padded = [row+numcols*('',) for row in table] # compute col widths, including separating space (except for last one) widths = [ 1 + max(len(x) for x in column) for column in zip(*padded)] widths[-1] -= 1 # drop or truncate columns from the right in order to fit while sum(widths) &gt; totwidth: mustlose = sum(widths) - totwidth if widths[-1] &lt;= mustlose: del widths[-1] else: widths[-1] -= mustlose break # and finally, the output phase! for row in padded: for w, i in zip(widths, row): sys.stdout.write('%*s' % (-w, i[:w])) sys.stdout.write('\n') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload