如何找到终端中可用线路的数量?
最好以跨平台的方式,但欢迎提出任何建议(甚至是针对特定操作系统的)。
可以使用 os
找到终端的高度和长度模块,但是这没有考虑可能已经使用的行数。
为了澄清这里的事情是一个例子:
在这个例子中,终端的高度是 33,但是因为已经使用了 3 行,所以只有 30 行可用。
最佳答案
通过该屏幕截图确定您使用的是 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
这将为您提供屏幕尺寸和光标位置。
cury
是行,所以你可以计算剩下的行数。
但是,您可能希望在进行过程中重新检查控制台窗口大小,因为用户可以随时调整窗口大小。
关于python - 获取终端中的可用线路数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48549535/