ansi-escapes icon indicating copy to clipboard operation
ansi-escapes copied to clipboard

clearScreen performs a full reset on some terminals

Open Tyriar opened this issue 3 years ago • 7 comments

👋

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.

Tyriar avatar May 05 '21 11:05 Tyriar

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.

Qix- avatar May 05 '21 12:05 Qix-

@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?

SamVerschueren avatar May 05 '21 12:05 SamVerschueren

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).

Qix- avatar May 05 '21 12:05 Qix-

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.

SamVerschueren avatar May 05 '21 12:05 SamVerschueren

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.

Qix- avatar May 05 '21 13:05 Qix-

Maybe establish a difference between erase and clear. Idea:

  • eraseLine - erasing line content, leaving the cursor alone
  • eraseLineLeft - erasing line content left of the cursor, leaving the cursor alone
  • eraseLineRight - erasing line content right of the cursor (+ content at cursor position), leaving the cursor alone
  • clearLine - erasing line content + cursor to left
  • eraseScreen - erasing screen content, leaving the cursor alone
  • eraseScreenTop - erasing screen top content up to cursor position, leaving the cursor alone
  • eraseScreenBottom - erasing screen bottom content from cursor position, leaving the cursor alone
  • clearScreen - easing content + cursor to top-left
  • clearScrollback - nullify scrollback
  • clearTerminal - same as clearScreen + clearScrollback
  • resetTerminal - RIS (or DECSTR, which is less "rebooty")

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.

jerch avatar May 06 '21 10:05 jerch

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

sindresorhus avatar May 20 '21 07:05 sindresorhus