sindresorhus / ansi-escapes

ANSI escape codes for manipulating the terminal
MIT License
494 stars 44 forks source link

clearScreen performs a full reset on some terminals #25

Open Tyriar opened 3 years ago

Tyriar commented 3 years ago

👋

We just had this issue reported which said \x1bc (from clearScreen) in xterm.js behaves differently to Terminal.app/iTerm https://github.com/xtermjs/xterm.js/issues/3315. We behave as xterm/VTE does and don't think we should change so I would suggest indicating the fact in this lib that clearScreen may actually clear everything.

Note also that RIS is also more destructive than just clearing the buffer, also resetting modes among other things.

Qix- commented 3 years ago

Hey @Tyriar :)

Yeah this is a naming issue. Most people want clearTerminal, which emits the (correct) \x1b[2J\x1b[3J\x1b[H.

clearScreen should be renamed to fullReset IMO, though this would be a major release.

SamVerschueren commented 3 years ago

@Qix- The problem with clearTerminal is that it resets the scrollback buffer, which is not always what people want. So from the docs clearScreen should actually clear the screen without resetting the buffer. So what I think what should happen is assign \u001B[H\u001B[2J to clearScreen (which is what the clear command prints when executed) and use \u001Bc as value for a new fullReset command.

Something like this

exports.clearScreen = `${ESC}H${ESC}2J`;
exports.fullReset = '\u001Bc';

Then clearScreen does what it's supposed to do according to the docs

Clear the terminal screen. (Viewport)

What do you think?

Qix- commented 3 years ago

Yes sorry, I meant to edit addressing your original ticket but my internet cut out.

You want eraseScreen. The cursor position should reset in that case, but if it doesn't then we should also add ESC[H to it as well (though I don't think that's an issue).

SamVerschueren commented 3 years ago

The problem with eraseScreen is that it seems to erase the screen but the cursor is not moved to the top left.

image

When I print the eraseScreen sequence with printf '\u001B[2J'

image

So I think we have to add ESC[H to move the cursor to the top left first.

Qix- commented 3 years ago

Interesting, it seems to be random which terminals reset cursor position and which do not. Fun.

It's @sindresorhus's decision - should eraseScreen be the pure terminal code or should it have the opinion that the cursor also goes to the top-left?

If the latter (which I would agree with), then ESC[H should be appended to eraseScreen and clearTerminal should just have the codes ESC[2JESC[3JESC[H directly instead of re-using eraseScreen.

jerch commented 3 years ago

Maybe establish a difference between erase and clear. Idea:

This model tries to resemble the ECMA-48 sequence ideas (VT100+), have not looked up your existing definitions, thus it might collide here or there. Well see it as an inspiration.

sindresorhus commented 3 years ago

There is some useful info in https://github.com/watchexec/clearscreen/blob/main/src/lib.rs