如何确定 ANSI 终端的大小? [英] How do I determine size of ANSI terminal?
问题描述
标准输入和输出连接到一个终端,该终端实现了 ANSI 转义序列,但维度未知.
Standard input and output are connected to a terminal that implements ANSI escape sequences, but is of unknown dimensions.
我需要知道终端有多大,以便于在其上绘制全屏文本 UI.我如何获得尺寸?
I need to know how big the terminal so to facilitate drawing a full-screen text UI on it. How can I get the size?
正确的大小没有加载到环境变量中.我不能使用 TIOCGETS;调用将返回成功但值不正确——内核也不知道大小.
The correct size is not loaded into environment variables. I cannot use TIOCGETS; the the call would return success but the values are not correct -- the kernel doesn't know the size either.
搜索 stackoverflow 有很多答案,但它们都取决于操作系统以一种或另一种方式提供答案;但这一次不是真的.
There are lots and lots of answers searching stackoverflow, but they all depend on the OS providing the answer one way or anther; but this time that is not true.
我能找到的最好的线索是返回当前光标位置的 DSR 命令;但没有移动到底部/右侧命令.
The best clue I can find is the DSR command which returns the current cursor position; but there's no move to bottom/right command.
推荐答案
resize
程序通过将光标移动到一个非常大的列和行来做到这一点;终端尽可能地移动,例如,
The resize
program does this by moving the cursor to a very large column and row; the terminal moves as far as it can, e.g.,
CUP 999 999
然后resize
询问光标在哪里:
DSR 6
终端回复实际光标位置(即光标位置报告CPR
),从中resize
知道终端的大小:光标在下方-右上角.
The terminal replies with the actual cursor position (i.e., the cursor position report CPR
), from which resize
knows the terminal's size: the cursor is on the lower-right corner.
这一切都使用标准 (ECMA-48/VT100) 转义序列.在 XTerm Control Sequences(应该适用于您的ANSI"终端)>
That's all done using standard (ECMA-48 / VT100) escape sequences. In XTerm Control Sequences (which should apply to your "ANSI" terminal)
CSI Ps n Device Status Report (DSR).
Ps = 6 -> Report Cursor Position (CPR) [row;column].
Result is CSI r ; c R
这篇关于如何确定 ANSI 终端的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!