Skip to content

Commit 522c8bf

Browse files
authored
Merge pull request #3214 from joyceerhl/api-view-classes
Move API view classes under common/public
2 parents 5a4b305 + a14bb3e commit 522c8bf

File tree

8 files changed

+172
-128
lines changed

8 files changed

+172
-128
lines changed

src/browser/Types.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface ITerminal extends IPublicTerminal, ICoreTerminal {
1515
screenElement: HTMLElement | undefined;
1616
browser: IBrowser;
1717
buffer: IBuffer;
18-
buffers: IBufferSet;
1918
viewport: IViewport | undefined;
2019
// TODO: We should remove options once components adopt optionsService
2120
options: ITerminalOptions;

src/browser/public/Terminal.ts

Lines changed: 5 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
* @license MIT
44
*/
55

6-
import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings, ITerminalAddon, ISelectionPosition, IBuffer as IBufferApi, IBufferNamespace as IBufferNamespaceApi, IBufferLine as IBufferLineApi, IBufferCell as IBufferCellApi, IParser, IFunctionIdentifier, ILinkProvider, IUnicodeHandling, IUnicodeVersionProvider, FontWeight } from 'xterm';
6+
import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings, ITerminalAddon, ISelectionPosition, IBufferNamespace as IBufferNamespaceApi, IParser, ILinkProvider, IUnicodeHandling, FontWeight } from 'xterm';
77
import { ITerminal } from 'browser/Types';
8-
import { IBufferLine, ICellData } from 'common/Types';
9-
import { IBuffer, IBufferSet } from 'common/buffer/Types';
10-
import { CellData } from 'common/buffer/CellData';
118
import { Terminal as TerminalCore } from '../Terminal';
129
import * as Strings from '../LocalizableStrings';
13-
import { IEvent, EventEmitter } from 'common/EventEmitter';
10+
import { IEvent } from 'common/EventEmitter';
11+
import { ParserApi } from 'common/public/ParserApi';
12+
import { UnicodeApi } from 'common/public/UnicodeApi';
1413
import { AddonManager } from './AddonManager';
15-
import { IParams } from 'common/parser/Types';
16-
import { BufferSet } from 'common/buffer/BufferSet';
14+
import { BufferNamespaceApi } from 'common/public/BufferNamespaceApi';
1715

