Skip to content

Commit d960793

Browse files
authored
Merge pull request #3062 from IllusionMH/numeric-fontWeight-3051
Add number type to fontWeight & fontWeightBold options
2 parents 4649ad7 + 4975f38 commit d960793

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

src/browser/public/Terminal.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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 } from 'xterm';
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';
77
import { ITerminal } from 'browser/Types';
88
import { IBufferLine, ICellData } from 'common/Types';
99
import { IBuffer, IBufferSet } from 'common/buffer/Types';
@@ -169,15 +169,16 @@ export class Terminal implements ITerminalApi {
169169
public paste(data: string): void {
170170
this._core.paste(data);
171171
}
172-
public getOption(key: 'bellSound' | 'bellStyle' | 'cursorStyle' | 'fontFamily' | 'fontWeight' | 'fontWeightBold' | 'logLevel' | 'rendererType' | 'termName' | 'wordSeparator'): string;
172+
public getOption(key: 'bellSound' | 'bellStyle' | 'cursorStyle' | 'fontFamily' | 'logLevel' | 'rendererType' | 'termName' | 'wordSeparator'): string;
173173
public getOption(key: 'allowTransparency' | 'cancelEvents' | 'convertEol' | 'cursorBlink' | 'disableStdin' | 'macOptionIsMeta' | 'rightClickSelectsWord' | 'popOnBell' | 'visualBell'): boolean;
174174
public getOption(key: 'cols' | 'fontSize' | 'letterSpacing' | 'lineHeight' | 'rows' | 'tabStopWidth' | 'scrollback'): number;
175+
public getOption(key: 'fontWeight' | 'fontWeightBold'): FontWeight;
175176
public getOption(key: string): any;
176177
public getOption(key: any): any {
177178
return this._core.optionsService.getOption(key);
178179
}
179180
public setOption(key: 'bellSound' | 'fontFamily' | 'termName' | 'wordSeparator', value: string): void;
180-
public setOption(key: 'fontWeight' | 'fontWeightBold', value: 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'): void;
181+
public setOption(key: 'fontWeight' | 'fontWeightBold', value: 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900' | number): void;
181182
public setOption(key: 'logLevel', value: 'debug' | 'info' | 'warn' | 'error' | 'off'): void;
182183
public setOption(key: 'bellStyle', value: 'none' | 'visual' | 'sound' | 'both'): void;
183184
public setOption(key: 'cursorStyle', value: 'block' | 'underline' | 'bar'): void;

src/common/services/OptionsService.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,42 @@ describe('OptionsService', () => {
1919
assert.equal(new OptionsService({tabStopWidth: 0}).getOption('tabStopWidth'), DEFAULT_OPTIONS.tabStopWidth);
2020
});
2121
});
22+
describe('setOption', () => {
23+
let service: OptionsService;
24+
beforeEach(() => {
25+
service = new OptionsService({});
26+
});
27+
it('applies valid fontWeight option values', () => {
28+
service.setOption('fontWeight', 'bold');
29+
assert.equal(service.getOption('fontWeight'), 'bold', '"bold" keyword value should be applied');
30+
31+
service.setOption('fontWeight', 'normal');
32+
assert.equal(service.getOption('fontWeight'), 'normal', '"normal" keyword value should be applied');
33+
34+
service.setOption('fontWeight', '600');
35+
assert.equal(service.getOption('fontWeight'), '600', 'String numeric values should be applied');
36+
37+
service.setOption('fontWeight', 350);
38+
assert.equal(service.getOption('fontWeight'), 350, 'Values between 1 and 1000 should be applied as is');
39+
40+
service.setOption('fontWeight', 1);
41+
assert.equal(service.getOption('fontWeight'), 1, 'Range should include minimum value: 1');
42+
43+
service.setOption('fontWeight', 1000);
44+
assert.equal(service.getOption('fontWeight'), 1000, 'Range should include maximum value: 1000');
45+
});
46+
it('normalizes invalid fontWeight option values', () => {
47+
service.setOption('fontWeight', 350);
48+
assert.doesNotThrow(() => service.setOption('fontWeight', 10000), 'fontWeight should be normalized instead of throwing');
49+
assert.equal(service.getOption('fontWeight'), DEFAULT_OPTIONS.fontWeight, 'Values greater than 1000 should be reset to default');
50+
51+
service.setOption('fontWeight', 350);
52+
service.setOption('fontWeight', -10);
53+
assert.equal(service.getOption('fontWeight'), DEFAULT_OPTIONS.fontWeight, 'Values less than 1 should be reset to default');
54+
55+
service.setOption('fontWeight', 350);
56+
service.setOption('fontWeight', 'bold700');
57+
assert.equal(service.getOption('fontWeight'), DEFAULT_OPTIONS.fontWeight, 'Wrong string literals should be reset to default');
58+
});
59+
});
2260
});

