简体   繁体   English

Python使用ANSI转义码在终端上不再可见的行上打印

[英]Python print on lines that are not visible anymore on the terminal with ANSI escape codes

I try to replace the text printed on a given line by another. 我尝试用另一行替换打印在给定行上的文本。 For that, I have been using ANSI escape codes. 为此,我一直在使用ANSI转义码。 My problem is when the line to be replaced is not visible on the screen anymore (but still visible by scrolling up the window) I don't seem to be able to modify it anymore. 我的问题是当要替换的行在屏幕上不再可见(但通过向上滚动窗口仍然可见)时,我似乎无法再对其进行修改。 Here is a simple standalone example of my problem : 这是我的问题的一个简单的独立示例:

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

Is there a way to access the line still? 有没有办法访问线路? by ANSI escape codes or any other method? 通过ANSI转义码或其他任何方法?

short: no 简短:没有

longer: by convention, once data has left the visible screen, terminal emulators may leave the data which has scrolled out of sight in read-only scrollback area. 更长:按照惯例,一旦数据离开可见的屏幕, 终端仿真器可能会将滚动到视线之外的数据保留在只读回滚区域中。

That reflects the adaptation of hardware terminals to emulators. 这反映了硬件终端对仿真器的适应性。 Very few hardware terminals provided for panning around in a wider region (and few provided any way to see data which had scrolled out of sight). 很少有硬件终端可以在更宽的区域内平移(很少有硬件终端可以以任何方式查看滚动到视线之外的数据)。 So what was standardized did not provide for this sort of feature, because the existing control sequences dealt with the visible screen. 因此,标准化的功能无法提供此类功能,因为现有的控制序列涉及可见屏幕。

There are of course exceptions (Hewlett Packard made some interesting terminals, for which you cannot find an emulator). 当然也有例外(Hewlett Packard制作了一些有趣的终端,您无法找到一个仿真器)。 xterm (and some that copied the feature) allows you to clear the scrollback. xterm(以及一些复制了该功能的部件)使您可以清除回滚。 But you're unlikely to find something that lets you (under program control) scroll back and modify the scrollback area. 但是您不太可能找到使您(在程序控制下)回滚并修改回滚区域的内容。

Application developers manage the visible display, writing lines on that using their own data rather than relying on the terminal to provide that out-of-sight information. 应用程序开发人员管理可见的显示,使用他们自己的数据在其上写行,而不是依靠终端来提供视线之外的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM