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

标签 python terminal

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

最好以跨平台的方式,但欢迎提出任何建议(甚至是针对特定操作系统的)。

可以使用 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/

相关文章:

macos - 无需密码即可通过网络设置连接到首选(已保存)网络

eclipse - 如何使用终端在 Ubuntu 12.04 上安装最新版本的 Eclipse Classic?

java - 从终端发送命令到 Java swing 应用程序

Google Calendar API v3 的 Python 客户端库插入事件抛出 "Missing end time"错误

python - 了解 PyTorch 预测

python:使变量等于运算符(+,/,*,-)

java - C printf 未按预期从终端工作

python - Pandas 对列中大于 _each_ 值的所有行进行分组

Python 如何从Basic Table获取Scrapy Xpath数据?

javascript - 如何将 .json 文件名作为参数传递给 node.js 中的命令行