Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

terminal - Clearing the screen by printing a character?

I'm using chez-scheme and I can't find a way to clear the screen completely. (If someone knows a better way than printing I'd be interested in that too but it's not my question here)

From what I can find clearing the screen by ^L (control-L) or giving the clear command (in bash at least) is equivalent to outputting ASCII character 12: Form feed. However, printing this does nothing. If I use (display (integer->char 12)) it just prints a newline. Another way to encode this character is f (analogous to for newline), but in Python print("f") as well as in Scheme (display "f") is just a newline.

Is my understanding of the meaning of ASCII 12 just wrong, or are implementations lacking?

Is there any way to clear the screen that should work across languages, analogous to for a newline?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to clear the screen, the "ANSI" sequence in a printf

33[2J

clears the entire screen, e.g.,

printf '33[2J'

The command-line clear program uses this, along with moving the cursor to the "home" position, again an "ANSI" sequence:

33[H

The program gets the information from the terminal database. For example, for TERM=vt100, it might see this (using E as 33):

clear=E[HE[J$<50>

(the $<50> indicates padding needed for real VT100s). You might notice that the 2 is absent from this string. That is because the cursor is first moved to the home (upper left) position, and the 2 (entire screen) is not necessary. Eliminating that from the string made VT100s a little faster.

On the other hand, if you just want to reset the terminal, you can use the VT100-style RIS:

33c

but that has side-effects, besides not being in ECMA-48. These bug reports were for side-effects of 33c:

Further reading:

CSI Ps J  Erase in Display (ED).
            Ps = 0  -> Erase Below (default).
            Ps = 1  -> Erase Above.
            Ps = 2  -> Erase All.
            Ps = 3  -> Erase Saved Lines (xterm).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...