From 0dfc1afc2192f6fb5cad928827776e6ec161894d Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 8 Jun 2019 16:59:28 -0700 Subject: [PATCH] Create BufferService --- src/Terminal.ts | 21 ++++++++++---------- src/browser/services/CharSizeService.ts | 2 +- src/common/services/BufferService.ts | 26 +++++++++++++++++++++++++ src/common/services/Services.d.ts | 9 +++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 src/common/services/BufferService.ts diff --git a/src/Terminal.ts b/src/Terminal.ts index 29a0478f8..402a4a2bc 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -51,10 +51,11 @@ import { Attributes, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { applyWindowsMode } from './WindowsMode'; import { ColorManager } from 'browser/ColorManager'; import { RenderCoordinator } from './renderer/RenderCoordinator'; -import { IOptionsService } from 'common/services/Services'; +import { IOptionsService, IBufferService } from 'common/services/Services'; import { OptionsService } from 'common/services/OptionsService'; import { ICharSizeService } from 'browser/services/Services'; import { CharSizeService } from 'browser/services/CharSizeService'; +import { BufferService, MINIMUM_COLS, MINIMUM_ROWS } from 'common/services/BufferService'; // Let it work inside Node.js for automated testing purposes. const document = (typeof window !== 'undefined') ? window.document : null; @@ -75,9 +76,6 @@ const WRITE_BUFFER_PAUSE_THRESHOLD = 5; const WRITE_TIMEOUT_MS = 12; const WRITE_BUFFER_LENGTH_THRESHOLD = 50; -const MINIMUM_COLS = 2; // Less than 2 can mess with wide chars -const MINIMUM_ROWS = 1; - export class Terminal extends EventEmitter implements ITerminal, IDisposable, IInputHandlingTerminal { public textarea: HTMLTextAreaElement; public element: HTMLElement; @@ -108,6 +106,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II private _customKeyEventHandler: CustomKeyEventHandler; // common services + private _bufferService: IBufferService; public optionsService: IOptionsService; // browser services @@ -188,8 +187,8 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II // bufferline to clone/copy from for new blank lines private _blankLine: IBufferLine = null; - public cols: number; - public rows: number; + public get cols(): number { return this._bufferService.cols; } + public get rows(): number { return this._bufferService.rows; } private _onCursorMove = new EventEmitter2(); public get onCursorMove(): IEvent { return this._onCursorMove.event; } @@ -226,7 +225,11 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II options: ITerminalOptions = {} ) { super(); + + // Initialize common services this.optionsService = new OptionsService(options); + this._bufferService = new BufferService(this.optionsService); + this._setupOptionsListeners(); // this.options = clone(options); @@ -263,9 +266,6 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II private _setup(): void { this._parent = document ? document.body : null; - this.cols = Math.max(this.options.cols, MINIMUM_COLS); - this.rows = Math.max(this.options.rows, MINIMUM_ROWS); - this.cursorState = 0; this.cursorHidden = false; this._customKeyEventHandler = null; @@ -1745,8 +1745,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II this.buffers.resize(x, y); - this.cols = x; - this.rows = y; + this._bufferService.resize(x, y); this.buffers.setupTabStops(this.cols); if (this._charSizeService) { diff --git a/src/browser/services/CharSizeService.ts b/src/browser/services/CharSizeService.ts index 60ce685e7..9312e3833 100644 --- a/src/browser/services/CharSizeService.ts +++ b/src/browser/services/CharSizeService.ts @@ -72,7 +72,7 @@ class DomMeasureStrategy implements IMeasureStrategy { // Note that this triggers a synchronous layout const geometry = this._measureElement.getBoundingClientRect(); -console.log('measure', geometry); + // If values are 0 then the element is likely currently display:none, in which case we should // retain the previous value. if (geometry.width !== 0 && geometry.height !== 0) { diff --git a/src/common/services/BufferService.ts b/src/common/services/BufferService.ts new file mode 100644 index 000000000..542166c11 --- /dev/null +++ b/src/common/services/BufferService.ts @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2019 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { IBufferService, IOptionsService } from './Services'; + +export const MINIMUM_COLS = 2; // Less than 2 can mess with wide chars +export const MINIMUM_ROWS = 1; + +export class BufferService implements IBufferService { + public cols: number; + public rows: number; + + constructor( + optionsService: IOptionsService + ) { + this.cols = Math.max(optionsService.options.cols, MINIMUM_COLS); + this.rows = Math.max(optionsService.options.rows, MINIMUM_ROWS); + } + + public resize(cols: number, rows: number): void { + this.cols = cols; + this.rows = rows; + } +} diff --git a/src/common/services/Services.d.ts b/src/common/services/Services.d.ts index 651e86360..8ebc0308d 100644 --- a/src/common/services/Services.d.ts +++ b/src/common/services/Services.d.ts @@ -5,6 +5,15 @@ import { IEvent } from 'common/EventEmitter2'; +export interface IBufferService { + readonly cols: number; + readonly rows: number; + + // TODO: Move resize event here + + resize(cols: number, rows: number): void; +} + export interface IOptionsService { readonly options: ITerminalOptions;