Windows控制台"ESC [2J""并不是真正的“清除"屏幕 [英] Windows console "ESC[2J" doesn't really "clear" the screen

查看:85
本文介绍了Windows控制台"ESC [2J""并不是真正的“清除"屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这类问题经常被问到,但是我认为这个问题有些不同,需要提出.

新的Windows控制台支持ANSI(VT100)控制代码: ANSI/VT100控制代码& Windows文档:控制代码.

但是, ESC [2J "并不能真正清除"屏幕,只是向下滚动以隐藏"打印内容.只有 ESC [H + ESC [J "真正清除"了屏幕上当前显示的内容.这是错误还是专为此目的?它写在某种文档上吗?

请运行以下 .bat 来了解我的意思:

  @echo关闭回声hello0回声hello1回声hello2回声hello3回声hello4回声你好5回声你好6回声你好7回声你好8回声你好9暂停回声[2J暂停回声[H [J暂停 

或运行以下 .py (Python 2):

  import sys从ctypes导入windllwindll.kernel32.SetConsoleMode(windll.kernel32.GetStdHandle(-11),7)#set ansi(vt100)控制代码解释#https://stackoverflow.com/questions/36760127/how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console显示= lambda s:sys.stdout.write(s)对于我在范围(10)中:打印("\ x1b [30; 47m你好\ x1b [0m%d"%i)raw_input()show("\ x1b [2J")raw_input()show("\ x1b [H \ x1b [J")raw_input() 

运行该简单脚本时可以看到, ESC [2J "只是向下滚动"以使屏幕清晰",内容仍然存在.

这有点难以解释,如果您不明白我的意思,请发表评论,谢谢!

另一个小问题:为什么 cls 命令比 ESC [2J ESC [H + ESC [J ?

解决方案

在支持控制序列的Windows的较早版本中," ESC [2J "的问题是已知的".也就是说,它应该但没有清除整个屏幕.您可能会发现一些近期评论.没有人实际测试过最近恢复的Windows控制序列,以指出与其他终端的兼容性.

控制台窗口具有一个缓冲区",其中包含可见字符滚动, cls 可以完全清除,大概比仅清除可见字符要花费更多的时间./p>

I know this kind of questions are asked frequently, but I think this one is a little different and needed to be asked.

The new Windows console supports ANSI (VT100) control codes: ANSI/VT100 control codes & Windows document: the control codes.

However, ESC[2J doesn't really "clear" the screen, it just scrolls down to "hide" printed contents. Only ESC[H+ESC[J really "clears" the currently showed contents on the screen. Is this a bug or it is designed to do this? Is it written on some kind of documentation?

Please run this .bat to see what I mean:

@echo off
echo hello0
echo hello1
echo hello2
echo hello3
echo hello4
echo hello5
echo hello6
echo hello7
echo hello8
echo hello9
pause
echo [2J
pause
echo [H[J
pause

Or run this .py(Python 2):

import sys
from ctypes import windll
windll.kernel32.SetConsoleMode(windll.kernel32.GetStdHandle(-11), 7)
#set ansi(vt100) control code interpreted
#https://stackoverflow.com/questions/36760127/how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console
show = lambda s: sys.stdout.write(s)
for i in range(10):
    print("\x1b[30;47m hello \x1b[0m%d"%i)
raw_input()
show("\x1b[2J")
raw_input()
show("\x1b[H\x1b[J")
raw_input()

As you can see when running this simple script, ESC[2J just "scrolls down" to "make your screen clear", the contents are still there.

This is a little hard to explain, please comment if you don't understand what I mean, thank you!

Another small question: why cls command is slower than ESC[2J or ESC[H+ESC[J?

解决方案

The issue withESC[2J was "known" in older versions of Windows which supported control sequences. That is, it should, but did not clear the whole screen. You might find some recent comment. No one's actually tested the recently reinstated Windows control sequences to point out compatibility with other terminals.

A console window has a "buffer" holding the visible characters plus the scrollback, which cls clears entirely, presumably taking more time than clearing just the visible characters.

这篇关于Windows控制台"ESC [2J""并不是真正的“清除"屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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