获取终端中可用线路的数量 [英] Getting the amount of available lines in a terminal

查看:22
本文介绍了获取终端中可用线路的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

最好采用跨平台的方式,但欢迎提出任何建议(甚至特定于操作系统).

可以使用

from ctypes import windll, create_string_buffer# 标准输入句柄是 -10# 标准输出句柄是 -11# stderr 句柄是 -12h = windll.kernel32.GetStdHandle(-12)csbi = create_string_buffer(22)res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)如果资源:导入结构(bufx, bufy, curx, cury, wattr,left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)sizex = 右 - 左 + 1sizey = 底部 - 顶部 + 1别的:sizex, sizey = 80, 25 # 无法确定实际大小 - 返回默认值打印 sizex、sizey、curx、cury

这将为您提供屏幕大小和光标位置.

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

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

How can the amount of available lines in a terminal be found?

Preferably in a cross-platform manner but any suggestions (even OS-specific) are welcome.

The height and length of a terminal can be found using the os module however this does not take into account the amount of lines that may already have been used.

To clarify things here is an example:

In this example the height of the terminal here is 33 however since 3 lines have been used, only 30 lines are available.

解决方案

Determining by that screen shot, you are on Windows

This is from 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

That will give you screen size, and the cursor position.

cury is the line, so you can calculate the number of lines left.

However, you may want to re-check the console window size as you progress, as the user may resize the window at any time.

这篇关于获取终端中可用线路的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