Chinaunix首页 | 论坛 | 博客
  • 博客访问: 122357
  • 博文数量: 31
  • 博客积分: 2551
  • 博客等级: 少校
  • 技术积分: 435
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-18 15:52
文章分类

全部博文(31)

文章存档

2011年(5)

2010年(4)

2009年(12)

2008年(10)

我的朋友

分类: Python/Ruby

2009-03-04 18:05:34

这条thread挺有用的:
 

之前只知道用curses模块,windows下面还要安装wcurses,这个1调用就跑出1窗口。

整理下, for linux(windows除了wcurses 不知道):
1. curses
win = curses.initscr()
w = win.getmaxyx()[1]
curses.endwin()

2. 命令 stty -a, 得到的columns
# stty -a 
speed 38400 baud; rows 58; columns 132; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

stty size 得到的1对值(rows, columns)
# stty  size
58 132

3. 命令 tput cols
# tput cols
132

4. 1个可用于多数unix的用法:
def getTerminalSize():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (env['LINES'], env['COLUMNS'])
except:
cr = (25, 80)
return int(cr[1]), int(cr[0])

阅读(590) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~