Debian Codegraph @

node/@xterm/xterm/src/common/CoreTerminal.ts

raw
1 /**
2 * Copyright (c) 2014-2020 The xterm.js authors. All rights reserved.
3 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
4 * @license MIT
5 *
6 * Originally forked from (with the author's permission):
7 * Fabrice Bellard's javascript vt100 for jslinux:
8 * http://bellard.org/jslinux/
9 * Copyright (c) 2011 Fabrice Bellard
10 * The original design remains. The terminal itself
11 * has been extended to include xterm CSI codes, among
12 * other features.
13 *
14 * Terminal Emulation References:
15 * http://vt100.net/
16 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.txt
17 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
18 * http://invisible-island.net/vttest/
19 * http://www.inwap.com/pdp10/ansicode.txt
20 * http://linux.die.net/man/4/console_codes
21 * http://linux.die.net/man/7/urxvt
22 */
23
24 import { IInstantiationService, IOptionsService, IBufferService, ILogService, ICharsetService, ICoreService, ICoreMouseService, IUnicodeService, LogLevelEnum, ITerminalOptions, IOscLinkService } from 'common/services/Services';
25 import { InstantiationService } from 'common/services/InstantiationService';
26 import { LogService } from 'common/services/LogService';
27 import { BufferService, MINIMUM_COLS, MINIMUM_ROWS } from 'common/services/BufferService';
28 import { OptionsService } from 'common/services/OptionsService';
29 import { IDisposable, IAttributeData, ICoreTerminal, IScrollEvent } from 'common/Types';
30 import { CoreService } from 'common/services/CoreService';
31 import { CoreMouseService } from 'common/services/CoreMouseService';
32 import { UnicodeService } from 'common/services/UnicodeService';
33 import { CharsetService } from 'common/services/CharsetService';
34 import { updateWindowsModeWrappedState } from 'common/WindowsMode';
35 import { IFunctionIdentifier, IParams } from 'common/parser/Types';
36 import { IBufferSet } from 'common/buffer/Types';
37 import { InputHandler } from 'common/InputHandler';
38 import { WriteBuffer } from 'common/input/WriteBuffer';
39 import { OscLinkService } from 'common/services/OscLinkService';
40 import { Emitter, Event } from 'vs/base/common/event';
41 import { Disposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
42
43 // Only trigger this warning a single time per session
44 let hasWriteSyncWarnHappened = false;
45
46 export abstract class CoreTerminal extends Disposable implements ICoreTerminal {
47 protected readonly _instantiationService: IInstantiationService;
48 protected readonly _bufferService: IBufferService;
49 protected readonly _logService: ILogService;
50 protected readonly _charsetService: ICharsetService;
51 protected readonly _oscLinkService: IOscLinkService;
52
53 public readonly coreMouseService: ICoreMouseService;
54 public readonly coreService: ICoreService;
55 public readonly unicodeService: IUnicodeService;
56 public readonly optionsService: IOptionsService;
57
58 protected _inputHandler: InputHandler;
59 private _writeBuffer: WriteBuffer;
60 private _windowsWrappingHeuristics = this._register(new MutableDisposable());
61
62 private readonly _onBinary = this._register(new Emitter<string>());
63 public readonly onBinary = this._onBinary.event;
64 private readonly _onData = this._register(new Emitter<string>());
65 public readonly onData = this._onData.event;
66 protected _onLineFeed = this._register(new Emitter<void>());
67 public readonly onLineFeed = this._onLineFeed.event;
68 private readonly _onResize = this._register(new Emitter<{ cols: number, rows: number }>());
69 public readonly onResize = this._onResize.event;
70 protected readonly _onWriteParsed = this._register(new Emitter<void>());
71 public readonly onWriteParsed = this._onWriteParsed.event;
72
73 /**
74 * Internally we track the source of the scroll but this is meaningless outside the library so
75 * it's filtered out.
76 */
77 protected _onScrollApi?: Emitter<number>;
78 protected _onScroll = this._register(new Emitter<IScrollEvent>());
79 public get onScroll(): Event<number> {
80 if (!this._onScrollApi) {
81 this._onScrollApi = this._register(new Emitter<number>());
82 this._onScroll.event(ev => {
83 this._onScrollApi?.fire(ev.position);
84 });
85 }
86 return this._onScrollApi.event;
87 }
88
89 public get cols(): number { return this._bufferService.cols; }
90 public get rows(): number { return this._bufferService.rows; }
91 public get buffers(): IBufferSet { return this._bufferService.buffers; }
92 public get options(): Required<ITerminalOptions> { return this.optionsService.options; }
93 public set options(options: ITerminalOptions) {
94 for (const key in options) {
95 this.optionsService.options[key] = options[key];
96 }
97 }
98
99 constructor(
100 options: Partial<ITerminalOptions>
101 ) {
102 super();
103
104 // Setup and initialize services
105 this._instantiationService = new InstantiationService();
106 this.optionsService = this._register(new OptionsService(options));
107 this._instantiationService.setService(IOptionsService, this.optionsService);
108 this._bufferService = this._register(this._instantiationService.createInstance(BufferService));
109 this._instantiationService.setService(IBufferService, this._bufferService);
110 this._logService = this._register(this._instantiationService.createInstance(LogService));
111 this._instantiationService.setService(ILogService, this._logService);
112 this.coreService = this._register(this._instantiationService.createInstance(CoreService));
113 this._instantiationService.setService(ICoreService, this.coreService);
114 this.coreMouseService = this._register(this._instantiationService.createInstance(CoreMouseService));
115 this._instantiationService.setService(ICoreMouseService, this.coreMouseService);
116 this.unicodeService = this._register(this._instantiationService.createInstance(UnicodeService));
117 this._instantiationService.setService(IUnicodeService, this.unicodeService);
118 this._charsetService = this._instantiationService.createInstance(CharsetService);
119 this._instantiationService.setService(ICharsetService, this._charsetService);
120 this._oscLinkService = this._instantiationService.createInstance(OscLinkService);
121 this._instantiationService.setService(IOscLinkService, this._oscLinkService);
122
123
124 // Register input handler and handle/forward events
125 this._inputHandler = this._register(new InputHandler(this._bufferService, this._charsetService, this.coreService, this._logService, this.optionsService, this._oscLinkService, this.coreMouseService, this.unicodeService));
126 this._register(Event.forward(this._inputHandler.onLineFeed, this._onLineFeed));
127 this._register(this._inputHandler);
128
129 // Setup listeners
130 this._register(Event.forward(this._bufferService.onResize, this._onResize));
131 this._register(Event.forward(this.coreService.onData, this._onData));
132 this._register(Event.forward(this.coreService.onBinary, this._onBinary));
133 this._register(this.coreService.onRequestScrollToBottom(() => this.scrollToBottom(true)));
134 this._register(this.coreService.onUserInput(() => this._writeBuffer.handleUserInput()));
135 this._register(this.optionsService.onMultipleOptionChange(['windowsMode', 'windowsPty'], () => this._handleWindowsPtyOptionChange()));
136 this._register(this._bufferService.onScroll(() => {
137 this._onScroll.fire({ position: this._bufferService.buffer.ydisp });
138 this._inputHandler.markRangeDirty(this._bufferService.buffer.scrollTop, this._bufferService.buffer.scrollBottom);
139 }));
140 // Setup WriteBuffer
141 this._writeBuffer = this._register(new WriteBuffer((data, promiseResult) => this._inputHandler.parse(data, promiseResult)));
142 this._register(Event.forward(this._writeBuffer.onWriteParsed, this._onWriteParsed));
143 }
144
145 public write(data: string | Uint8Array, callback?: () => void): void {
146 this._writeBuffer.write(data, callback);
147 }
148
149 /**
150 * Write data to terminal synchonously.
151 *
152 * This method is unreliable with async parser handlers, thus should not
153 * be used anymore. If you need blocking semantics on data input consider
154 * `write` with a callback instead.
155 *
156 * @deprecated Unreliable, will be removed soon.
157 */
158 public writeSync(data: string | Uint8Array, maxSubsequentCalls?: number): void {
159 if (this._logService.logLevel <= LogLevelEnum.WARN && !hasWriteSyncWarnHappened) {
160 this._logService.warn('writeSync is unreliable and will be removed soon.');
161 hasWriteSyncWarnHappened = true;
162 }
163 this._writeBuffer.writeSync(data, maxSubsequentCalls);
164 }
165
166 public input(data: string, wasUserInput: boolean = true): void {
167 this.coreService.triggerDataEvent(data, wasUserInput);
168 }
169
170 public resize(x: number, y: number): void {
171 if (isNaN(x) || isNaN(y)) {
172 return;
173 }
174
175 x = Math.max(x, MINIMUM_COLS);
176 y = Math.max(y, MINIMUM_ROWS);
177
178 this._bufferService.resize(x, y);
179 }
180
181 /**
182 * Scroll the terminal down 1 row, creating a blank line.
183 * @param eraseAttr The attribute data to use the for blank line.
184 * @param isWrapped Whether the new line is wrapped from the previous line.
185 */
186 public scroll(eraseAttr: IAttributeData, isWrapped: boolean = false): void {
187 this._bufferService.scroll(eraseAttr, isWrapped);
188 }
189
190 /**
191 * Scroll the display of the terminal
192 * @param disp The number of lines to scroll down (negative scroll up).
193 * @param suppressScrollEvent Don't emit the scroll event as scrollLines. This is used to avoid
194 * unwanted events being handled by the viewport when the event was triggered from the viewport
195 * originally.
196 */
197 public scrollLines(disp: number, suppressScrollEvent?: boolean): void {
198 this._bufferService.scrollLines(disp, suppressScrollEvent);
199 }
200
201 public scrollPages(pageCount: number): void {
202 this.scrollLines(pageCount * (this.rows - 1));
203 }
204
205 public scrollToTop(): void {
206 this.scrollLines(-this._bufferService.buffer.ydisp);
207 }
208
209 public scrollToBottom(disableSmoothScroll?: boolean): void {
210 this.scrollLines(this._bufferService.buffer.ybase - this._bufferService.buffer.ydisp);
211 }
212
213 public scrollToLine(line: number): void {
214 const scrollAmount = line - this._bufferService.buffer.ydisp;
215 if (scrollAmount !== 0) {
216 this.scrollLines(scrollAmount);
217 }
218 }
219
220 /** Add handler for ESC escape sequence. See xterm.d.ts for details. */
221 public registerEscHandler(id: IFunctionIdentifier, callback: () => boolean | Promise<boolean>): IDisposable {
222 return this._inputHandler.registerEscHandler(id, callback);
223 }
224
225 /** Add handler for DCS escape sequence. See xterm.d.ts for details. */
226 public registerDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: IParams) => boolean | Promise<boolean>): IDisposable {
227 return this._inputHandler.registerDcsHandler(id, callback);
228 }
229
230 /** Add handler for CSI escape sequence. See xterm.d.ts for details. */
231 public registerCsiHandler(id: IFunctionIdentifier, callback: (params: IParams) => boolean | Promise<boolean>): IDisposable {
232 return this._inputHandler.registerCsiHandler(id, callback);
233 }
234
235 /** Add handler for OSC escape sequence. See xterm.d.ts for details. */
236 public registerOscHandler(ident: number, callback: (data: string) => boolean | Promise<boolean>): IDisposable {
237 return this._inputHandler.registerOscHandler(ident, callback);
238 }
239
240 protected _setup(): void {
241 this._handleWindowsPtyOptionChange();
242 }
243
244 public reset(): void {
245 this._inputHandler.reset();
246 this._bufferService.reset();
247 this._charsetService.reset();
248 this.coreService.reset();
249 this.coreMouseService.reset();
250 }
251
252
253 private _handleWindowsPtyOptionChange(): void {
254 let value = false;
255 const windowsPty = this.optionsService.rawOptions.windowsPty;
256 if (windowsPty && windowsPty.buildNumber !== undefined && windowsPty.buildNumber !== undefined) {
257 value = !!(windowsPty.backend === 'conpty' && windowsPty.buildNumber < 21376);
258 } else if (this.optionsService.rawOptions.windowsMode) {
259 value = true;
260 }
261 if (value) {
262 this._enableWindowsWrappingHeuristics();
263 } else {
264 this._windowsWrappingHeuristics.clear();
265 }
266 }
267
268 protected _enableWindowsWrappingHeuristics(): void {
269 if (!this._windowsWrappingHeuristics.value) {
270 const disposables: IDisposable[] = [];
271 disposables.push(this.onLineFeed(updateWindowsModeWrappedState.bind(null, this._bufferService)));
272 disposables.push(this.registerCsiHandler({ final: 'H' }, () => {
273 updateWindowsModeWrappedState(this._bufferService);
274 return false;
275 }));
276 this._windowsWrappingHeuristics.value = toDisposable(() => {
277 for (const d of disposables) {
278 d.dispose();
279 }
280 });
281 }
282 }
283 }