Skip to content

Commit

Permalink
Add IBuffer, IBufferSet and fix CircularList type
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Jul 15, 2017
1 parent 1a2e3fb commit 3979720
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/BufferSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* @license MIT
*/

import { ITerminal } from './Interfaces';
import { ITerminal, IBufferSet } from './Interfaces';
import { Buffer } from './Buffer';
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;
Expand Down
4 changes: 3 additions & 1 deletion src/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
32 changes: 27 additions & 5 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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];
Expand All @@ -72,20 +89,25 @@ export interface ILinkifier {
deregisterLinkMatcher(matcherId: number): boolean;
}

interface ICircularList<T> {
export interface ICircularList<T> 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
Expand Down
6 changes: 3 additions & 3 deletions src/SelectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -13,7 +13,7 @@ import { BufferSet } from './BufferSet';
class TestSelectionManager extends SelectionManager {
constructor(
terminal: ITerminal,
buffer: CircularList<any>,
buffer: ICircularList<[number, string, number][]>,
rowContainer: HTMLElement,
charMeasure: CharMeasure
) {
Expand All @@ -37,7 +37,7 @@ describe('SelectionManager', () => {
let document: Document;

let terminal: ITerminal;
let bufferLines: CircularList<any>;
let bufferLines: ICircularList<[number, string, number][]>;
let rowContainer: HTMLElement;
let selectionManager: TestSelectionManager;

Expand Down
10 changes: 5 additions & 5 deletions src/SelectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -98,7 +98,7 @@ export class SelectionManager extends EventEmitter {

constructor(
private _terminal: ITerminal,
private _buffer: CircularList<any>,
private _buffer: ICircularList<[number, string, number][]>,
private _rowContainer: HTMLElement,
private _charMeasure: CharMeasure
) {
Expand Down Expand Up @@ -147,7 +147,7 @@ export class SelectionManager extends EventEmitter {
* switched in or out.
* @param buffer The active buffer.
*/
public setBuffer(buffer: CircularList<any>): void {
public setBuffer(buffer: ICircularList<[number, string, number][]>): void {
this._buffer = buffer;
this.clearSelection();
}
Expand Down Expand Up @@ -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 ((<any>bufferLine).isWrapped) {
result[result.length - 1] += lineText;
} else {
result.push(lineText);
Expand All @@ -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 ((<any>bufferLine).isWrapped) {
result[result.length - 1] += lineText;
} else {
result.push(lineText);
Expand Down

0 comments on commit 3979720

Please sign in to comment.