From 397972086e5ddfec5e2510931f37e678c0c2428c Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 15 Jul 2017 10:33:49 -0700 Subject: [PATCH] Add IBuffer, IBufferSet and fix CircularList type --- src/Buffer.ts | 4 ++-- src/BufferSet.ts | 4 ++-- src/EventEmitter.ts | 4 +++- src/Interfaces.ts | 32 +++++++++++++++++++++++++++----- src/SelectionManager.test.ts | 6 +++--- src/SelectionManager.ts | 10 +++++----- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 256f6c903f..93af3c460f 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -13,7 +13,7 @@ import { CircularList } from './utils/CircularList'; * - scroll position */ export class Buffer { - public lines: CircularList<[string, number, string]>; + public lines: CircularList<[number, string, number][]>; /** * Create a new Buffer. @@ -33,7 +33,7 @@ export class Buffer { public scrollTop: number = 0, public tabs: any = {}, ) { - this.lines = new CircularList<[string, number, string]>(this.terminal.scrollback); + this.lines = new CircularList<[number, string, number][]>(this.terminal.scrollback); this.scrollBottom = this.terminal.rows - 1; } } diff --git a/src/BufferSet.ts b/src/BufferSet.ts index d4a471aba2..e86c098f5f 100644 --- a/src/BufferSet.ts +++ b/src/BufferSet.ts @@ -2,7 +2,7 @@ * @license MIT */ -import { ITerminal } from './Interfaces'; +import { ITerminal, IBufferSet } from './Interfaces'; import { Buffer } from './Buffer'; import { EventEmitter } from './EventEmitter'; @@ -10,7 +10,7 @@ import { EventEmitter } from './EventEmitter'; * The BufferSet represents the set of two buffers used by xterm terminals (normal and alt) and * provides also utilities for working with them. */ -export class BufferSet extends EventEmitter { +export class BufferSet extends EventEmitter implements IBufferSet { private _normal: Buffer; private _alt: Buffer; private _activeBuffer: Buffer; diff --git a/src/EventEmitter.ts b/src/EventEmitter.ts index a4fd4542c3..00c1094186 100644 --- a/src/EventEmitter.ts +++ b/src/EventEmitter.ts @@ -2,12 +2,14 @@ * @license MIT */ +import { IEventEmitter } from './Interfaces'; + interface ListenerType { (): void; listener?: () => void; }; -export class EventEmitter { +export class EventEmitter implements IEventEmitter { private _events: {[type: string]: ListenerType[]}; constructor() { diff --git a/src/Interfaces.ts b/src/Interfaces.ts index b41a7d1a69..06e8d802d7 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -33,9 +33,8 @@ export interface ITerminal { cursorState: number; defAttr: number; scrollback: number; - buffers: any; // This should be a `BufferSet` class, but it would result in circular dependency - buffer: any; // This should be a `Buffer` class, but it would result in circular dependency - viewport: any; // This should be a `Viewport` class, but it would result in circular dependency + buffers: IBufferSet; + buffer: IBuffer; /** * Emit the 'data' event and populate the given data. @@ -51,6 +50,24 @@ export interface ITerminal { showCursor(): void; } +export interface IBuffer { + lines: ICircularList<[number, string, number][]>; + ydisp: number; + ybase: number; + y: number; + x: number; + tabs: any; +} + +export interface IBufferSet { + alt: IBuffer; + normal: IBuffer; + active: IBuffer; + + activateNormalBuffer(): void; + activateAltBuffer(): void; +} + export interface ISelectionManager { selectionText: string; selectionStart: [number, number]; @@ -72,20 +89,25 @@ export interface ILinkifier { deregisterLinkMatcher(matcherId: number): boolean; } -interface ICircularList { +export interface ICircularList extends IEventEmitter { length: number; maxLength: number; forEach(callbackfn: (value: T, index: number, array: T[]) => void): void; get(index: number): T; set(index: number, value: T): void; - push(value: T): void; + push(value: T[]): void; pop(): T; splice(start: number, deleteCount: number, ...items: T[]): void; trimStart(count: number): void; shiftElements(start: number, count: number, offset: number): void; } +export interface IEventEmitter { + on(type, listener): void; + off(type, listener): void; +} + export interface LinkMatcherOptions { /** * The index of the link from the regex.match(text) call. This defaults to 0 diff --git a/src/SelectionManager.test.ts b/src/SelectionManager.test.ts index 66d692ddd6..668fd92eb5 100644 --- a/src/SelectionManager.test.ts +++ b/src/SelectionManager.test.ts @@ -3,7 +3,7 @@ */ import jsdom = require('jsdom'); import { assert } from 'chai'; -import { ITerminal } from './Interfaces'; +import { ITerminal, ICircularList } from './Interfaces'; import { CharMeasure } from './utils/CharMeasure'; import { CircularList } from './utils/CircularList'; import { SelectionManager } from './SelectionManager'; @@ -13,7 +13,7 @@ import { BufferSet } from './BufferSet'; class TestSelectionManager extends SelectionManager { constructor( terminal: ITerminal, - buffer: CircularList, + buffer: ICircularList<[number, string, number][]>, rowContainer: HTMLElement, charMeasure: CharMeasure ) { @@ -37,7 +37,7 @@ describe('SelectionManager', () => { let document: Document; let terminal: ITerminal; - let bufferLines: CircularList; + let bufferLines: ICircularList<[number, string, number][]>; let rowContainer: HTMLElement; let selectionManager: TestSelectionManager; diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 58b37b9a6e..5a86a2b9cf 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -7,7 +7,7 @@ import * as Browser from './utils/Browser'; import { CharMeasure } from './utils/CharMeasure'; import { CircularList } from './utils/CircularList'; import { EventEmitter } from './EventEmitter'; -import { ITerminal } from './Interfaces'; +import { ITerminal, ICircularList } from './Interfaces'; import { SelectionModel } from './SelectionModel'; import { translateBufferLineToString } from './utils/BufferLine'; @@ -98,7 +98,7 @@ export class SelectionManager extends EventEmitter { constructor( private _terminal: ITerminal, - private _buffer: CircularList, + private _buffer: ICircularList<[number, string, number][]>, private _rowContainer: HTMLElement, private _charMeasure: CharMeasure ) { @@ -147,7 +147,7 @@ export class SelectionManager extends EventEmitter { * switched in or out. * @param buffer The active buffer. */ - public setBuffer(buffer: CircularList): void { + public setBuffer(buffer: ICircularList<[number, string, number][]>): void { this._buffer = buffer; this.clearSelection(); } @@ -186,7 +186,7 @@ export class SelectionManager extends EventEmitter { for (let i = start[1] + 1; i <= end[1] - 1; i++) { const bufferLine = this._buffer.get(i); const lineText = translateBufferLineToString(bufferLine, true); - if (bufferLine.isWrapped) { + if ((bufferLine).isWrapped) { result[result.length - 1] += lineText; } else { result.push(lineText); @@ -197,7 +197,7 @@ export class SelectionManager extends EventEmitter { if (start[1] !== end[1]) { const bufferLine = this._buffer.get(end[1]); const lineText = translateBufferLineToString(bufferLine, true, 0, end[0]); - if (bufferLine.isWrapped) { + if ((bufferLine).isWrapped) { result[result.length - 1] += lineText; } else { result.push(lineText);