我尝试用另一行替换给定行上打印的文本。为此,我一直在使用ANSI转义码。 我的问题是何时被替换的行在屏幕上不再可见(但通过向上滚动窗口仍然可见),我似乎无法修改它了。 这里是我的问题的一个简单的独立例如:Python打印行上不再可见的终端上使用ANSI转义码
import os
nb_lines_term = int(os.popen('stty size', 'r').read().split()[0])
tot_lines = nb_lines_term + 5
for i in range(tot_lines):
print 'line', tot_lines - i
line_to_replace = nb_lines_term + 2
new_str = "\033[F" * line_to_replace # go u
new_str += 'replacing line ' + str(line_to_replace)
new_str += "\033[E" * (line_to_replace - 1) # go back down
print new_str
是否有仍然可以访问行的方法吗?通过ANSI转义代码或任何其他方法?
请注意,您应该使用['curses'](https://docs.python.org/2/library/curses.html)('tigetstr()'和朋友)。控制序列是特定于终端的(例如'CSI F'是经典xterm中的“结束”密钥)。在Linux上,'CSI F'将光标移动到某一级别,并且您正在覆盖终端缓冲区中的字符......没有线路可以恢复 – dhke