xterm
[VSoRC/.git] / node_modules / xterm / src / public / Terminal.ts
1 /**
2  * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings, ITerminalAddon, ISelectionPosition, IBuffer as IBufferApi, IBufferLine as IBufferLineApi, IBufferCell as IBufferCellApi, IParser, IFunctionIdentifier } from 'xterm';
7 import { ITerminal } from '../Types';
8 import { IBufferLine } from 'common/Types';
9 import { IBuffer } from 'common/buffer/Types';
10 import { Terminal as TerminalCore } from '../Terminal';
11 import * as Strings from '../browser/LocalizableStrings';
12 import { IEvent } from 'common/EventEmitter';
13 import { AddonManager } from './AddonManager';
14 import { IParams } from 'common/parser/Types';
15
16 export class Terminal implements ITerminalApi {
17   private _core: ITerminal;
18   private _addonManager: AddonManager;
19   private _parser: IParser;
20
21   constructor(options?: ITerminalOptions) {
22     this._core = new TerminalCore(options);
23     this._addonManager = new AddonManager();
24   }
25
26   public get onCursorMove(): IEvent<void> { return this._core.onCursorMove; }
27   public get onLineFeed(): IEvent<void> { return this._core.onLineFeed; }
28   public get onSelectionChange(): IEvent<void> { return this._core.onSelectionChange; }
29   public get onData(): IEvent<string> { return this._core.onData; }
30   public get onTitleChange(): IEvent<string> { return this._core.onTitleChange; }
31   public get onScroll(): IEvent<number> { return this._core.onScroll; }
32   public get onKey(): IEvent<{ key: string, domEvent: KeyboardEvent }> { return this._core.onKey; }
33   public get onRender(): IEvent<{ start: number, end: number }> { return this._core.onRender; }
34   public get onResize(): IEvent<{ cols: number, rows: number }> { return this._core.onResize; }
35
36   public get element(): HTMLElement | undefined { return this._core.element; }
37   public get parser(): IParser {
38     if (!this._parser) {
39       this._parser = new ParserApi(this._core);
40     }
41     return this._parser;
42   }
43   public get textarea(): HTMLTextAreaElement | undefined { return this._core.textarea; }
44   public get rows(): number { return this._core.rows; }
45   public get cols(): number { return this._core.cols; }
46   public get buffer(): IBufferApi { return new BufferApiView(this._core.buffer); }
47   public get markers(): ReadonlyArray<IMarker> { return this._core.markers; }
48   public blur(): void {
49     this._core.blur();
50   }
51   public focus(): void {
52     this._core.focus();
53   }
54   public resize(columns: number, rows: number): void {
55     this._verifyIntegers(columns, rows);
56     this._core.resize(columns, rows);
57   }
58   public open(parent: HTMLElement): void {
59     this._core.open(parent);
60   }
61   public attachCustomKeyEventHandler(customKeyEventHandler: (event: KeyboardEvent) => boolean): void {
62     this._core.attachCustomKeyEventHandler(customKeyEventHandler);
63   }
64   public registerLinkMatcher(regex: RegExp, handler: (event: MouseEvent, uri: string) => void, options?: ILinkMatcherOptions): number {
65     return this._core.registerLinkMatcher(regex, handler, options);
66   }
67   public deregisterLinkMatcher(matcherId: number): void {
68     this._core.deregisterLinkMatcher(matcherId);
69   }
70   public registerCharacterJoiner(handler: (text: string) => [number, number][]): number {
71     return this._core.registerCharacterJoiner(handler);
72   }
73   public deregisterCharacterJoiner(joinerId: number): void {
74     this._core.deregisterCharacterJoiner(joinerId);
75   }
76   public addMarker(cursorYOffset: number): IMarker {
77     this._verifyIntegers(cursorYOffset);
78     return this._core.addMarker(cursorYOffset);
79   }
80   public hasSelection(): boolean {
81     return this._core.hasSelection();
82   }
83   public select(column: number, row: number, length: number): void {
84     this._verifyIntegers(column, row, length);
85     this._core.select(column, row, length);
86   }
87   public getSelection(): string {
88     return this._core.getSelection();
89   }
90   public getSelectionPosition(): ISelectionPosition | undefined {
91     return this._core.getSelectionPosition();
92   }
93   public clearSelection(): void {
94     this._core.clearSelection();
95   }
96   public selectAll(): void {
97     this._core.selectAll();
98   }
99   public selectLines(start: number, end: number): void {
100     this._verifyIntegers(start, end);
101     this._core.selectLines(start, end);
102   }
103   public dispose(): void {
104     this._addonManager.dispose();
105     this._core.dispose();
106   }
107   public scrollLines(amount: number): void {
108     this._verifyIntegers(amount);
109     this._core.scrollLines(amount);
110   }
111   public scrollPages(pageCount: number): void {
112     this._verifyIntegers(pageCount);
113     this._core.scrollPages(pageCount);
114   }
115   public scrollToTop(): void {
116     this._core.scrollToTop();
117   }
118   public scrollToBottom(): void {
119     this._core.scrollToBottom();
120   }
121   public scrollToLine(line: number): void {
122     this._verifyIntegers(line);
123     this._core.scrollToLine(line);
124   }
125   public clear(): void {
126     this._core.clear();
127   }
128   public write(data: string | Uint8Array, callback?: () => void): void {
129     this._core.write(data, callback);
130   }
131   public writeUtf8(data: Uint8Array, callback?: () => void): void {
132     this._core.write(data, callback);
133   }
134   public writeln(data: string | Uint8Array, callback?: () => void): void {
135     this._core.write(data);
136     this._core.write('\r\n', callback);
137   }
138   public paste(data: string): void {
139     this._core.paste(data);
140   }
141   public getOption(key: 'bellSound' | 'bellStyle' | 'cursorStyle' | 'fontFamily' | 'fontWeight' | 'fontWeightBold' | 'logLevel' | 'rendererType' | 'termName' | 'wordSeparator'): string;
142   public getOption(key: 'allowTransparency' | 'cancelEvents' | 'convertEol' | 'cursorBlink' | 'disableStdin' | 'macOptionIsMeta' | 'rightClickSelectsWord' | 'popOnBell' | 'screenKeys' | 'useFlowControl' | 'visualBell'): boolean;
143   public getOption(key: 'colors'): string[];
144   public getOption(key: 'cols' | 'fontSize' | 'letterSpacing' | 'lineHeight' | 'rows' | 'tabStopWidth' | 'scrollback'): number;
145   public getOption(key: 'handler'): (data: string) => void;
146   public getOption(key: string): any;
147   public getOption(key: any): any {
148     return this._core.optionsService.getOption(key);
149   }
150   public setOption(key: 'bellSound' | 'fontFamily' | 'termName' | 'wordSeparator', value: string): void;
151   public setOption(key: 'fontWeight' | 'fontWeightBold', value: 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'): void;
152   public setOption(key: 'logLevel', value: 'debug' | 'info' | 'warn' | 'error' | 'off'): void;
153   public setOption(key: 'bellStyle', value: 'none' | 'visual' | 'sound' | 'both'): void;
154   public setOption(key: 'cursorStyle', value: 'block' | 'underline' | 'bar'): void;
155   public setOption(key: 'allowTransparency' | 'cancelEvents' | 'convertEol' | 'cursorBlink' | 'disableStdin' | 'macOptionIsMeta' | 'rightClickSelectsWord' | 'popOnBell' | 'screenKeys' | 'useFlowControl' | 'visualBell', value: boolean): void;
156   public setOption(key: 'colors', value: string[]): void;
157   public setOption(key: 'fontSize' | 'letterSpacing' | 'lineHeight' | 'tabStopWidth' | 'scrollback', value: number): void;
158   public setOption(key: 'handler', value: (data: string) => void): void;
159   public setOption(key: 'theme', value: ITheme): void;
160   public setOption(key: 'cols' | 'rows', value: number): void;
161   public setOption(key: string, value: any): void;
162   public setOption(key: any, value: any): void {
163     this._core.optionsService.setOption(key, value);
164   }
165   public refresh(start: number, end: number): void {
166     this._verifyIntegers(start, end);
167     this._core.refresh(start, end);
168   }
169   public reset(): void {
170     this._core.reset();
171   }
172   public loadAddon(addon: ITerminalAddon): void {
173     return this._addonManager.loadAddon(this, addon);
174   }
175   public static get strings(): ILocalizableStrings {
176     return Strings;
177   }
178
179   private _verifyIntegers(...values: number[]): void {
180     values.forEach(value => {
181       if (value === Infinity || isNaN(value) || value % 1 !== 0) {
182         throw new Error('This API only accepts integers');
183       }
184     });
185   }
186 }
187
188 class BufferApiView implements IBufferApi {
189   constructor(private _buffer: IBuffer) {}
190
191   public get cursorY(): number { return this._buffer.y; }
192   public get cursorX(): number { return this._buffer.x; }
193   public get viewportY(): number { return this._buffer.ydisp; }
194   public get baseY(): number { return this._buffer.ybase; }
195   public get length(): number { return this._buffer.lines.length; }
196   public getLine(y: number): IBufferLineApi | undefined {
197     const line = this._buffer.lines.get(y);
198     if (!line) {
199       return undefined;
200     }
201     return new BufferLineApiView(line);
202   }
203 }
204
205 class BufferLineApiView implements IBufferLineApi {
206   constructor(private _line: IBufferLine) {}
207
208   public get isWrapped(): boolean { return this._line.isWrapped; }
209   public getCell(x: number): IBufferCellApi | undefined {
210     if (x < 0 || x >= this._line.length) {
211       return undefined;
212     }
213     return new BufferCellApiView(this._line, x);
214   }
215   public translateToString(trimRight?: boolean, startColumn?: number, endColumn?: number): string {
216     return this._line.translateToString(trimRight, startColumn, endColumn);
217   }
218 }
219
220 class BufferCellApiView implements IBufferCellApi {
221   constructor(private _line: IBufferLine, private _x: number) {}
222   public get char(): string { return this._line.getString(this._x); }
223   public get width(): number { return this._line.getWidth(this._x); }
224 }
225
226 class ParserApi implements IParser {
227   constructor(private _core: ITerminal) {}
228
229   public addCsiHandler(id: IFunctionIdentifier, callback: (params: (number | number[])[]) => boolean): IDisposable {
230     return this._core.addCsiHandler(id, (params: IParams) => callback(params.toArray()));
231   }
232   public addDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: (number | number[])[]) => boolean): IDisposable {
233     return this._core.addDcsHandler(id, (data: string, params: IParams) => callback(data, params.toArray()));
234   }
235   public addEscHandler(id: IFunctionIdentifier, handler: () => boolean): IDisposable {
236     return this._core.addEscHandler(id, handler);
237   }
238   public addOscHandler(ident: number, callback: (data: string) => boolean): IDisposable {
239     return this._core.addOscHandler(ident, callback);
240   }
241 }