diff --git a/src/TerminalLine.test.ts b/src/TerminalLine.test.ts new file mode 100644 index 00000000..e5a57aa5 --- /dev/null +++ b/src/TerminalLine.test.ts @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2018 The xterm.js authors. All rights reserved. + * @license MIT + */ +import * as chai from 'chai'; +import { TerminalLine } from './TerminalLine'; +import { CharData } from './Types'; +import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE, CHAR_DATA_ATTR_INDEX, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX } from './Buffer'; + +describe('TerminalLine', function(): void { + it('ctor', function(): void { + let line = new TerminalLine(); + chai.expect(line.length).equals(0); + chai.expect(line.pop()).equals(undefined); + chai.expect(line.isWrapped).equals(false); + line = new TerminalLine(10); + chai.expect(line.length).equals(10); + chai.expect(line.pop()).eql(TerminalLine.defaultCell); + chai.expect(line.isWrapped).equals(false); + line = new TerminalLine(10, null, true); + chai.expect(line.length).equals(10); + chai.expect(line.pop()).eql(TerminalLine.defaultCell); + chai.expect(line.isWrapped).equals(true); + line = new TerminalLine(10, [123, 'a', 456, 789], true); + chai.expect(line.length).equals(10); + chai.expect(line.pop()).eql([123, 'a', 456, 789]); + chai.expect(line.isWrapped).equals(true); + }); + it('splice', function(): void { + const line = new TerminalLine(); + const data: CharData[] = [ + [1, 'a', 0, 0], + [2, 'b', 0, 0], + [3, 'c', 0, 0] + ]; + for (let i = 0; i < data.length; ++i) line.push(data[i]); + chai.expect(line.length).equals(data.length); + const removed1 = line.splice(1, 1, [4, 'd', 0, 0]); + const removed2 = data.splice(1, 1, [4, 'd', 0, 0]); + chai.expect(removed1).eql(removed2); + chai.expect(line.toArray()).eql(data); + }); + it('TerminalLine.blankLine', function(): void { + const line = TerminalLine.blankLine(5, 123); + chai.expect(line.length).equals(5); + chai.expect(line.isWrapped).equals(false); + const ch = line.get(0); + chai.expect(ch[CHAR_DATA_ATTR_INDEX]).equals(123); + chai.expect(ch[CHAR_DATA_CHAR_INDEX]).equals(NULL_CELL_CHAR); + chai.expect(ch[CHAR_DATA_WIDTH_INDEX]).equals(NULL_CELL_WIDTH); + chai.expect(ch[CHAR_DATA_CODE_INDEX]).equals(NULL_CELL_CODE); + }); +}); diff --git a/src/TerminalLine.ts b/src/TerminalLine.ts index cf4f467c..fa13197f 100644 --- a/src/TerminalLine.ts +++ b/src/TerminalLine.ts @@ -1,5 +1,5 @@ /** - * Copyright (c) 2017 The xterm.js authors. All rights reserved. + * Copyright (c) 2018 The xterm.js authors. All rights reserved. * @license MIT */ import { CharData } from './Types'; @@ -11,25 +11,28 @@ import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR } from './Buffer'; * Once the storages are in place it will proxy access to * typed array based line data. * TODO: move typical line actions in `InputHandler` and `Terminal` here: - * - create blank line + * - create blank line - done * - insert cells * - remove cells + * - maybe Buffer.translateBufferLineToString */ export class TerminalLine { + static defaultCell: CharData = [0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; static blankLine(cols: number, attr: number, isWrapped?: boolean): TerminalLine { const ch: CharData = [attr, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]; - const line = new TerminalLine(); - if (isWrapped) line.isWrapped = true; - for (let i = 0; i < cols; i++) line.push(ch); - return line; + return new TerminalLine(cols, ch, isWrapped); } private _data: CharData[]; public isWrapped = false; length: number; - constructor() { + constructor(cols?: number, ch?: CharData, isWrapped?: boolean) { this._data = []; this.length = this._data.length; - + if (cols) { + if (!ch) ch = TerminalLine.defaultCell; + for (let i = 0; i < cols; i++) this.push(ch); // Note: the ctor ch is not cloned + } + if (isWrapped) this.isWrapped = true; // for debugging purpose: // throw Error when something tries to do number index access // TODO: remove when done with transition @@ -45,7 +48,6 @@ export class TerminalLine { }); } */ - } get(index: number): CharData { return this._data[index]; @@ -75,4 +77,7 @@ export class TerminalLine { release(): void { // TODO: unref here } + toArray(): CharData[] { + return this._data; + } }