Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/browser/public/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ export class Terminal implements ITerminalApi {
this._core = new TerminalCore(options);
this._addonManager = new AddonManager();

this._publicOptions = {};
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;
}
});
}
this._publicOptions = new Proxy(this._core.options, {
get: (target, propName: string): any => {
return target[propName];
},
set: (target, propName: string, value): any => {
this._checkReadonlyOptions(propName);
target[propName] = value;
return true;
}
});
}

private _checkReadonlyOptions(propName: string): void {
Expand Down
4 changes: 2 additions & 2 deletions src/common/services/OptionsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('OptionsService', () => {
describe('constructor', () => {
const originalError = console.error;
beforeEach(() => {
console.error = () => {};
console.error = () => { };
});
afterEach(() => {
console.error = originalError;
Expand All @@ -26,7 +26,7 @@ describe('OptionsService', () => {
assert.equal(optionsService.getOption('cols'), 80);
});
it('uses default value if invalid constructor option value passed', () => {
assert.equal(new OptionsService({tabStopWidth: 0}).getOption('tabStopWidth'), DEFAULT_OPTIONS.tabStopWidth);
assert.equal(new OptionsService({ tabStopWidth: 0 }).getOption('tabStopWidth'), DEFAULT_OPTIONS.tabStopWidth);
});
});
describe('setOption', () => {
Expand Down
50 changes: 23 additions & 27 deletions src/common/services/OptionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,55 +60,51 @@ const FONT_WEIGHT_OPTIONS: Extract<FontWeight, string>[] = ['normal', 'bold', '1
export class OptionsService implements IOptionsService {
public serviceBrand: any;

private _options: ITerminalOptions;
public options: ITerminalOptions;

private _onOptionChange = new EventEmitter<string>();
public get onOptionChange(): IEvent<string> { return this._onOptionChange.event; }

constructor(options: Partial<ITerminalOptions>) {
// set the default value of each option
this._options = { ...DEFAULT_OPTIONS };
const defaultOptions = { ...DEFAULT_OPTIONS };
for (const key in options) {
if (key in this._options) {
if (key in defaultOptions) {
try {
const newValue = options[key];
this._options[key] = this._sanitizeAndValidateOption(key, newValue);
defaultOptions[key] = this._sanitizeAndValidateOption(key, newValue);
} catch (e) {
console.error(e);
}
}
}

// set up getters and setters for each option
this.options = this._setupOptions(this._options);
this.options = this._setupOptions(defaultOptions);
}

private _setupOptions(options: ITerminalOptions): ITerminalOptions {
const copiedOptions = { ... options };
for (const propName in copiedOptions) {
Object.defineProperty(copiedOptions, propName, {
get: () => {
if (!(propName in DEFAULT_OPTIONS)) {
throw new Error(`No option with key "${propName}"`);
}
return this._options[propName];
},
set: (value: any) => {
if (!(propName in DEFAULT_OPTIONS)) {
throw new Error(`No option with key "${propName}"`);
}
return new Proxy(options, {
get: (target, propName: string): any => {
if (!(propName in DEFAULT_OPTIONS)) {
throw new Error(`No option with key "${propName}"`);
}
return target[propName];
},
set: (target, propName: string, value): any => {
if (!(propName in DEFAULT_OPTIONS)) {
throw new Error(`No option with key "${propName}"`);
}

value = this._sanitizeAndValidateOption(propName, value);
// Don't fire an option change event if they didn't change
if (this._options[propName] !== value) {
this._options[propName] = value;
this._onOptionChange.fire(propName);
}
value = this._sanitizeAndValidateOption(propName, value);
// Don't fire an option change event if they didn't change
if (target[propName] !== value) {
target[propName] = value;
this._onOptionChange.fire(propName);
}
});
}
return copiedOptions;
return true;
}
});
}

public setOption(key: string, value: any): void {
Expand Down