Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atomatic render, replace the current buffer without causing re-render #3859

Closed
Lantianyou opened this issue Jun 13, 2022 · 1 comment
Closed
Labels
as-designed type/question A question on how to use the library

Comments

@Lantianyou
Copy link

Lantianyou commented Jun 13, 2022

Hi everyone, I am really new to xterm.js and maybe suggesting a very dump question.

Every single time, the clear() method is called, the scroll event is fired as the source here shows:
(

public clear(): void {
if (this.buffer.ybase === 0 && this.buffer.y === 0) {
// Don't clear if it's already clear
return;
}
this.buffer.clearAllMarkers(0);
this.buffer.lines.set(0, this.buffer.lines.get(this.buffer.ybase + this.buffer.y)!);
this.buffer.lines.length = 1;
this.buffer.ydisp = 0;
this.buffer.ybase = 0;
this.buffer.y = 0;
for (let i = 1; i < this.rows; i++) {
this.buffer.lines.push(this.buffer.getBlankLine(DEFAULT_ATTR_DATA));
}
this.refresh(0, this.rows - 1);
this._onScroll.fire({ position: this.buffer.ydisp, source: ScrollSource.TERMINAL });
}
)
However, I want to clear the current buffer without causing the scroll event to fire.
Since I want to replace the current buffer with new buffer. My implementation is like this:

terminal.clear()
newBuffer.map(line => terminal.writeln(line))
terminal.scrollToBottom()

But the terminal instance is rendered twice, 1 after clear() 2. the terminal with newBuffer. And the scroll event is fired in between.

I want to

  1. simply renter the terminal once or
  2. after clear method is called, do not fire the scroll event.
@Tyriar
Copy link
Member

Tyriar commented Jun 13, 2022

While we should maybe not be firing that event if ydisp was 0 before, we fire it because the scroll bar/scroll state changed. As for the double rendering, this is by design since writing to the terminal is always queued/async:

https://github.com/Tyriar/xterm.js/blob/a5f2ea336eff69c784414f371baacde641f03517/src/common/input/WriteBuffer.ts#L105-L109

There is no way to do an atomic render like this and I don't think it's worth adding, but in your case you could hide the element temporarily and then show it again

// hide the terminal element
terminal.clear()
// FYI forEach is better here as it will not create and return another array
newBuffer.forEach(line => terminal.writeln(line))
terminal.scrollToBottom()
// show the terminal element

@Tyriar Tyriar closed this as completed Jun 13, 2022
@Tyriar Tyriar added type/question A question on how to use the library as-designed labels Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as-designed type/question A question on how to use the library
Projects
None yet
Development

No branches or pull requests

2 participants