xtermjs / xterm.js

A terminal for the web
https://xtermjs.org/
MIT License
17.04k stars 1.6k forks source link

Unable to clear terminal using clear method #5019

Closed destinytaoer closed 2 months ago

destinytaoer commented 3 months ago

Details

Unable to clear terminal using clear(),but reset() can clear the terminal what is the different between clear() and reset()? temrinal

Steps to reproduce

  1. terminal.write("something\r\n")
  2. temrinal.write("last line")
  3. setTimeout(() => terminal.clear(), 3000) image
destinytaoer commented 3 months ago

The last line can't be cleared. But when i write the last line with CRLF, it can be cleared.

Skinbow commented 2 months ago

Hello. From what I gather, reset actually resets the entire terminal (it restarts all the running services dependent on the terminal). However (quoting comment) "reset does bot clear input buffers and does not reset the parser". Clear on the other hand simply clears the buffer, while making the prompt line the first line in the buffer and firing a scroll event, which is why it isn't clearing everything, but rather keeping the last line intact. I hope this helped.

destinytaoer commented 2 months ago

Hello. From what I gather, reset actually resets the entire terminal (it restarts all the running services dependent on the terminal). However (quoting comment) "reset does bot clear input buffers and does not reset the parser". Clear on the other hand simply clears the buffer, while making the prompt line the first line in the buffer and firing a scroll event, which is why it isn't clearing everything, but rather keeping the last line intact. I hope this helped.

I got it. The clear method is like entering clear at a terminal, it just clear historical output, and the first line is considered as the user's input. It's that right?

destinytaoer commented 2 months ago

so when i want to clear all input, just use reset method? other advise?

jerch commented 2 months ago

.reset() might not be what you want, it effectively reboots the terminal (its like pressing off/on on a device), which can have unwanted side effects like erasing pending input. Normally it is enough to call the VT function DECSTR ('\x1b[!p') or RIS ('\x1bc').

destinytaoer commented 2 months ago

.reset() might not be what you want, it effectively reboots the terminal (its like pressing off/on on a device), which can have unwanted side effects like erasing pending input. Normally it is enough to call the VT function DECSTR ('\x1b[!p') or RIS ('\x1bc').

image

it seems that reset and RIS are the same

jerch commented 2 months ago

There is a difference - RIS is band-synchronous (postponed until all previous input in the write buffer was handled), while term.reset() gets executed immediately in JS, thus might cut into any state of the terminal. But sure DECSTR is most of the time enough.

And if you just want to clear the viewport + scrollback while leaving other terminal states untouched, ED 2 + ED 3 + CUP is your friend ('\x1b[2J\x1b[3J\x1b[H');

destinytaoer commented 2 months ago

There is a difference - RIS is band-synchronous (postponed until all previous input in the write buffer was handled), while term.reset() gets executed immediately in JS, thus might cut into any state of the terminal. But sure DECSTR is most of the time enough.

And if you just want to clear the viewport + scrollback while leaving other terminal states untouched, ED 2 + ED 3 + CUP is your friend ('\x1b[2J\x1b[3J\x1b[H');

Got it, Thank you very much!