Debian Codegraph @

node/@xterm/xterm/src/browser/public/Terminal.ts

raw
1 /**
2 * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import * as Strings from 'browser/LocalizableStrings';
7 import { CoreBrowserTerminal as TerminalCore } from 'browser/CoreBrowserTerminal';
8 import { IBufferRange, ITerminal } from 'browser/Types';
9 import { Disposable } from 'vs/base/common/lifecycle';
10 import { ITerminalOptions } from 'common/Types';
11 import { AddonManager } from 'common/public/AddonManager';
12 import { BufferNamespaceApi } from 'common/public/BufferNamespaceApi';
13 import { ParserApi } from 'common/public/ParserApi';
14 import { UnicodeApi } from 'common/public/UnicodeApi';
15 import { IBufferNamespace as IBufferNamespaceApi, IDecoration, IDecorationOptions, IDisposable, ILinkProvider, ILocalizableStrings, IMarker, IModes, IParser, ITerminalAddon, Terminal as ITerminalApi, ITerminalInitOnlyOptions, IUnicodeHandling } from '@xterm/xterm';
16 import type { Event } from 'vs/base/common/event';
17
18 /**
19 * The set of options that only have an effect when set in the Terminal constructor.
20 */
21 const CONSTRUCTOR_ONLY_OPTIONS = ['cols', 'rows'];
22
23 let $value = 0;
24
25 export class Terminal extends Disposable implements ITerminalApi {
26 private _core: ITerminal;
27 private _addonManager: AddonManager;
28 private _parser: IParser | undefined;
29 private _buffer: BufferNamespaceApi | undefined;
30 private _publicOptions: Required<ITerminalOptions>;
31
32 constructor(options?: ITerminalOptions & ITerminalInitOnlyOptions) {
33 super();
34
35 this._core = this._register(new TerminalCore(options));
36 this._addonManager = this._register(new AddonManager());
37
38 this._publicOptions = { ... this._core.options };
39 const getter = (propName: string): any => {
40 return this._core.options[propName];
41 };
42 const setter = (propName: string, value: any): void => {
43 this._checkReadonlyOptions(propName);
44 this._core.options[propName] = value;
45 };
46
47 for (const propName in this._core.options) {
48 const desc = {
49 get: getter.bind(this, propName),
50 set: setter.bind(this, propName)
51 };
52 Object.defineProperty(this._publicOptions, propName, desc);
53 }
54 }
55
56 private _checkReadonlyOptions(propName: string): void {
57 // Throw an error if any constructor only option is modified
58 // from terminal.options
59 // Modifications from anywhere else are allowed
60 if (CONSTRUCTOR_ONLY_OPTIONS.includes(propName)) {
61 throw new Error(`Option "${propName}" can only be set in the constructor`);
62 }
63 }
64
65 private _checkProposedApi(): void {
66 if (!this._core.optionsService.rawOptions.allowProposedApi) {
67 throw new Error('You must set the allowProposedApi option to true to use proposed API');
68 }
69 }
70
71 public get onBell(): Event<void> { return this._core.onBell; }
72 public get onBinary(): Event<string> { return this._core.onBinary; }
73 public get onCursorMove(): Event<void> { return this._core.onCursorMove; }
74 public get onData(): Event<string> { return this._core.onData; }
75 public get onKey(): Event<{ key: string, domEvent: KeyboardEvent }> { return this._core.onKey; }
76 public get onLineFeed(): Event<void> { return this._core.onLineFeed; }
77 public get onRender(): Event<{ start: number, end: number }> { return this._core.onRender; }
78 public get onResize(): Event<{ cols: number, rows: number }> { return this._core.onResize; }
79 public get onScroll(): Event<number> { return this._core.onScroll; }
80 public get onSelectionChange(): Event<void> { return this._core.onSelectionChange; }
81 public get onTitleChange(): Event<string> { return this._core.onTitleChange; }
82 public get onWriteParsed(): Event<void> { return this._core.onWriteParsed; }
83
84 public get element(): HTMLElement | undefined { return this._core.element; }
85 public get parser(): IParser {
86 if (!this._parser) {
87 this._parser = new ParserApi(this._core);
88 }
89 return this._parser;
90 }
91 public get unicode(): IUnicodeHandling {
92 this._checkProposedApi();
93 return new UnicodeApi(this._core);
94 }
95 public get textarea(): HTMLTextAreaElement | undefined { return this._core.textarea; }
96 public get rows(): number { return this._core.rows; }
97 public get cols(): number { return this._core.cols; }
98 public get buffer(): IBufferNamespaceApi {
99 if (!this._buffer) {
100 this._buffer = this._register(new BufferNamespaceApi(this._core));
101 }
102 return this._buffer;
103 }
104 public get markers(): ReadonlyArray<IMarker> {
105 this._checkProposedApi();
106 return this._core.markers;
107 }
108 public get modes(): IModes {
109 const m = this._core.coreService.decPrivateModes;
110 let mouseTrackingMode: 'none' | 'x10' | 'vt200' | 'drag' | 'any' = 'none';
111 switch (this._core.coreMouseService.activeProtocol) {
112 case 'X10': mouseTrackingMode = 'x10'; break;
113 case 'VT200': mouseTrackingMode = 'vt200'; break;
114 case 'DRAG': mouseTrackingMode = 'drag'; break;
115 case 'ANY': mouseTrackingMode = 'any'; break;
116 }
117 return {
118 applicationCursorKeysMode: m.applicationCursorKeys,
119 applicationKeypadMode: m.applicationKeypad,
120 bracketedPasteMode: m.bracketedPasteMode,
121 insertMode: this._core.coreService.modes.insertMode,
122 mouseTrackingMode: mouseTrackingMode,
123 originMode: m.origin,
124 reverseWraparoundMode: m.reverseWraparound,
125 sendFocusMode: m.sendFocus,
126 synchronizedOutputMode: m.synchronizedOutput,
127 wraparoundMode: m.wraparound
128 };
129 }
130 public get options(): Required<ITerminalOptions> {
131 return this._publicOptions;
132 }
133 public set options(options: ITerminalOptions) {
134 for (const propName in options) {
135 this._publicOptions[propName] = options[propName];
136 }
137 }
138 public blur(): void {
139 this._core.blur();
140 }
141 public focus(): void {
142 this._core.focus();
143 }
144 public input(data: string, wasUserInput: boolean = true): void {
145 this._core.input(data, wasUserInput);
146 }
147 public resize(columns: number, rows: number): void {
148 this._verifyIntegers(columns, rows);
149 this._core.resize(columns, rows);
150 }
151 public open(parent: HTMLElement): void {
152 this._core.open(parent);
153 }
154 public attachCustomKeyEventHandler(customKeyEventHandler: (event: KeyboardEvent) => boolean): void {
155 this._core.attachCustomKeyEventHandler(customKeyEventHandler);
156 }
157 public attachCustomWheelEventHandler(customWheelEventHandler: (event: WheelEvent) => boolean): void {
158 this._core.attachCustomWheelEventHandler(customWheelEventHandler);
159 }
160 public registerLinkProvider(linkProvider: ILinkProvider): IDisposable {
161 return this._core.registerLinkProvider(linkProvider);
162 }
163 public registerCharacterJoiner(handler: (text: string) => [number, number][]): number {
164 this._checkProposedApi();
165 return this._core.registerCharacterJoiner(handler);
166 }
167 public deregisterCharacterJoiner(joinerId: number): void {
168 this._checkProposedApi();
169 this._core.deregisterCharacterJoiner(joinerId);
170 }
171 public registerMarker(cursorYOffset: number = 0): IMarker {
172 this._verifyIntegers(cursorYOffset);
173 return this._core.registerMarker(cursorYOffset);
174 }
175 public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined {
176 this._checkProposedApi();
177 this._verifyPositiveIntegers(decorationOptions.x ?? 0, decorationOptions.width ?? 0, decorationOptions.height ?? 0);
178 return this._core.registerDecoration(decorationOptions);
179 }
180 public hasSelection(): boolean {
181 return this._core.hasSelection();
182 }
183 public select(column: number, row: number, length: number): void {
184 this._verifyIntegers(column, row, length);
185 this._core.select(column, row, length);
186 }
187 public getSelection(): string {
188 return this._core.getSelection();
189 }
190 public getSelectionPosition(): IBufferRange | undefined {
191 return this._core.getSelectionPosition();
192 }
193 public clearSelection(): void {
194 this._core.clearSelection();
195 }
196 public selectAll(): void {
197 this._core.selectAll();
198 }
199 public selectLines(start: number, end: number): void {
200 this._verifyIntegers(start, end);
201 this._core.selectLines(start, end);
202 }
203 public dispose(): void {
204 super.dispose();
205 }
206 public scrollLines(amount: number): void {
207 this._verifyIntegers(amount);
208 this._core.scrollLines(amount);
209 }
210 public scrollPages(pageCount: number): void {
211 this._verifyIntegers(pageCount);
212 this._core.scrollPages(pageCount);
213 }
214 public scrollToTop(): void {
215 this._core.scrollToTop();
216 }
217 public scrollToBottom(): void {
218 this._core.scrollToBottom();
219 }
220 public scrollToLine(line: number): void {
221 this._verifyIntegers(line);
222 this._core.scrollToLine(line);
223 }
224 public clear(): void {
225 this._core.clear();
226 }
227 public write(data: string | Uint8Array, callback?: () => void): void {
228 this._core.write(data, callback);
229 }
230 public writeln(data: string | Uint8Array, callback?: () => void): void {
231 this._core.write(data);
232 this._core.write('\r\n', callback);
233 }
234 public paste(data: string): void {
235 this._core.paste(data);
236 }
237 public refresh(start: number, end: number): void {
238 this._verifyIntegers(start, end);
239 this._core.refresh(start, end);
240 }
241 public reset(): void {
242 this._core.reset();
243 }
244 public clearTextureAtlas(): void {
245 this._core.clearTextureAtlas();
246 }
247 public loadAddon(addon: ITerminalAddon): void {
248 this._addonManager.loadAddon(this, addon);
249 }
250 public static get strings(): ILocalizableStrings {
251 // A wrapper is required here because esbuild prevents setting an `export let`
252 return {
253 get promptLabel(): string { return Strings.promptLabel.get(); },
254 set promptLabel(value: string) { Strings.promptLabel.set(value); },
255 get tooMuchOutput(): string { return Strings.tooMuchOutput.get(); },
256 set tooMuchOutput(value: string) { Strings.tooMuchOutput.set(value); }
257 };
258 }
259
260 private _verifyIntegers(...values: number[]): void {
261 for ($value of values) {
262 if ($value === Infinity || isNaN($value) || $value % 1 !== 0) {
263 throw new Error('This API only accepts integers');
264 }
265 }
266 }
267
268 private _verifyPositiveIntegers(...values: number[]): void {
269 for ($value of values) {
270 if ($value && ($value === Infinity || isNaN($value) || $value % 1 !== 0 || $value < 0)) {
271 throw new Error('This API only accepts positive integers');
272 }
273 }
274 }
275 }