1816
export class Terminal implements ITerminalApi {
1917
private _core: ITerminal;
@@ -218,123 +216,3 @@ export class Terminal implements ITerminalApi {
218216
}
219217
}
220218
}
221-
222-
class BufferApiView implements IBufferApi {
223-
constructor(
224-
private _buffer: IBuffer,
225-
public readonly type: 'normal' | 'alternate'
226-
) { }
227-
228-
public init(buffer: IBuffer): BufferApiView {
229-
this._buffer = buffer;
230-
return this;
231-
}
232-
233-
public get cursorY(): number { return this._buffer.y; }
234-
public get cursorX(): number { return this._buffer.x; }
235-
public get viewportY(): number { return this._buffer.ydisp; }
236-
public get baseY(): number { return this._buffer.ybase; }
237-
public get length(): number { return this._buffer.lines.length; }
238-
public getLine(y: number): IBufferLineApi | undefined {
239-
const line = this._buffer.lines.get(y);
240-
if (!line) {
241-
return undefined;
242-
}
243-
return new BufferLineApiView(line);
244-
}
245-
public getNullCell(): IBufferCellApi { return new CellData(); }
246-
}
247-
248-
class BufferNamespaceApi implements IBufferNamespaceApi {
249-
private _normal: BufferApiView;
250-
private _alternate: BufferApiView;
251-
private _onBufferChange = new EventEmitter<IBufferApi>();
252-
public get onBufferChange(): IEvent<IBufferApi> { return this._onBufferChange.event; }
253-
254-
constructor(private _core: ITerminal) {
255-
this._normal = new BufferApiView(this._core.buffers.normal, 'normal');
256-
this._alternate = new BufferApiView(this._core.buffers.alt, 'alternate');
257-
this._core.buffers.onBufferActivate(() => this._onBufferChange.fire(this.active));
258-
}
259-
public get active(): IBufferApi {
260-
if (this._core.buffers.active === this._core.buffers.normal) { return this.normal; }
261-
if (this._core.buffers.active === this._core.buffers.alt) { return this.alternate; }
262-
throw new Error('Active buffer is neither normal nor alternate');
263-
}
264-
public get normal(): IBufferApi {
265-
return this._normal.init(this._core.buffers.normal);
266-
}
267-
public get alternate(): IBufferApi {
268-
return this._alternate.init(this._core.buffers.alt);
269-
}
270-
}
271-
272-
class BufferLineApiView implements IBufferLineApi {
273-
constructor(private _line: IBufferLine) { }
274-
275-
public get isWrapped(): boolean { return this._line.isWrapped; }
276-
public get length(): number { return this._line.length; }
277-
public getCell(x: number, cell?: IBufferCellApi): IBufferCellApi | undefined {
278-
if (x < 0 || x >= this._line.length) {
279-
return undefined;
280-
}
281-
282-
if (cell) {
283-
this._line.loadCell(x, cell as ICellData);
284-
return cell;
285-
}
286-
return this._line.loadCell(x, new CellData());
287-
}
288-
public translateToString(trimRight?: boolean, startColumn?: number, endColumn?: number): string {
289-
return this._line.translateToString(trimRight, startColumn, endColumn);
290-
}
291-
}
292-
293-
class ParserApi implements IParser {
294-
constructor(private _core: ITerminal) { }
295-
296-
public registerCsiHandler(id: IFunctionIdentifier, callback: (params: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
297-
return this._core.registerCsiHandler(id, (params: IParams) => callback(params.toArray()));
298-
}
299-
public addCsiHandler(id: IFunctionIdentifier, callback: (params: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
300-
return this.registerCsiHandler(id, callback);
301-
}
302-
public registerDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
303-
return this._core.registerDcsHandler(id, (data: string, params: IParams) => callback(data, params.toArray()));
304-
}
305-
public addDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
306-
return this.registerDcsHandler(id, callback);
307-
}
308-
public registerEscHandler(id: IFunctionIdentifier, handler: () => boolean | Promise<boolean>): IDisposable {
309-
return this._core.registerEscHandler(id, handler);
310-
}
311-
public addEscHandler(id: IFunctionIdentifier, handler: () => boolean | Promise<boolean>): IDisposable {
312-
return this.registerEscHandler(id, handler);
313-
}
314-
public registerOscHandler(ident: number, callback: (data: string) => boolean | Promise<boolean>): IDisposable {
315-
return this._core.registerOscHandler(ident, callback);
316-
}
317-
public addOscHandler(ident: number, callback: (data: string) => boolean | Promise<boolean>): IDisposable {
318-
return this.registerOscHandler(ident, callback);
319-
}
320-
}
321-
322-
class UnicodeApi implements IUnicodeHandling {
323-
constructor(private _core: ITerminal) { }
324-
325-
public register(provider: IUnicodeVersionProvider): void {
326-
this._core.unicodeService.register(provider);
327-
}
328-
329-
public get versions(): string[] {
330-
return this._core.unicodeService.versions;
331-
}
332-
333-
public get activeVersion(): string {
334-
return this._core.unicodeService.activeVersion;
335-
}
336-
337-
public set activeVersion(version: string) {
338-
this._core.unicodeService.activeVersion = version;
339-
}
340-
}

src/common/Types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import { IEvent, IEventEmitter } from 'common/EventEmitter';
88
import { IDeleteEvent, IInsertEvent } from 'common/CircularList';
99
import { IParams } from 'common/parser/Types';
1010
import { IOptionsService, IUnicodeService } from 'common/services/Services';
11+
import { IBufferSet } from 'common/buffer/Types';
1112

