Windows console “ESC[2J” doesn't really “clear” the screen

纵然是瞬间 提交于 2021-02-10 16:10:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48612106/windows-console-esc2j-doesnt-really-clear-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!