src/common/services/OptionsService.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @license MIT
44
*/
55

6-
import { IOptionsService, ITerminalOptions, IPartialTerminalOptions } from 'common/services/Services';
6+
import { IOptionsService, ITerminalOptions, IPartialTerminalOptions, FontWeight } from 'common/services/Services';
77
import { EventEmitter, IEvent } from 'common/EventEmitter';
88
import { isMac } from 'common/Platform';
99
import { clone } from 'common/Clone';
@@ -56,6 +56,8 @@ export const DEFAULT_OPTIONS: ITerminalOptions = Object.freeze({
5656
cancelEvents: false
5757
});
5858

59+
const FONT_WEIGHT_OPTIONS: Extract<FontWeight, string>[] = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900'];
60+
5961
/**
6062
* The set of options that only have an effect when set in the Terminal constructor.
6163
*/
@@ -109,14 +111,20 @@ export class OptionsService implements IOptionsService {
109111
switch (key) {
110112
case 'bellStyle':
111113
case 'cursorStyle':
112-
case 'fontWeight':
113-
case 'fontWeightBold':
114114
case 'rendererType':
115115
case 'wordSeparator':
116116
if (!value) {
117117
value = DEFAULT_OPTIONS[key];
118118
}
119119
break;
120+
case 'fontWeight':
121+
case 'fontWeightBold':
122+
if (typeof value === 'number' && 1 <= value && value <= 1000) {
123+
// already valid numeric value
124+
break;
125+
}
126+
value = FONT_WEIGHT_OPTIONS.indexOf(value) !== -1 ? value : DEFAULT_OPTIONS[key];
127+
break;
120128
case 'cursorWidth':
121129
value = Math.floor(value);
122130
// Fall through for bounds check

src/common/services/Services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export interface IOptionsService {
179179
getOption<T>(key: string): T | undefined;
180180
}
181181

182-
export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900';
182+
export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900' | number;
183183
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'off';
184184
export type RendererType = 'dom' | 'canvas';
185185

typings/xterm.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
declare module 'xterm' {
1313
/**
14-
* A string representing text font weight.
14+
* A string or number representing text font weight.
1515
*/
16-
export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900';
16+
export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900' | number;
1717

1818
/**
1919
* A string representing log level.
@@ -933,7 +933,7 @@ declare module 'xterm' {
933933
* Retrieves an option's value from the terminal.
934934
* @param key The option key.
935935
*/
936-
getOption(key: 'bellSound' | 'bellStyle' | 'cursorStyle' | 'fontFamily' | 'fontWeight' | 'fontWeightBold' | 'logLevel' | 'rendererType' | 'termName' | 'wordSeparator'): string;
936+
getOption(key: 'bellSound' | 'bellStyle' | 'cursorStyle' | 'fontFamily' | 'logLevel' | 'rendererType' | 'termName' | 'wordSeparator'): string;
937937
/**
938938
* Retrieves an option's value from the terminal.
939939
* @param key The option key.
@@ -944,6 +944,11 @@ declare module 'xterm' {
944944
* @param key The option key.
945945
*/
946946
getOption(key: 'cols' | 'fontSize' | 'letterSpacing' | 'lineHeight' | 'rows' | 'tabStopWidth' | 'scrollback'): number;
947+
/**
948+
* Retrieves an option's value from the terminal.
949+
* @param key The option key.
950+
*/
951+
getOption(key: 'fontWeight' | 'fontWeightBold'): FontWeight;
947952
/**
948953
* Retrieves an option's value from the terminal.
949954
* @param key The option key.
@@ -961,7 +966,7 @@ declare module 'xterm' {
961966
* @param key The option key.
962967
* @param value The option value.
963968
*/
964-
setOption(key: 'fontWeight' | 'fontWeightBold', value: null | 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900'): void;
969+
setOption(key: 'fontWeight' | 'fontWeightBold', value: null | 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900' | number): void;
965970
/**
966971
* Sets an option on the terminal.
967972
* @param key The option key.

0 commit comments

Comments
 (0)