From a1385f589b25b67f86a15485a0f3a71e37c2a6cb Mon Sep 17 00:00:00 2001 From: Simon Lamon Date: Fri, 5 Aug 2022 16:16:12 +0000 Subject: [PATCH 1/2] Required options --- .../src/atlas/CharAtlasCache.ts | 4 +- .../src/atlas/CharAtlasUtils.ts | 4 +- addons/xterm-addon-webgl/src/WebglRenderer.ts | 6 +- .../src/atlas/CharAtlasUtils.ts | 20 +++--- .../src/renderLayer/CursorRenderLayer.ts | 2 +- demo/client.ts | 2 +- src/browser/TestUtils.test.ts | 2 +- src/browser/Types.d.ts | 2 +- src/browser/public/Terminal.ts | 4 +- src/common/CoreTerminal.ts | 2 +- src/common/TestUtils.test.ts | 4 +- src/common/services/OptionsService.ts | 8 +-- src/common/services/Services.ts | 72 +++++++++---------- src/headless/Terminal.ts | 2 +- src/headless/public/Terminal.ts | 4 +- typings/xterm-headless.d.ts | 13 ++-- typings/xterm.d.ts | 15 ++-- 17 files changed, 84 insertions(+), 82 deletions(-) diff --git a/addons/xterm-addon-canvas/src/atlas/CharAtlasCache.ts b/addons/xterm-addon-canvas/src/atlas/CharAtlasCache.ts index 7d020dcc..0c78808e 100644 --- a/addons/xterm-addon-canvas/src/atlas/CharAtlasCache.ts +++ b/addons/xterm-addon-canvas/src/atlas/CharAtlasCache.ts @@ -8,7 +8,7 @@ import { BaseCharAtlas } from './BaseCharAtlas'; import { DynamicCharAtlas } from './DynamicCharAtlas'; import { ICharAtlasConfig } from './Types'; import { IColorSet } from 'browser/Types'; -import { ITerminalOptions } from 'common/services/Services'; +import { ITerminalOptions } from 'xterm'; interface ICharAtlasCacheEntry { atlas: BaseCharAtlas; @@ -25,7 +25,7 @@ const charAtlasCache: ICharAtlasCacheEntry[] = []; * one that is in use by another terminal. */ export function acquireCharAtlas( - options: ITerminalOptions, + options: Required, rendererId: number, colors: IColorSet, scaledCharWidth: number, diff --git a/addons/xterm-addon-canvas/src/atlas/CharAtlasUtils.ts b/addons/xterm-addon-canvas/src/atlas/CharAtlasUtils.ts index 5d44b859..b5674304 100644 --- a/addons/xterm-addon-canvas/src/atlas/CharAtlasUtils.ts +++ b/addons/xterm-addon-canvas/src/atlas/CharAtlasUtils.ts @@ -6,9 +6,9 @@ import { ICharAtlasConfig } from './Types'; import { DEFAULT_COLOR } from 'common/buffer/Constants'; import { IColorSet, IPartialColorSet } from 'browser/Types'; -import { ITerminalOptions } from 'common/services/Services'; +import { ITerminalOptions } from 'xterm'; -export function generateConfig(scaledCharWidth: number, scaledCharHeight: number, options: ITerminalOptions, colors: IColorSet): ICharAtlasConfig { +export function generateConfig(scaledCharWidth: number, scaledCharHeight: number, options: Required, colors: IColorSet): ICharAtlasConfig { // null out some fields that don't matter const clonedColors: IPartialColorSet = { foreground: colors.foreground, diff --git a/addons/xterm-addon-webgl/src/WebglRenderer.ts b/addons/xterm-addon-webgl/src/WebglRenderer.ts index 9e90df02..4848162d 100644 --- a/addons/xterm-addon-webgl/src/WebglRenderer.ts +++ b/addons/xterm-addon-webgl/src/WebglRenderer.ts @@ -552,18 +552,18 @@ export class WebglRenderer extends Disposable implements IRenderer { // Calculate the scaled cell height, if lineHeight is _not_ 1, the resulting value will be // floored since lineHeight can never be lower then 1, this guarentees the scaled cell height // will always be larger than scaled char height. - this.dimensions.scaledCellHeight = Math.floor(this.dimensions.scaledCharHeight * this._terminal.options.lineHeight!); + this.dimensions.scaledCellHeight = Math.floor(this.dimensions.scaledCharHeight * this._terminal.options.lineHeight); // Calculate the y offset within a cell that glyph should draw at in order for it to be centered // correctly within the cell. this.dimensions.scaledCharTop = this._terminal.options.lineHeight === 1 ? 0 : Math.round((this.dimensions.scaledCellHeight - this.dimensions.scaledCharHeight) / 2); // Calculate the scaled cell width, taking the letterSpacing into account. - this.dimensions.scaledCellWidth = this.dimensions.scaledCharWidth + Math.round(this._terminal.options.letterSpacing!); + this.dimensions.scaledCellWidth = this.dimensions.scaledCharWidth + Math.round(this._terminal.options.letterSpacing); // Calculate the x offset with a cell that text should draw from in order for it to be centered // correctly within the cell. - this.dimensions.scaledCharLeft = Math.floor(this._terminal.options.letterSpacing! / 2); + this.dimensions.scaledCharLeft = Math.floor(this._terminal.options.letterSpacing / 2); // Recalculate the canvas dimensions, the scaled dimensions define the actual number of pixel in // the canvas diff --git a/addons/xterm-addon-webgl/src/atlas/CharAtlasUtils.ts b/addons/xterm-addon-webgl/src/atlas/CharAtlasUtils.ts index 86f7e5ab..18de5739 100644 --- a/addons/xterm-addon-webgl/src/atlas/CharAtlasUtils.ts +++ b/addons/xterm-addon-webgl/src/atlas/CharAtlasUtils.ts @@ -32,21 +32,21 @@ export function generateConfig(scaledCellWidth: number, scaledCellHeight: number contrastCache: colors.contrastCache }; return { - customGlyphs: terminal.options.customGlyphs!, + customGlyphs: terminal.options.customGlyphs, devicePixelRatio: window.devicePixelRatio, - letterSpacing: terminal.options.letterSpacing!, - lineHeight: terminal.options.lineHeight!, + letterSpacing: terminal.options.letterSpacing, + lineHeight: terminal.options.lineHeight, scaledCellWidth, scaledCellHeight, scaledCharWidth, scaledCharHeight, - fontFamily: terminal.options.fontFamily!, - fontSize: terminal.options.fontSize!, - fontWeight: terminal.options.fontWeight as FontWeight, - fontWeightBold: terminal.options.fontWeightBold as FontWeight, - allowTransparency: terminal.options.allowTransparency!, - drawBoldTextInBrightColors: terminal.options.drawBoldTextInBrightColors!, - minimumContrastRatio: terminal.options.minimumContrastRatio!, + fontFamily: terminal.options.fontFamily, + fontSize: terminal.options.fontSize, + fontWeight: terminal.options.fontWeight, + fontWeightBold: terminal.options.fontWeightBold, + allowTransparency: terminal.options.allowTransparency, + drawBoldTextInBrightColors: terminal.options.drawBoldTextInBrightColors, + minimumContrastRatio: terminal.options.minimumContrastRatio, colors: clonedColors }; } diff --git a/addons/xterm-addon-webgl/src/renderLayer/CursorRenderLayer.ts b/addons/xterm-addon-webgl/src/renderLayer/CursorRenderLayer.ts index 04e2b387..3c804b3d 100644 --- a/addons/xterm-addon-webgl/src/renderLayer/CursorRenderLayer.ts +++ b/addons/xterm-addon-webgl/src/renderLayer/CursorRenderLayer.ts @@ -213,7 +213,7 @@ export class CursorRenderLayer extends BaseRenderLayer { private _renderBarCursor(terminal: Terminal, x: number, y: number, cell: ICellData): void { this._ctx.save(); this._ctx.fillStyle = this._colors.cursor.css; - this._fillLeftLineAtCell(x, y, terminal.options.cursorWidth!); + this._fillLeftLineAtCell(x, y, terminal.options.cursorWidth); this._ctx.restore(); } diff --git a/demo/client.ts b/demo/client.ts index 64c694f5..175b61ba 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -343,7 +343,7 @@ function initOptions(term: TerminalType): void { ]; const stringOptions = { cursorStyle: ['block', 'underline', 'bar'], - fastScrollModifier: ['alt', 'ctrl', 'shift', undefined], + fastScrollModifier: ['none', 'alt', 'ctrl', 'shift'], fontFamily: null, fontWeight: ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900'], fontWeightBold: ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900'], diff --git a/src/browser/TestUtils.test.ts b/src/browser/TestUtils.test.ts index e09097a2..fa33802b 100644 --- a/src/browser/TestUtils.test.ts +++ b/src/browser/TestUtils.test.ts @@ -141,7 +141,7 @@ export class MockTerminal implements ITerminal { public renderer!: IRenderer; public linkifier2!: ILinkifier2; public isFocused!: boolean; - public options: ITerminalOptions = {}; + public options!: Required; public element!: HTMLElement; public screenElement!: HTMLElement; public rowContainer!: HTMLElement; diff --git a/src/browser/Types.d.ts b/src/browser/Types.d.ts index a3c27a88..48461a5e 100644 --- a/src/browser/Types.d.ts +++ b/src/browser/Types.d.ts @@ -16,7 +16,7 @@ export interface ITerminal extends IPublicTerminal, ICoreTerminal { browser: IBrowser; buffer: IBuffer; viewport: IViewport | undefined; - options: ITerminalOptions; + options: Required; linkifier2: ILinkifier2; onBlur: IEvent; diff --git a/src/browser/public/Terminal.ts b/src/browser/public/Terminal.ts index 57efdbd0..daf064ac 100644 --- a/src/browser/public/Terminal.ts +++ b/src/browser/public/Terminal.ts @@ -24,7 +24,7 @@ export class Terminal implements ITerminalApi { private _addonManager: AddonManager; private _parser: IParser | undefined; private _buffer: BufferNamespaceApi | undefined; - private _publicOptions: ITerminalOptions; + private _publicOptions: Required; constructor(options?: ITerminalOptions) { this._core = new TerminalCore(options); @@ -123,7 +123,7 @@ export class Terminal implements ITerminalApi { wraparoundMode: m.wraparound }; } - public get options(): ITerminalOptions { + public get options(): Required { return this._publicOptions; } public set options(options: ITerminalOptions) { diff --git a/src/common/CoreTerminal.ts b/src/common/CoreTerminal.ts index af9ec3f9..20761732 100644 --- a/src/common/CoreTerminal.ts +++ b/src/common/CoreTerminal.ts @@ -88,7 +88,7 @@ export abstract class CoreTerminal extends Disposable implements ICoreTerminal { public get cols(): number { return this._bufferService.cols; } public get rows(): number { return this._bufferService.rows; } public get buffers(): IBufferSet { return this._bufferService.buffers; } - public get options(): ITerminalOptions { return this.optionsService.options; } + public get options(): Required { return this.optionsService.options; } public set options(options: ITerminalOptions) { for (const key in options) { this.optionsService.options[key] = options[key]; diff --git a/src/common/TestUtils.test.ts b/src/common/TestUtils.test.ts index 48f3a69e..8f941fff 100644 --- a/src/common/TestUtils.test.ts +++ b/src/common/TestUtils.test.ts @@ -121,8 +121,8 @@ export class MockLogService implements ILogService { export class MockOptionsService implements IOptionsService { public serviceBrand: any; - public readonly rawOptions: ITerminalOptions = clone(DEFAULT_OPTIONS); - public options: ITerminalOptions = this.rawOptions; + public readonly rawOptions: Required = clone(DEFAULT_OPTIONS); + public options: Required = this.rawOptions; public onOptionChange: IEvent = new EventEmitter().event; constructor(testOptions?: Partial) { if (testOptions) { diff --git a/src/common/services/OptionsService.ts b/src/common/services/OptionsService.ts index ab9edfbf..1e4f53ce 100644 --- a/src/common/services/OptionsService.ts +++ b/src/common/services/OptionsService.ts @@ -8,7 +8,7 @@ import { EventEmitter, IEvent } from 'common/EventEmitter'; import { isMac } from 'common/Platform'; import { CursorStyle } from 'common/Types'; -export const DEFAULT_OPTIONS: Readonly = { +export const DEFAULT_OPTIONS: Readonly> = { cols: 80, rows: 24, cursorBlink: false, @@ -45,7 +45,7 @@ export const DEFAULT_OPTIONS: Readonly = { convertEol: false, termName: 'xterm', cancelEvents: false, - overviewRulerWidth: undefined + overviewRulerWidth: 0 }; const FONT_WEIGHT_OPTIONS: Extract[] = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']; @@ -53,8 +53,8 @@ const FONT_WEIGHT_OPTIONS: Extract[] = ['normal', 'bold', '1 export class OptionsService implements IOptionsService { public serviceBrand: any; - public readonly rawOptions: ITerminalOptions; - public options: ITerminalOptions; + public readonly rawOptions: Required; + public options: Required; private _onOptionChange = new EventEmitter(); public get onOptionChange(): IEvent { return this._onOptionChange.event; } diff --git a/src/common/services/Services.ts b/src/common/services/Services.ts index 585b29ac..ab21ecef 100644 --- a/src/common/services/Services.ts +++ b/src/common/services/Services.ts @@ -194,8 +194,8 @@ export interface IOptionsService { * single options without any validation as we trust TypeScript to enforce correct usage * internally. */ - readonly rawOptions: Readonly; - readonly options: ITerminalOptions; + readonly rawOptions: Required; + readonly options: Required; readonly onOptionChange: IEvent; } @@ -204,40 +204,40 @@ export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '50 export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'off'; export interface ITerminalOptions { - allowProposedApi: boolean; - allowTransparency: boolean; - altClickMovesCursor: boolean; - cols: number; - convertEol: boolean; - cursorBlink: boolean; - cursorStyle: CursorStyle; - cursorWidth: number; - customGlyphs: boolean; - disableStdin: boolean; - drawBoldTextInBrightColors: boolean; - fastScrollModifier: 'alt' | 'ctrl' | 'shift' | undefined; - fastScrollSensitivity: number; - fontSize: number; - fontFamily: string; - fontWeight: FontWeight; - fontWeightBold: FontWeight; - letterSpacing: number; - lineHeight: number; - logLevel: LogLevel; - macOptionIsMeta: boolean; - macOptionClickForcesSelection: boolean; - minimumContrastRatio: number; - rightClickSelectsWord: boolean; - rows: number; - screenReaderMode: boolean; - scrollback: number; - scrollSensitivity: number; - smoothScrollDuration: number; - tabStopWidth: number; - theme: ITheme; - windowsMode: boolean; - windowOptions: IWindowOptions; - wordSeparator: string; + allowProposedApi?: boolean; + allowTransparency?: boolean; + altClickMovesCursor?: boolean; + cols?: number; + convertEol?: boolean; + cursorBlink?: boolean; + cursorStyle?: CursorStyle; + cursorWidth?: number; + customGlyphs?: boolean; + disableStdin?: boolean; + drawBoldTextInBrightColors?: boolean; + fastScrollModifier?: 'none' | 'alt' | 'ctrl' | 'shift'; + fastScrollSensitivity?: number; + fontSize?: number; + fontFamily?: string; + fontWeight?: FontWeight; + fontWeightBold?: FontWeight; + letterSpacing?: number; + lineHeight?: number; + logLevel?: LogLevel; + macOptionIsMeta?: boolean; + macOptionClickForcesSelection?: boolean; + minimumContrastRatio?: number; + rightClickSelectsWord?: boolean; + rows?: number; + screenReaderMode?: boolean; + scrollback?: number; + scrollSensitivity?: number; + smoothScrollDuration?: number; + tabStopWidth?: number; + theme?: ITheme; + windowsMode?: boolean; + windowOptions?: IWindowOptions; + wordSeparator?: string; overviewRulerWidth?: number; [key: string]: any; diff --git a/src/headless/Terminal.ts b/src/headless/Terminal.ts index 7f138ce1..1cad0ee2 100644 --- a/src/headless/Terminal.ts +++ b/src/headless/Terminal.ts @@ -30,7 +30,7 @@ import { IMarker, ITerminalOptions, ScrollSource } from 'common/Types'; export class Terminal extends CoreTerminal { // TODO: We should remove options once components adopt optionsService - public get options(): IInitializedTerminalOptions { return this.optionsService.options; } + public get options(): Required { return this.optionsService.options; } private _onBell = new EventEmitter(); public get onBell(): IEvent { return this._onBell.event; } diff --git a/src/headless/public/Terminal.ts b/src/headless/public/Terminal.ts index efc0a8a1..d4360c7b 100644 --- a/src/headless/public/Terminal.ts +++ b/src/headless/public/Terminal.ts @@ -21,7 +21,7 @@ export class Terminal implements ITerminalApi { private _addonManager: AddonManager; private _parser: IParser | undefined; private _buffer: BufferNamespaceApi | undefined; - private _publicOptions: ITerminalOptions; + private _publicOptions: Required; constructor(options?: ITerminalOptions & ITerminalInitOnlyOptions) { this._core = new TerminalCore(options); @@ -123,7 +123,7 @@ export class Terminal implements ITerminalApi { wraparoundMode: m.wraparound }; } - public get options(): ITerminalOptions { + public get options(): Required { return this._publicOptions; } public set options(options: ITerminalOptions) { diff --git a/typings/xterm-headless.d.ts b/typings/xterm-headless.d.ts index cda7ad1a..55114bf6 100644 --- a/typings/xterm-headless.d.ts +++ b/typings/xterm-headless.d.ts @@ -84,7 +84,7 @@ declare module 'xterm-headless' { /** * The modifier key hold to multiply scroll speed. */ - fastScrollModifier?: 'alt' | 'ctrl' | 'shift' | undefined; + fastScrollModifier?: 'none' | 'alt' | 'ctrl' | 'shift'; /** * The spacing in whole pixels between characters. @@ -538,11 +538,12 @@ declare module 'xterm-headless' { * ```typescript * console.log(terminal.options.fontSize); * ``` + */ + get options(): Required; + + /** + * Gets or sets the terminal options. This supports setting multiple options. * - * @example Set a single option - * ```typescript - * terminal.options.fontSize = 12; - * ``` * * @example Set multiple options * ```typescript @@ -552,7 +553,7 @@ declare module 'xterm-headless' { * }; * ``` */ - options: ITerminalOptions; + set options(options: ITerminalOptions); /** * Natural language strings that can be localized. diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 05058d04..e62c5e70 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -91,7 +91,7 @@ declare module 'xterm' { /** * The modifier key hold to multiply scroll speed. */ - fastScrollModifier?: 'alt' | 'ctrl' | 'shift' | undefined; + fastScrollModifier?: 'none' | 'alt' | 'ctrl' | 'shift'; /** * The scroll speed multiplier used for fast scrolling. @@ -700,11 +700,12 @@ declare module 'xterm' { * ```typescript * console.log(terminal.options.fontSize); * ``` + */ + get options(): Required; + + /** + * Gets or sets the terminal options. This supports setting multiple options. * - * @example Set a single option - * ```typescript - * terminal.options.fontSize = 12; - * ``` * * @example Set multiple options * ```typescript @@ -714,7 +715,7 @@ declare module 'xterm' { * }; * ``` */ - options: ITerminalOptions; + set options(options: ITerminalOptions); /** * Natural language strings that can be localized. @@ -1069,7 +1070,7 @@ declare module 'xterm' { /** * An object representing a range within the viewport of the terminal. */ - export interface IViewportRange { + export interface IViewportRange { /** * The start of the range. */ From 6e123e90eec4cd1f223c8b7182e75b2bfde856ce Mon Sep 17 00:00:00 2001 From: Simon Lamon Date: Fri, 5 Aug 2022 16:56:54 +0000 Subject: [PATCH 2/2] Add example back --- typings/xterm-headless.d.ts | 4 ++++ typings/xterm.d.ts | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/typings/xterm-headless.d.ts b/typings/xterm-headless.d.ts index 55114bf6..1c0a986a 100644 --- a/typings/xterm-headless.d.ts +++ b/typings/xterm-headless.d.ts @@ -544,6 +544,10 @@ declare module 'xterm-headless' { /** * Gets or sets the terminal options. This supports setting multiple options. * + * @example Set a single option + * ```typescript + * terminal.options.fontSize = 12; + * ``` * * @example Set multiple options * ```typescript diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index e62c5e70..e989cdbc 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -706,6 +706,10 @@ declare module 'xterm' { /** * Gets or sets the terminal options. This supports setting multiple options. * + * @example Set a single option + * ```typescript + * terminal.options.fontSize = 12; + * ``` * * @example Set multiple options * ```typescript