Debian Codegraph @

node/@xterm/xterm/src/common/services/BufferService.ts

raw
1 /**
2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { Disposable } from 'vs/base/common/lifecycle';
7 import { IAttributeData, IBufferLine } from 'common/Types';
8 import { BufferSet } from 'common/buffer/BufferSet';
9 import { IBuffer, IBufferSet } from 'common/buffer/Types';
10 import { IBufferService, IOptionsService, type IBufferResizeEvent } from 'common/services/Services';
11 import { Emitter } from 'vs/base/common/event';
12
13 export const MINIMUM_COLS = 2; // Less than 2 can mess with wide chars
14 export const MINIMUM_ROWS = 1;
15
16 export class BufferService extends Disposable implements IBufferService {
17 public serviceBrand: any;
18
19 public cols: number;
20 public rows: number;
21 public buffers: IBufferSet;
22 /** Whether the user is scrolling (locks the scroll position) */
23 public isUserScrolling: boolean = false;
24
25 private readonly _onResize = this._register(new Emitter<IBufferResizeEvent>());
26 public readonly onResize = this._onResize.event;
27 private readonly _onScroll = this._register(new Emitter<number>());
28 public readonly onScroll = this._onScroll.event;
29
30 public get buffer(): IBuffer { return this.buffers.active; }
31
32 /** An IBufferline to clone/copy from for new blank lines */
33 private _cachedBlankLine: IBufferLine | undefined;
34
35 constructor(@IOptionsService optionsService: IOptionsService) {
36 super();
37 this.cols = Math.max(optionsService.rawOptions.cols || 0, MINIMUM_COLS);
38 this.rows = Math.max(optionsService.rawOptions.rows || 0, MINIMUM_ROWS);
39 this.buffers = this._register(new BufferSet(optionsService, this));
40 this._register(this.buffers.onBufferActivate(e => {
41 this._onScroll.fire(e.activeBuffer.ydisp);
42 }));
43 }
44
45 public resize(cols: number, rows: number): void {
46 const colsChanged = this.cols !== cols;
47 const rowsChanged = this.rows !== rows;
48 this.cols = cols;
49 this.rows = rows;
50 this.buffers.resize(cols, rows);
51 this._onResize.fire({ cols, rows, colsChanged, rowsChanged });
52 }
53
54 public reset(): void {
55 this.buffers.reset();
56 this.isUserScrolling = false;
57 }
58
59 /**
60 * Scroll the terminal down 1 row, creating a blank line.
61 * @param eraseAttr The attribute data to use the for blank line.
62 * @param isWrapped Whether the new line is wrapped from the previous line.
63 */
64 public scroll(eraseAttr: IAttributeData, isWrapped: boolean = false): void {
65 const buffer = this.buffer;
66
67 let newLine: IBufferLine | undefined;
68 newLine = this._cachedBlankLine;
69 if (!newLine || newLine.length !== this.cols || newLine.getFg(0) !== eraseAttr.fg || newLine.getBg(0) !== eraseAttr.bg) {
70 newLine = buffer.getBlankLine(eraseAttr, isWrapped);
71 this._cachedBlankLine = newLine;
72 }
73 newLine.isWrapped = isWrapped;
74
75 const topRow = buffer.ybase + buffer.scrollTop;
76 const bottomRow = buffer.ybase + buffer.scrollBottom;
77
78 if (buffer.scrollTop === 0) {
79 // Determine whether the buffer is going to be trimmed after insertion.
80 const willBufferBeTrimmed = buffer.lines.isFull;
81
82 // Insert the line using the fastest method
83 if (bottomRow === buffer.lines.length - 1) {
84 if (willBufferBeTrimmed) {
85 buffer.lines.recycle().copyFrom(newLine);
86 } else {
87 buffer.lines.push(newLine.clone());
88 }
89 } else {
90 buffer.lines.splice(bottomRow + 1, 0, newLine.clone());
91 }
92
93 // Only adjust ybase and ydisp when the buffer is not trimmed
94 if (!willBufferBeTrimmed) {
95 buffer.ybase++;
96 // Only scroll the ydisp with ybase if the user has not scrolled up
97 if (!this.isUserScrolling) {
98 buffer.ydisp++;
99 }
100 } else {
101 // When the buffer is full and the user has scrolled up, keep the text
102 // stable unless ydisp is right at the top
103 if (this.isUserScrolling) {
104 buffer.ydisp = Math.max(buffer.ydisp - 1, 0);
105 }
106 }
107 } else {
108 // scrollTop is non-zero which means no line will be going to the
109 // scrollback, instead we can just shift them in-place.
110 const scrollRegionHeight = bottomRow - topRow + 1 /* as it's zero-based */;
111 buffer.lines.shiftElements(topRow + 1, scrollRegionHeight - 1, -1);
112 buffer.lines.set(bottomRow, newLine.clone());
113 }
114
115 // Move the viewport to the bottom of the buffer unless the user is
116 // scrolling.
117 if (!this.isUserScrolling) {
118 buffer.ydisp = buffer.ybase;
119 }
120
121 this._onScroll.fire(buffer.ydisp);
122 }
123
124 /**
125 * Scroll the display of the terminal
126 * @param disp The number of lines to scroll down (negative scroll up).
127 * @param suppressScrollEvent Don't emit the scroll event as scrollLines. This is used
128 * to avoid unwanted events being handled by the viewport when the event was triggered from the
129 * viewport originally.
130 */
131 public scrollLines(disp: number, suppressScrollEvent?: boolean): void {
132 const buffer = this.buffer;
133 if (disp < 0) {
134 if (buffer.ydisp === 0) {
135 return;
136 }
137 this.isUserScrolling = true;
138 } else if (disp + buffer.ydisp >= buffer.ybase) {
139 this.isUserScrolling = false;
140 }
141
142 const oldYdisp = buffer.ydisp;
143 buffer.ydisp = Math.max(Math.min(buffer.ydisp + disp, buffer.ybase), 0);
144
145 // No change occurred, don't trigger scroll/refresh
146 if (oldYdisp === buffer.ydisp) {
147 return;
148 }
149
150 if (!suppressScrollEvent) {
151 this._onScroll.fire(buffer.ydisp);
152 }
153 }
154 }