获取终端中可用线路的数量

Sim*_*mon 7 python terminal

如何找到终端中可用线路的数量?

优选地,以跨平台的方式,但是欢迎任何建议(甚至是OS特定的).

可以使用os模块找到终端的高度和长度,但是这不考虑可能已经使用的线路的数量.

澄清这里的事情就是一个例子:

在这个例子中,这里终端的高度是33,但是由于使用了3​​条线路,因此只有30条线路可用.

Edw*_*rlo 7

通过该屏幕截图确定您在Windows上

这是来自http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/

from ctypes import windll, create_string_buffer

# stdin handle is -10
# stdout handle is -11
# stderr handle is -12

h = windll.kernel32.GetStdHandle(-12)
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 # can't determine actual size - return default values

print sizex, sizey, curx, cury
Run Code Online (Sandbox Code Playgroud)

这将为您提供屏幕尺寸和光标位置.

cury 是行,所以你可以计算剩余的行数.

但是,您可能希望在进度时重新检查控制台窗口大小,因为用户可以随时调整窗口大小.