English 中文(简体)
a 类似于一栏产出——原始图书馆
原标题:apt like column output - python library
  • 时间:2009-09-08 23:12:30
  •  标签:

浸泡器的精度工具产出是穿制服的 column子。 例如,试图操作“才能搜索引擎”,所有名字都出现在同一宽度的第一栏。

现在,如果你重新定位终端,则对该栏的宽度进行相应的调整。

是否有一座能够让人们这样做的山洞? 请注意,图书馆必须了解终端宽度,并作为投入表――例如,可在<代码>上(快速、通用公平市价客户转换)......。 还注意到以下第二栏中的插座如果超过终端宽度,那么会如何消减,从而不会引入未预定的第二行。

$ aptitude search svn
[...]
p   python-svn-dbg                    - A(nother) Python interface to Subversion (d
v   python2.5-svn                     -                                            
v   python2.6-svn                     -                                            
p   rapidsvn                          - A GUI client for subversion                
p   statsvn                           - SVN repository statistics                  
p   svn-arch-mirror                   - one-way mirroring from Subversion to Arch r
p   svn-autoreleasedeb                - Automatically release/upload debian package
p   svn-buildpackage                  - helper programs to maintain Debian packages
p   svn-load                          - An enhanced import facility for Subversion 
p   svn-workbench                     - A Workbench for Subversion                 
p   svnmailer                         - extensible Subversion commit notification t
p   websvn                            - interface for subversion repositories writt
$

www.un.org/Depts/DGACM/index_spanish.htm (根据以下各表的答复)......产出将类似于对1的纬度探测,但最后一栏(这是在一段行长处的唯一一栏),2)通常只有2-4栏,但最后一栏(“描述”)至少需要一半的终端宽度。 3) 所有条目都包含相同的栏目,4)

最佳回答

我不认为存在一种通向“瞄准终点站宽度”的通用、跨层方式——,最无限期的NOT。 “了解格乌玛环境变量”(见我对这个问题的评论)。 关于LC和MacOS X(我期待着所有现代的“Unix”版本)。

curses.wrapper(lambda _: curses.tigetnum( cols ))

我不知道

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.

因此,对你不表达的所有幽灵,采用某种武断的格言,一种做法可能像......:

import sys

def colprint(totwidth, table):
  numcols = max(len(row) for row in table)
  # ensure all rows have >= 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) > totwidth:
    mustlose = sum(widths) - totwidth
    if widths[-1] <= 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( 
 )
问题回答

<>Update: www.un.org/Depts/DGACM/index_french.htm http://code.activestate.com/pypm/applib/“rel=“nofollow noreferer”>applib 。 图书馆主办。

这里是你们感兴趣的人的完整的方案:

# This function was written by Alex Martelli
# http://stackoverflow.com/questions/1396820/
def colprint(table, totwidth=None):
    """Print the table in terminal taking care of wrapping/alignment

    - `table`:    A table of strings. Elements must not be `None`
    - `totwidth`: If None, console width is used
    """
    if not table: return
    if totwidth is None:
        totwidth = find_console_width()
        totwidth -= 1 # for not printing an extra empty line on windows
    numcols = max(len(row) for row in table)
    # ensure all rows have >= 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) > totwidth:
        mustlose = sum(widths) - totwidth
        if widths[-1] <= mustlose:
            del widths[-1]
        else:
            widths[-1] -= mustlose
            break
    # and finally, the output phase!
    for row in padded:
        print(  .join([u %*s  % (-w, i[:w])
                       for w, i in zip(widths, row)]))

def find_console_width():
    if sys.platform.startswith( win ):
        return _find_windows_console_width()
    else:
        return _find_unix_console_width()
def _find_unix_console_width():
    """Return the width of the Unix terminal

    If `stdout` is not a real terminal, return the default value (80)
    """
    import termios, fcntl, struct, sys

    # fcntl.ioctl will fail if stdout is not a tty
    if not sys.stdout.isatty():
        return 80

    s = struct.pack("HHHH", 0, 0, 0, 0)
    fd_stdout = sys.stdout.fileno()
    size = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
    height, width = struct.unpack("HHHH", size)[:2]
    return width
def _find_windows_console_width():
    """Return the width of the Windows console

    If the width cannot be determined, return the default value (80)
    """
    # http://code.activestate.com/recipes/440694/
    from ctypes import windll, create_string_buffer
    STDIN, STDOUT, STDERR = -10, -11, -12

    h = windll.kernel32.GetStdHandle(STDERR)
    csbi = create_string_buffer(22)
    res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)

    if res:
        import struct
        (bufx, bufy, curx, cury, wattr,
         left, top, right, bottom,
         maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
        sizex = right - left + 1
        sizey = bottom - top + 1
    else:
        sizex, sizey = 80, 25

    return sizex

纬度使用cwidget ,以在只读写的栏目中打上格式。 页: 1 撰写长篇大事,但我认为这不值得麻烦。 你们可以采用你首选的方法,在果园中实现实际的横向规模,并计算出雨水。

首先,使用<条码>查询/编码>,以达到TTY的大小:

import termios, fcntl, struct, sys

def get_tty_size():
    s = struct.pack("HHHH", 0, 0, 0, 0)
    fd_stdout = sys.stdout.fileno()
    size = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
    return struct.unpack("HHHH", size)[:2]

print get_tty_size()

然后利用类似职能来编组:

pad = lambda s, n=20: "%s%s" % (s,   *(n-len(s)))

把这些人聚集在一起,你把 column子转成ole子!





相关问题
热门标签