在 Python 3 中使用 ANSI 序列确定终端光标位置

net*_*ego 4 python terminal ansi-escape python-3.x

我想编写一个小脚本,将图像打印到终端/usr/lib/w3mimgdisplay(就像在 mac osx 中一样lsi)。因此,当脚本启动时,我需要实际的光标位置(或插入符号位置)。到目前为止,我想出了用 ANSI 序列获取 shell 中光标位置的方法:

$ echo -en "\e[6n"
^[[2;1R$ 1R
Run Code Online (Sandbox Code Playgroud)

这就是这个 ANSI 序列的响应的样子(urvxt 和 bash - 不知道这是否重要)。所以这个序列立即打印出结果(^[[2;1R)。这就是我不明白的地方。这是怎么做到的?如果我编写一个非常简单的 shell 脚本,仅使用该指令并跟踪脚本,这并不能解决问题。什么?然后我尝试通过查看 terminfo 联机帮助页来弄清楚这是如何发生的。在这里找不到它(也许我没有努力)。此时我发现自己对这个概念非常困惑。终端是否将位置写入标准输出?

终端

#!/bin/bash
echo -en "\e[6n"

$ strace sh curpos.sh
[...]
read(255, "#!/bin/bash\necho -en \"\\e[6n\"\n", 29) = 29
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 8), ...}) = 0
write(1, "\33[6n", 4)                   = 4
^[[54;21Rread(255, "", 29)                       = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
exit_group(0)                           = ?
+++ exited with 0 +++
Run Code Online (Sandbox Code Playgroud)

Python

首先,我尝试使用subprocess.check_output它,当然只是返回我回显的字符串。我如何捕获对此 ANSI 序列的响应?

>>> subprocess.check_output(["echo", "-en", "\x1b[6n"])
b"\x1b[6n"
Run Code Online (Sandbox Code Playgroud)

我还尝试了很多其他事情,比如读取标准输入和标准输出!?有或没有线程,但所有这一切更多的是猜测和嘲笑,而不是知道该怎么做。我也在互联网上搜索了很长一段时间,希望找到一个如何做到这一点的例子,但没有运气。我找到了同一问题的答案: https: //stackoverflow.com/a/35526389/2787738但这不起作用。实际上我不知道这是否有效,因为在这个答案中,ANSI 序列在开始从标准输入读取之前被写入标准输出?在这里我再次意识到我不理解这些 ANSI 序列如何真正工作的概念/机制。因此,在这一点上,我们非常感谢每一个澄清事情的解释。我发现的最有帮助的帖子是这个: https: //www.linuxquestions.org/questions/programming-9/get-cursor-position-in-c-947833/。在这个线程中,有人发布了这个 bash 脚本:

#!/bin/bash
# Restore terminal settings when the script exits.
termios="$(stty -g)"
trap "stty '$termios'" EXIT
# Disable ICANON ECHO. Should probably also disable CREAD.
stty -icanon -echo
# Request cursor coordinates
printf '\033[6n'
# Read response from standard input; note, it ends at R, not at newline
read -d "R" rowscols
# Clean up the rowscols (from \033[rows;cols -- the R at end was eaten)
rowscols="${rowscols//[^0-9;]/}"
rowscols=("${rowscols//;/ }")
printf '(row %d, column %d) ' ${rowscols[0]} ${rowscols[1]}
# Reset original terminal settings.
stty "$termios"
Run Code Online (Sandbox Code Playgroud)

在这里我们可以看到,响应确实以某种方式神奇地出现在屏幕上:)。这就是为什么此脚本禁用终端上的回显,并在读取响应后通过 重置原始终端设置stty。

net*_*ego 6

这是一个 POC 片段,说明如何通过 ansi/vt100 控制序列读取当前光标位置。

Curpos.py

import os, re, sys, termios, tty

def getpos():

    buf = ""
    stdin = sys.stdin.fileno()
    tattr = termios.tcgetattr(stdin)

    try:
        tty.setcbreak(stdin, termios.TCSANOW)
        sys.stdout.write("\x1b[6n")
        sys.stdout.flush()

        while True:
            buf += sys.stdin.read(1)
            if buf[-1] == "R":
                break

    finally:
        termios.tcsetattr(stdin, termios.TCSANOW, tattr)

    # reading the actual values, but what if a keystroke appears while reading
    # from stdin? As dirty work around, getpos() returns if this fails: None
    try:
        matches = re.match(r"^\x1b\[(\d*);(\d*)R", buf)
        groups = matches.groups()
    except AttributeError:
        return None

    return (int(groups[0]), int(groups[1]))

if __name__ == "__main__":
    print(getpos())
Run Code Online (Sandbox Code Playgroud)

示例输出

$ python ./curpos.py
(2, 1)
Run Code Online (Sandbox Code Playgroud)

警告

这并不完美。为了使其更加健壮,在从标准输入读取数据的同时对用户的击键进行排序的例程会很好。