[Toybox] [PATCH] clear.c: Clear scrollback buffer on non-vte (gnome based) terminals

Oliver Webb aquahobbyist at proton.me
Tue Mar 5 12:44:04 PST 2024


>From https://landley.net/notes-2022.html#17-10-2022:
the smallest one is "clear" which is actually fraught (the escape does not reset the TTY out of "cooked" mode,

ncurses clear doesn't seem to effect tty settings (unix2dox because -opost):
$ stty raw && stty | unix2dos
speed 38400 baud; line = 0;
min = 1; time = 0;
-brkint -icrnl -imaxbel
-opost
-isig -icanon
$ clear && stty | unix2dos
speed 38400 baud; line = 0;
-brkint -icrnl -imaxbel
-opost
-isig
$ stty cooked && stty
speed 38400 baud; line = 0;
-imaxbel iutf8
$ clear && stty
speed 38400 baud; line = 0;
-imaxbel

Looking more at ncurses clear, what does it do?
It has a -T option to specify TERM (because env wasn't good enough apparently):
$ clear -T linux | xxd
00000000: 1b5b 481b 5b4a 1b5b 334a                 .[H.[J.[3J
$ clear -T tmux | xxd
00000000: 1b5b 481b 5b4a 1b5b 334a                 .[H.[J.[3J
$ clear -T xterm | xxd
00000000: 1b5b 481b 5b32 4a1b 5b33 4a              .[H.[2J.[3J
$ clear -T st | xxd
00000000: 1b5b 481b 5b32 4a                        .[H.[2J
$ clear -T vt100 | xxd
00000000: 1b5b 481b 5b4a 0000 0000 0000 0000 0000  .[H.[J..........
[Large block of zeros]
$ env -i clear
TERM environment variable not set

Trying to find all the terminals clear supported lead me down a rabbit hole
through decompiling the entire terminfo database and sorting through it.
The answer is 159 unique ways, some of them being line mode ttys that clear by
doing '\r\n' and _4_ things for the Apple II (You can set your TERM to 
aepro, apple-videx{2,3}, and apple-80 to get them, in case you were interested).

"\ec" is supported by linux ttys, and clears the screen in all terminals I tested.
And "\e[3J" has been supported by linux since 3.0, and clears the scrollback buffer in every
terminal I've tested that has one, and is a NOP for terminals without one like linux tty's or st 

-   Oliver Webb <aquahobbyist at proton.me>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-clear.c-Clear-scrollback-buffer-on-Non-VTE-Terminals.patch
Type: text/x-patch
Size: 597 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20240305/d6eb5710/attachment.bin>


More information about the Toybox mailing list