1213
export interface ICoreTerminal {
1314
optionsService: IOptionsService;
1415
unicodeService: IUnicodeService;
16+
buffers: IBufferSet;
17+
registerCsiHandler(id: IFunctionIdentifier, callback: (params: IParams) => boolean | Promise<boolean>): IDisposable;
18+
registerDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: IParams) => boolean | Promise<boolean>): IDisposable;
19+
registerEscHandler(id: IFunctionIdentifier, callback: () => boolean | Promise<boolean>): IDisposable;
20+
registerOscHandler(ident: number, callback: (data: string) => boolean | Promise<boolean>): IDisposable;
1521
}
1622

1723
export interface IDisposable {

src/common/public/BufferApiView.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) 2021 The xterm.js authors. All rights reserved.
3+
* @license MIT
4+
*/
5+
6+
import { IBuffer as IBufferApi, IBufferLine as IBufferLineApi, IBufferCell as IBufferCellApi } from 'xterm';
7+
import { IBuffer } from 'common/buffer/Types';
8+
import { BufferLineApiView } from 'common/public/BufferLineApiView';
9+
import { CellData } from 'common/buffer/CellData';
10+
11+
export class BufferApiView implements IBufferApi {
12+
constructor(
13+
private _buffer: IBuffer,
14+
public readonly type: 'normal' | 'alternate'
15+
) { }
16+
17+
public init(buffer: IBuffer): BufferApiView {
18+
this._buffer = buffer;
19+
return this;
20+
}
21+
22+
public get cursorY(): number { return this._buffer.y; }
23+
public get cursorX(): number { return this._buffer.x; }
24+
public get viewportY(): number { return this._buffer.ydisp; }
25+
public get baseY(): number { return this._buffer.ybase; }
26+
public get length(): number { return this._buffer.lines.length; }
27+
public getLine(y: number): IBufferLineApi | undefined {
28+
const line = this._buffer.lines.get(y);
29+
if (!line) {
30+
return undefined;
31+
}
32+
return new BufferLineApiView(line);
33+
}
34+
public getNullCell(): IBufferCellApi { return new CellData(); }
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2021 The xterm.js authors. All rights reserved.
3+
* @license MIT
4+
*/
5+
6+
import { CellData } from 'common/buffer/CellData';
7+
import { IBufferLine, ICellData } from 'common/Types';
8+
import { IBufferCell as IBufferCellApi, IBufferLine as IBufferLineApi } from 'xterm';
9+
10+
export class BufferLineApiView implements IBufferLineApi {
11+
constructor(private _line: IBufferLine) { }
12+
13+
public get isWrapped(): boolean { return this._line.isWrapped; }
14+
public get length(): number { return this._line.length; }
15+
public getCell(x: number, cell?: IBufferCellApi): IBufferCellApi | undefined {
16+
if (x < 0 || x >= this._line.length) {
17+
return undefined;
18+
}
19+
20+
if (cell) {
21+
this._line.loadCell(x, cell as ICellData);
22+
return cell;
23+
}
24+
return this._line.loadCell(x, new CellData());
25+
}
26+
public translateToString(trimRight?: boolean, startColumn?: number, endColumn?: number): string {
27+
return this._line.translateToString(trimRight, startColumn, endColumn);
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2021 The xterm.js authors. All rights reserved.
3+
* @license MIT
4+
*/
5+
6+
import { IBuffer as IBufferApi, IBufferNamespace as IBufferNamespaceApi } from 'xterm';
7+
import { BufferApiView } from 'common/public/BufferApiView';
8+
import { IEvent, EventEmitter } from 'common/EventEmitter';
9+
import { ICoreTerminal } from 'common/Types';
10+
11+
export class BufferNamespaceApi implements IBufferNamespaceApi {
12+
private _normal: BufferApiView;
13+
private _alternate: BufferApiView;
14+
private _onBufferChange = new EventEmitter<IBufferApi>();
15+
public get onBufferChange(): IEvent<IBufferApi> { return this._onBufferChange.event; }
16+
17+
constructor(private _core: ICoreTerminal) {
18+
this._normal = new BufferApiView(this._core.buffers.normal, 'normal');
19+
this._alternate = new BufferApiView(this._core.buffers.alt, 'alternate');
20+
this._core.buffers.onBufferActivate(() => this._onBufferChange.fire(this.active));
21+
}
22+
public get active(): IBufferApi {
23+
if (this._core.buffers.active === this._core.buffers.normal) { return this.normal; }
24+
if (this._core.buffers.active === this._core.buffers.alt) { return this.alternate; }
25+
throw new Error('Active buffer is neither normal nor alternate');
26+
}
27+
public get normal(): IBufferApi {
28+
return this._normal.init(this._core.buffers.normal);
29+
}
30+
public get alternate(): IBufferApi {
31+
return this._alternate.init(this._core.buffers.alt);
32+
}
33+
}

src/common/public/ParserApi.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2021 The xterm.js authors. All rights reserved.
3+
* @license MIT
4+
*/
5+
6+
import { IParams } from 'common/parser/Types';
7+
import { IDisposable, IFunctionIdentifier, IParser } from 'xterm';
8+
import { ICoreTerminal } from 'common/Types';
9+
10+
export class ParserApi implements IParser {
11+
constructor(private _core: ICoreTerminal) { }
12+
13+
public registerCsiHandler(id: IFunctionIdentifier, callback: (params: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
14+
return this._core.registerCsiHandler(id, (params: IParams) => callback(params.toArray()));
15+
}
16+
public addCsiHandler(id: IFunctionIdentifier, callback: (params: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
17+
return this.registerCsiHandler(id, callback);
18+
}
19+
public registerDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
20+
return this._core.registerDcsHandler(id, (data: string, params: IParams) => callback(data, params.toArray()));
21+
}
22+
public addDcsHandler(id: IFunctionIdentifier, callback: (data: string, param: (number | number[])[]) => boolean | Promise<boolean>): IDisposable {
23+
return this.registerDcsHandler(id, callback);
24+
}
25+
public registerEscHandler(id: IFunctionIdentifier, handler: () => boolean | Promise<boolean>): IDisposable {
26+
return this._core.registerEscHandler(id, handler);
27+
}
28+
public addEscHandler(id: IFunctionIdentifier, handler: () => boolean | Promise<boolean>): IDisposable {
29+
return this.registerEscHandler(id, handler);
30+
}
31+
public registerOscHandler(ident: number, callback: (data: string) => boolean | Promise<boolean>): IDisposable {
32+
return this._core.registerOscHandler(ident, callback);
33+
}
34+
public addOscHandler(ident: number, callback: (data: string) => boolean | Promise<boolean>): IDisposable {
35+
return this.registerOscHandler(ident, callback);
36+
}
37+
}

src/common/public/UnicodeApi.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) 2021 The xterm.js authors. All rights reserved.
3+
* @license MIT
4+
*/
5+
6+
import { ICoreTerminal } from 'common/Types';
7+
import { IUnicodeHandling, IUnicodeVersionProvider } from 'xterm';
8+
9+
export class UnicodeApi implements IUnicodeHandling {
10+
constructor(private _core: ICoreTerminal) { }
11+
12+
public register(provider: IUnicodeVersionProvider): void {
13+
this._core.unicodeService.register(provider);
14+
}
15+
16+
public get versions(): string[] {
17+
return this._core.unicodeService.versions;
18+
}
19+
20+
public get activeVersion(): string {
21+
return this._core.unicodeService.activeVersion;
22+
}
23+
24+
public set activeVersion(version: string) {
25+
this._core.unicodeService.activeVersion = version;
26+
}
27+
}

0 commit comments

Comments
 (0)