Merge pull request #3599 from silamon/options5
Add options to headless typings
This commit is contained in:
@@ -5,10 +5,11 @@
|
|||||||
|
|
||||||
import { deepStrictEqual, strictEqual, throws } from 'assert';
|
import { deepStrictEqual, strictEqual, throws } from 'assert';
|
||||||
import { Terminal } from 'headless/public/Terminal';
|
import { Terminal } from 'headless/public/Terminal';
|
||||||
|
import { ITerminalOptions } from 'xterm-headless';
|
||||||
|
|
||||||
let term: Terminal;
|
let term: Terminal;
|
||||||
|
|
||||||
describe('Headless API Tests', function(): void {
|
describe('Headless API Tests', function (): void {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// Create default terminal to be used by most tests
|
// Create default terminal to be used by most tests
|
||||||
term = new Terminal();
|
term = new Terminal();
|
||||||
@@ -119,13 +120,43 @@ describe('Headless API Tests', function(): void {
|
|||||||
strictEqual(term.getOption('scrollback'), 50);
|
strictEqual(term.getOption('scrollback'), 50);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('options', () => {
|
||||||
|
const termOptions = {
|
||||||
|
cols: 80,
|
||||||
|
rows: 24
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
term = new Terminal(termOptions);
|
||||||
|
});
|
||||||
|
it('get options', () => {
|
||||||
|
const options: ITerminalOptions = term.options;
|
||||||
|
strictEqual(options.cols, 80);
|
||||||
|
strictEqual(options.rows, 24);
|
||||||
|
});
|
||||||
|
it('set options', async () => {
|
||||||
|
const options: ITerminalOptions = term.options;
|
||||||
|
throws(() => options.cols = 40);
|
||||||
|
throws(() => options.rows = 20);
|
||||||
|
term.options.scrollback = 1;
|
||||||
|
strictEqual(term.options.scrollback, 1);
|
||||||
|
term.options= {
|
||||||
|
fontSize: 12,
|
||||||
|
fontFamily: 'Arial'
|
||||||
|
};
|
||||||
|
strictEqual(term.options.fontSize, 12);
|
||||||
|
strictEqual(term.options.fontFamily, 'Arial');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('loadAddon', () => {
|
describe('loadAddon', () => {
|
||||||
it('constructor', async () => {
|
it('constructor', async () => {
|
||||||
term = new Terminal({ cols: 5 });
|
term = new Terminal({ cols: 5 });
|
||||||
let cols = 0;
|
let cols = 0;
|
||||||
term.loadAddon({
|
term.loadAddon({
|
||||||
activate: (t) => cols = t.cols,
|
activate: (t) => cols = t.cols,
|
||||||
dispose: () => {}
|
dispose: () => { }
|
||||||
});
|
});
|
||||||
strictEqual(cols, 5);
|
strictEqual(cols, 5);
|
||||||
});
|
});
|
||||||
@@ -133,7 +164,7 @@ describe('Headless API Tests', function(): void {
|
|||||||
it('dispose (addon)', async () => {
|
it('dispose (addon)', async () => {
|
||||||
let disposeCalled = false;
|
let disposeCalled = false;
|
||||||
const addon = {
|
const addon = {
|
||||||
activate: () => {},
|
activate: () => { },
|
||||||
dispose: () => disposeCalled = true
|
dispose: () => disposeCalled = true
|
||||||
};
|
};
|
||||||
term.loadAddon(addon);
|
term.loadAddon(addon);
|
||||||
@@ -145,7 +176,7 @@ describe('Headless API Tests', function(): void {
|
|||||||
it('dispose (terminal)', async () => {
|
it('dispose (terminal)', async () => {
|
||||||
let disposeCalled = false;
|
let disposeCalled = false;
|
||||||
term.loadAddon({
|
term.loadAddon({
|
||||||
activate: () => {},
|
activate: () => { },
|
||||||
dispose: () => disposeCalled = true
|
dispose: () => disposeCalled = true
|
||||||
});
|
});
|
||||||
strictEqual(disposeCalled, false);
|
strictEqual(disposeCalled, false);
|
||||||
|
|||||||
@@ -7,19 +7,61 @@ import { IEvent } from 'common/EventEmitter';
|
|||||||
import { BufferNamespaceApi } from 'common/public/BufferNamespaceApi';
|
import { BufferNamespaceApi } from 'common/public/BufferNamespaceApi';
|
||||||
import { ParserApi } from 'common/public/ParserApi';
|
import { ParserApi } from 'common/public/ParserApi';
|
||||||
import { UnicodeApi } from 'common/public/UnicodeApi';
|
import { UnicodeApi } from 'common/public/UnicodeApi';
|
||||||
import { IBufferNamespace as IBufferNamespaceApi, IMarker, IModes, IParser, ITerminalAddon, ITerminalOptions, IUnicodeHandling, Terminal as ITerminalApi } from 'xterm-headless';
|
import { IBufferNamespace as IBufferNamespaceApi, IMarker, IModes, IParser, ITerminalAddon, IUnicodeHandling, Terminal as ITerminalApi } from 'xterm-headless';
|
||||||
import { Terminal as TerminalCore } from 'headless/Terminal';
|
import { Terminal as TerminalCore } from 'headless/Terminal';
|
||||||
import { AddonManager } from 'common/public/AddonManager';
|
import { AddonManager } from 'common/public/AddonManager';
|
||||||
|
import { ITerminalOptions } from 'common/Types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The set of options that only have an effect when set in the Terminal constructor.
|
||||||
|
*/
|
||||||
|
const CONSTRUCTOR_ONLY_OPTIONS = ['cols', 'rows'];
|
||||||
|
|
||||||
export class Terminal implements ITerminalApi {
|
export class Terminal implements ITerminalApi {
|
||||||
private _core: TerminalCore;
|
private _core: TerminalCore;
|
||||||
private _addonManager: AddonManager;
|
private _addonManager: AddonManager;
|
||||||
private _parser: IParser | undefined;
|
private _parser: IParser | undefined;
|
||||||
private _buffer: BufferNamespaceApi | undefined;
|
private _buffer: BufferNamespaceApi | undefined;
|
||||||
|
private _publicOptions: ITerminalOptions;
|
||||||
|
|
||||||
constructor(options?: ITerminalOptions) {
|
constructor(options?: ITerminalOptions) {
|
||||||
this._core = new TerminalCore(options);
|
this._core = new TerminalCore(options);
|
||||||
this._addonManager = new AddonManager();
|
this._addonManager = new AddonManager();
|
||||||
|
|
||||||
|
this._publicOptions = { ... this._core.options };
|
||||||
|
const getter = (propName: string): any => {
|
||||||
|
return this._core.options[propName];
|
||||||
|
};
|
||||||
|
const setter = (propName: string, value: any): void => {
|
||||||
|
this._checkReadonlyOptions(propName);
|
||||||
|
this._core.options[propName] = value;
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const propName in this._core.options) {
|
||||||
|
Object.defineProperty(this._publicOptions, propName, {
|
||||||
|
get: () => {
|
||||||
|
return this._core.options[propName];
|
||||||
|
},
|
||||||
|
set: (value: any) => {
|
||||||
|
this._checkReadonlyOptions(propName);
|
||||||
|
this._core.options[propName] = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const desc = {
|
||||||
|
get: getter.bind(this, propName),
|
||||||
|
set: setter.bind(this, propName)
|
||||||
|
};
|
||||||
|
Object.defineProperty(this._publicOptions, propName, desc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private _checkReadonlyOptions(propName: string): void {
|
||||||
|
// Throw an error if any constructor only option is modified
|
||||||
|
// from terminal.options
|
||||||
|
// Modifications from anywhere else are allowed
|
||||||
|
if (CONSTRUCTOR_ONLY_OPTIONS.includes(propName)) {
|
||||||
|
throw new Error(`Option "${propName}" can only be set in the constructor`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _checkProposedApi(): void {
|
private _checkProposedApi(): void {
|
||||||
@@ -82,6 +124,14 @@ export class Terminal implements ITerminalApi {
|
|||||||
wraparoundMode: m.wraparound
|
wraparoundMode: m.wraparound
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
public get options(): ITerminalOptions {
|
||||||
|
return this._publicOptions;
|
||||||
|
}
|
||||||
|
public set options(options: ITerminalOptions) {
|
||||||
|
for (const propName in options) {
|
||||||
|
this._publicOptions[propName] = options[propName];
|
||||||
|
}
|
||||||
|
}
|
||||||
public resize(columns: number, rows: number): void {
|
public resize(columns: number, rows: number): void {
|
||||||
this._verifyIntegers(columns, rows);
|
this._verifyIntegers(columns, rows);
|
||||||
this._core.resize(columns, rows);
|
this._core.resize(columns, rows);
|
||||||
|
|||||||
Vendored
+23
@@ -534,6 +534,29 @@ declare module 'xterm-headless' {
|
|||||||
*/
|
*/
|
||||||
readonly modes: IModes;
|
readonly modes: IModes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets or sets the terminal options. This supports setting multiple options.
|
||||||
|
*
|
||||||
|
* @example Get a single option
|
||||||
|
* ```typescript
|
||||||
|
* console.log(terminal.options.fontSize);
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @example Set a single option
|
||||||
|
* ```typescript
|
||||||
|
* terminal.options.fontSize = 12;
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* @example Set multiple options
|
||||||
|
* ```typescript
|
||||||
|
* terminal.options = {
|
||||||
|
* fontSize: 12,
|
||||||
|
* fontFamily: 'Arial',
|
||||||
|
* };
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
options: ITerminalOptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Natural language strings that can be localized.
|
* Natural language strings that can be localized.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user