| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 7 |
import { isMac } from 'common/Platform'; |
| 8 |
import { CursorStyle, IDisposable } from 'common/Types'; |
| 9 |
import { FontWeight, IOptionsService, ITerminalOptions } from 'common/services/Services'; |
| 10 |
import { Emitter } from 'vs/base/common/event'; |
| 11 |
|
| 12 |
export const DEFAULT_OPTIONS: Readonly<Required<ITerminalOptions>> = { |
| 13 |
cols: 80, |
| 14 |
rows: 24, |
| 15 |
cursorBlink: false, |
| 16 |
cursorStyle: 'block', |
| 17 |
cursorWidth: 1, |
| 18 |
cursorInactiveStyle: 'outline', |
| 19 |
customGlyphs: true, |
| 20 |
drawBoldTextInBrightColors: true, |
| 21 |
documentOverride: null, |
| 22 |
fastScrollModifier: 'alt', |
| 23 |
fastScrollSensitivity: 5, |
| 24 |
fontFamily: 'monospace', |
| 25 |
fontSize: 15, |
| 26 |
fontWeight: 'normal', |
| 27 |
fontWeightBold: 'bold', |
| 28 |
ignoreBracketedPasteMode: false, |
| 29 |
lineHeight: 1.0, |
| 30 |
letterSpacing: 0, |
| 31 |
linkHandler: null, |
| 32 |
logLevel: 'info', |
| 33 |
logger: null, |
| 34 |
scrollback: 1000, |
| 35 |
scrollOnEraseInDisplay: false, |
| 36 |
scrollOnUserInput: true, |
| 37 |
scrollSensitivity: 1, |
| 38 |
screenReaderMode: false, |
| 39 |
smoothScrollDuration: 0, |
| 40 |
macOptionIsMeta: false, |
| 41 |
macOptionClickForcesSelection: false, |
| 42 |
minimumContrastRatio: 1, |
| 43 |
disableStdin: false, |
| 44 |
allowProposedApi: false, |
| 45 |
allowTransparency: false, |
| 46 |
tabStopWidth: 8, |
| 47 |
theme: {}, |
| 48 |
reflowCursorLine: false, |
| 49 |
rescaleOverlappingGlyphs: false, |
| 50 |
rightClickSelectsWord: isMac, |
| 51 |
windowOptions: {}, |
| 52 |
windowsMode: false, |
| 53 |
windowsPty: {}, |
| 54 |
wordSeparator: ' ()[]{}\',"`', |
| 55 |
altClickMovesCursor: true, |
| 56 |
convertEol: false, |
| 57 |
termName: 'xterm', |
| 58 |
cancelEvents: false, |
| 59 |
overviewRuler: {} |
| 60 |
}; |
| 61 |
|
| 62 |
const FONT_WEIGHT_OPTIONS: Extract<FontWeight, string>[] = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']; |
| 63 |
|
| 64 |
export class OptionsService extends Disposable implements IOptionsService { |
| 65 |
public serviceBrand: any; |
| 66 |
|
| 67 |
public readonly rawOptions: Required<ITerminalOptions>; |
| 68 |
public options: Required<ITerminalOptions>; |
| 69 |
|
| 70 |
private readonly _onOptionChange = this._register(new Emitter<keyof ITerminalOptions>()); |
| 71 |
public readonly onOptionChange = this._onOptionChange.event; |
| 72 |
|
| 73 |
constructor(options: Partial<ITerminalOptions>) { |
| 74 |
super(); |
| 75 |
// set the default value of each option |
| 76 |
const defaultOptions = { ...DEFAULT_OPTIONS }; |
| 77 |
for (const key in options) { |
| 78 |
if (key in defaultOptions) { |
| 79 |
try { |
| 80 |
const newValue = options[key]; |
| 81 |
defaultOptions[key] = this._sanitizeAndValidateOption(key, newValue); |
| 82 |
} catch (e) { |
| 83 |
console.error(e); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// set up getters and setters for each option |
| 89 |
this.rawOptions = defaultOptions; |
| 90 |
this.options = { ... defaultOptions }; |
| 91 |
this._setupOptions(); |
| 92 |
|
| 93 |
// Clear out options that could link outside xterm.js as they could easily cause an embedder |
| 94 |
// memory leak |
| 95 |
this._register(toDisposable(() => { |
| 96 |
this.rawOptions.linkHandler = null; |
| 97 |
this.rawOptions.documentOverride = null; |
| 98 |
})); |
| 99 |
} |
| 100 |
|
| 101 |
// eslint-disable-next-line @typescript-eslint/naming-convention |
| 102 |
public onSpecificOptionChange<T extends keyof ITerminalOptions>(key: T, listener: (value: ITerminalOptions[T]) => any): IDisposable { |
| 103 |
return this.onOptionChange(eventKey => { |
| 104 |
if (eventKey === key) { |
| 105 |
listener(this.rawOptions[key]); |
| 106 |
} |
| 107 |
}); |
| 108 |
} |
| 109 |
|
| 110 |
// eslint-disable-next-line @typescript-eslint/naming-convention |
| 111 |
public onMultipleOptionChange(keys: (keyof ITerminalOptions)[], listener: () => any): IDisposable { |
| 112 |
return this.onOptionChange(eventKey => { |
| 113 |
if (keys.indexOf(eventKey) !== -1) { |
| 114 |
listener(); |
| 115 |
} |
| 116 |
}); |
| 117 |
} |
| 118 |
|
| 119 |
private _setupOptions(): void { |
| 120 |
const getter = (propName: string): any => { |
| 121 |
if (!(propName in DEFAULT_OPTIONS)) { |
| 122 |
throw new Error(`No option with key "${propName}"`); |
| 123 |
} |
| 124 |
return this.rawOptions[propName]; |
| 125 |
}; |
| 126 |
|
| 127 |
const setter = (propName: string, value: any): void => { |
| 128 |
if (!(propName in DEFAULT_OPTIONS)) { |
| 129 |
throw new Error(`No option with key "${propName}"`); |
| 130 |
} |
| 131 |
|
| 132 |
value = this._sanitizeAndValidateOption(propName, value); |
| 133 |
// Don't fire an option change event if they didn't change |
| 134 |
if (this.rawOptions[propName] !== value) { |
| 135 |
this.rawOptions[propName] = value; |
| 136 |
this._onOptionChange.fire(propName); |
| 137 |
} |
| 138 |
}; |
| 139 |
|
| 140 |
for (const propName in this.rawOptions) { |
| 141 |
const desc = { |
| 142 |
get: getter.bind(this, propName), |
| 143 |
set: setter.bind(this, propName) |
| 144 |
}; |
| 145 |
Object.defineProperty(this.options, propName, desc); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
private _sanitizeAndValidateOption(key: string, value: any): any { |
| 150 |
switch (key) { |
| 151 |
case 'cursorStyle': |
| 152 |
if (!value) { |
| 153 |
value = DEFAULT_OPTIONS[key]; |
| 154 |
} |
| 155 |
if (!isCursorStyle(value)) { |
| 156 |
throw new Error(`"${value}" is not a valid value for ${key}`); |
| 157 |
} |
| 158 |
break; |
| 159 |
case 'wordSeparator': |
| 160 |
if (!value) { |
| 161 |
value = DEFAULT_OPTIONS[key]; |
| 162 |
} |
| 163 |
break; |
| 164 |
case 'fontWeight': |
| 165 |
case 'fontWeightBold': |
| 166 |
if (typeof value === 'number' && 1 <= value && value <= 1000) { |
| 167 |
// already valid numeric value |
| 168 |
break; |
| 169 |
} |
| 170 |
value = FONT_WEIGHT_OPTIONS.includes(value) ? value : DEFAULT_OPTIONS[key]; |
| 171 |
break; |
| 172 |
case 'cursorWidth': |
| 173 |
value = Math.floor(value); |
| 174 |
// Fall through for bounds check |
| 175 |
case 'lineHeight': |
| 176 |
case 'tabStopWidth': |
| 177 |
if (value < 1) { |
| 178 |
throw new Error(`${key} cannot be less than 1, value: ${value}`); |
| 179 |
} |
| 180 |
break; |
| 181 |
case 'minimumContrastRatio': |
| 182 |
value = Math.max(1, Math.min(21, Math.round(value * 10) / 10)); |
| 183 |
break; |
| 184 |
case 'scrollback': |
| 185 |
value = Math.min(value, 4294967295); |
| 186 |
if (value < 0) { |
| 187 |
throw new Error(`${key} cannot be less than 0, value: ${value}`); |
| 188 |
} |
| 189 |
break; |
| 190 |
case 'fastScrollSensitivity': |
| 191 |
case 'scrollSensitivity': |
| 192 |
if (value <= 0) { |
| 193 |
throw new Error(`${key} cannot be less than or equal to 0, value: ${value}`); |
| 194 |
} |
| 195 |
break; |
| 196 |
case 'rows': |
| 197 |
case 'cols': |
| 198 |
if (!value && value !== 0) { |
| 199 |
throw new Error(`${key} must be numeric, value: ${value}`); |
| 200 |
} |
| 201 |
break; |
| 202 |
case 'windowsPty': |
| 203 |
value = value ?? {}; |
| 204 |
break; |
| 205 |
} |
| 206 |
return value; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
function isCursorStyle(value: unknown): value is CursorStyle { |
| 211 |
return value === 'block' || value === 'underline' || value === 'bar'; |
| 212 |
} |