获得命令行宽/高

问题

Zengming Zhang <[email protected]>
sender-time     Sent at 01:30 (GMT+08:00). Current time there: 10:58 AM. ✆
reply-to        [email protected]
to      [email protected]
date    Sun, Apr 18, 2010 at 01:30

subject [CPyUG] 请问下如何在python中获得命令行终端的宽度?

宽度

我找到解决方法了,请参见下面的程序: get_terminal_width.py`

   1 import commands
   2 def get_terminal_width_resize():
   3     c = commands.getoutput('resize').split('\n')
   4     c = [x for x in c if x.startswith('COLUMNS=')]
   5     if c:
   6         c = c[0]
   7         dummy, c = c.split('=', 1)
   8         if c[-1] == ';':
   9             c = c[:-1]
  10     if c:
  11         return int(c)
  12     else:
  13         return None
  14         
  15 print get_terminal_width_resize()

在此附上Radovan Garabík的版权说明:

zzm@zzm-desktop:~/Downloads/cli-tools/pydf/pydf-8$ more COPYING 
This package was written by Radovan Garabík <garabik @ kassiopeia.juls.savba.sk>
Copyright:
Public domain. Do whatever you want to do with this program.

高度

同样可以得到获得终端高度的程序:

   1 import commands
   2 
   3 def get_terminal_height():
   4 
   5     c = commands.getoutput('resize').split('\n')
   6     c = [x for x in c if x.startswith('LINES=')]
   7 
   8     if c:
   9         c = c[0]
  10         dummy, c = c.split('=', 1)
  11         if c[-1] == ';':
  12             c = c[:-1]
  13     if c:
  14         return int(c)
  15     else:
  16         return None
  17         
  18 print get_terminal_height()


反馈

创建 by -- ZoomQuiet [2010-04-19 02:59:53]

MiscItems/2010-04-19 (last edited 2010-04-19 02:59:53 by ZoomQuiet)