Skip to content

Commit

Permalink
Move BufferLines.ts into Buffer.ts
Browse files Browse the repository at this point in the history
Part of xtermjs#791
  • Loading branch information
Tyriar committed Aug 7, 2017
1 parent 091854d commit c2bb507
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 92 deletions.
51 changes: 51 additions & 0 deletions src/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { ITerminal, IBuffer } from './Interfaces';
import { CircularList } from './utils/CircularList';
import { LineData, CharData } from './Types';

const CHAR_DATA_CHAR_INDEX = 1;
const CHAR_DATA_WIDTH_INDEX = 2;

/**
* This class represents a terminal buffer (an internal state of the terminal), where the
* following information is stored (in high-level):
Expand Down Expand Up @@ -146,4 +149,52 @@ export class Buffer implements IBuffer {
this.scrollTop = 0;
this.scrollBottom = newRows - 1;
}

/**
* Translates a buffer line to a string, with optional start and end columns.
* Wide characters will count as two columns in the resulting string. This
* function is useful for getting the actual text underneath the raw selection
* position.
* @param line The line being translated.
* @param trimRight Whether to trim whitespace to the right.
* @param startCol The column to start at.
* @param endCol The column to end at.
*/
public translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol: number = 0, endCol: number = null): string {
// Get full line
let lineString = '';
let widthAdjustedStartCol = startCol;
let widthAdjustedEndCol = endCol;
const line = this.lines.get(lineIndex);
for (let i = 0; i < line.length; i++) {
const char = line[i];
lineString += char[CHAR_DATA_CHAR_INDEX];
// Adjust start and end cols for wide characters if they affect their
// column indexes
if (char[CHAR_DATA_WIDTH_INDEX] === 0) {
if (startCol >= i) {
widthAdjustedStartCol--;
}
if (endCol >= i) {
widthAdjustedEndCol--;
}
}
}

// Calculate the final end col by trimming whitespace on the right of the
// line if needed.
let finalEndCol = widthAdjustedEndCol || line.length;
if (trimRight) {
const rightWhitespaceIndex = lineString.search(/\s+$/);
if (rightWhitespaceIndex !== -1) {
finalEndCol = Math.min(finalEndCol, rightWhitespaceIndex);
}
// Return the empty string if only trimmed whitespace is selected
if (finalEndCol <= widthAdjustedStartCol) {
return '';
}
}

return lineString.substring(widthAdjustedStartCol, finalEndCol);
}
}
3 changes: 2 additions & 1 deletion src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ export class InputHandler implements IInputHandler {
case 47: // alt screen buffer
case 1047: // alt screen buffer
this._terminal.buffers.activateAltBuffer();
this._terminal.selectionManager.setBuffer(this._terminal.buffer);
this._terminal.viewport.syncScrollArea();
this._terminal.showCursor();
break;
Expand Down Expand Up @@ -1113,7 +1114,7 @@ export class InputHandler implements IInputHandler {
// if (params[0] === 1049) {
// this.restoreCursor(params);
// }
this._terminal.selectionManager.setBuffer(this._terminal.buffer.lines);
this._terminal.selectionManager.setBuffer(this._terminal.buffer);
this._terminal.refresh(0, this._terminal.rows - 1);
this._terminal.viewport.syncScrollArea();
this._terminal.showCursor();
Expand Down
3 changes: 2 additions & 1 deletion src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export interface IBuffer {
scrollTop: number;
savedY: number;
savedX: number;
translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol?: number, endCol?: number): string;
}

export interface IBufferSet {
Expand All @@ -166,7 +167,7 @@ export interface ISelectionManager {

disable(): void;
enable(): void;
setBuffer(buffer: ICircularList<LineData>): void;
setBuffer(buffer: IBuffer): void;
setSelection(row: number, col: number, length: number): void;
}

Expand Down
34 changes: 17 additions & 17 deletions src/SelectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import jsdom = require('jsdom');
import { assert } from 'chai';
import { ITerminal, ICircularList } from './Interfaces';
import { ITerminal, ICircularList, IBuffer } from './Interfaces';
import { CharMeasure } from './utils/CharMeasure';
import { CircularList } from './utils/CircularList';
import { SelectionManager } from './SelectionManager';
Expand All @@ -16,7 +16,7 @@ import { LineData } from './Types';
class TestSelectionManager extends SelectionManager {
constructor(
terminal: ITerminal,
buffer: ICircularList<LineData>,
buffer: IBuffer,
rowContainer: HTMLElement,
charMeasure: CharMeasure
) {
Expand All @@ -40,7 +40,7 @@ describe('SelectionManager', () => {
let document: Document;

let terminal: ITerminal;
let bufferLines: ICircularList<LineData>;
let buffer: IBuffer;
let rowContainer: HTMLElement;
let selectionManager: TestSelectionManager;

Expand All @@ -55,8 +55,8 @@ describe('SelectionManager', () => {
terminal.options.scrollback = 100;
terminal.buffers = new BufferSet(terminal);
terminal.buffer = terminal.buffers.active;
bufferLines = terminal.buffer.lines;
selectionManager = new TestSelectionManager(terminal, bufferLines, rowContainer, null);
buffer = terminal.buffer;
selectionManager = new TestSelectionManager(terminal, buffer, rowContainer, null);
});

function stringToRow(text: string): LineData {
Expand All @@ -69,7 +69,7 @@ describe('SelectionManager', () => {

describe('_selectWordAt', () => {
it('should expand selection for normal width chars', () => {
bufferLines.set(0, stringToRow('foo bar'));
buffer.lines.set(0, stringToRow('foo bar'));
selectionManager.selectWordAt([0, 0]);
assert.equal(selectionManager.selectionText, 'foo');
selectionManager.selectWordAt([1, 0]);
Expand All @@ -86,7 +86,7 @@ describe('SelectionManager', () => {
assert.equal(selectionManager.selectionText, 'bar');
});
it('should expand selection for whitespace', () => {
bufferLines.set(0, stringToRow('a b'));
buffer.lines.set(0, stringToRow('a b'));
selectionManager.selectWordAt([0, 0]);
assert.equal(selectionManager.selectionText, 'a');
selectionManager.selectWordAt([1, 0]);
Expand All @@ -100,7 +100,7 @@ describe('SelectionManager', () => {
});
it('should expand selection for wide characters', () => {
// Wide characters use a special format
bufferLines.set(0, [
buffer.lines.set(0, [
[null, '中', 2],
[null, '', 0],
[null, '文', 2],
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('SelectionManager', () => {
assert.equal(selectionManager.selectionText, 'foo');
});
it('should select up to non-path characters that are commonly adjacent to paths', () => {
bufferLines.set(0, stringToRow('(cd)[ef]{gh}\'ij"'));
buffer.lines.set(0, stringToRow('(cd)[ef]{gh}\'ij"'));
selectionManager.selectWordAt([0, 0]);
assert.equal(selectionManager.selectionText, '(cd');
selectionManager.selectWordAt([1, 0]);
Expand Down Expand Up @@ -190,7 +190,7 @@ describe('SelectionManager', () => {

describe('_selectLineAt', () => {
it('should select the entire line', () => {
bufferLines.set(0, stringToRow('foo bar'));
buffer.lines.set(0, stringToRow('foo bar'));
selectionManager.selectLineAt(0);
assert.equal(selectionManager.selectionText, 'foo bar', 'The selected text is correct');
assert.deepEqual(selectionManager.model.finalSelectionStart, [0, 0]);
Expand All @@ -200,14 +200,14 @@ describe('SelectionManager', () => {

describe('selectAll', () => {
it('should select the entire buffer, beyond the viewport', () => {
bufferLines.length = 5;
bufferLines.set(0, stringToRow('1'));
bufferLines.set(1, stringToRow('2'));
bufferLines.set(2, stringToRow('3'));
bufferLines.set(3, stringToRow('4'));
bufferLines.set(4, stringToRow('5'));
buffer.lines.length = 5;
buffer.lines.set(0, stringToRow('1'));
buffer.lines.set(1, stringToRow('2'));
buffer.lines.set(2, stringToRow('3'));
buffer.lines.set(3, stringToRow('4'));
buffer.lines.set(4, stringToRow('5'));
selectionManager.selectAll();
terminal.buffer.ybase = bufferLines.length - terminal.rows;
terminal.buffer.ybase = buffer.lines.length - terminal.rows;
assert.equal(selectionManager.selectionText, '1\n2\n3\n4\n5');
});
});
Expand Down
29 changes: 14 additions & 15 deletions src/SelectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import * as Browser from './utils/Browser';
import { CharMeasure } from './utils/CharMeasure';
import { CircularList } from './utils/CircularList';
import { EventEmitter } from './EventEmitter';
import { ITerminal, ICircularList, ISelectionManager } from './Interfaces';
import { ITerminal, ICircularList, ISelectionManager, IBuffer } from './Interfaces';
import { SelectionModel } from './SelectionModel';
import { translateBufferLineToString } from './utils/BufferLine';
import { LineData } from './Types';

/**
Expand Down Expand Up @@ -102,7 +101,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager

constructor(
private _terminal: ITerminal,
private _buffer: ICircularList<LineData>,
private _buffer: IBuffer,
private _rowContainer: HTMLElement,
private _charMeasure: CharMeasure
) {
Expand All @@ -127,7 +126,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
// reverseIndex) and delete in a splice is only ever used when the same
// number of elements was just added. Given this is could actually be
// beneficial to leave the selection as is for these cases.
this._buffer.on('trim', (amount: number) => this._onTrim(amount));
this._buffer.lines.on('trim', (amount: number) => this._onTrim(amount));
}

/**
Expand All @@ -151,7 +150,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
* switched in or out.
* @param buffer The active buffer.
*/
public setBuffer(buffer: ICircularList<LineData>): void {
public setBuffer(buffer: IBuffer): void {
this._buffer = buffer;
this.clearSelection();
}
Expand Down Expand Up @@ -184,12 +183,12 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
// Get first row
const startRowEndCol = start[1] === end[1] ? end[0] : null;
let result: string[] = [];
result.push(translateBufferLineToString(this._buffer.get(start[1]), true, start[0], startRowEndCol));
result.push(this._buffer.translateBufferLineToString(start[1], true, start[0], startRowEndCol));

// Get middle rows
for (let i = start[1] + 1; i <= end[1] - 1; i++) {
const bufferLine = this._buffer.get(i);
const lineText = translateBufferLineToString(bufferLine, true);
const bufferLine = this._buffer.lines.get(i);
const lineText = this._buffer.translateBufferLineToString(i, true);
if ((<any>bufferLine).isWrapped) {
result[result.length - 1] += lineText;
} else {
Expand All @@ -199,8 +198,8 @@ export class SelectionManager extends EventEmitter implements ISelectionManager

// Get final row
if (start[1] !== end[1]) {
const bufferLine = this._buffer.get(end[1]);
const lineText = translateBufferLineToString(bufferLine, true, 0, end[0]);
const bufferLine = this._buffer.lines.get(end[1]);
const lineText = this._buffer.translateBufferLineToString(end[1], true, 0, end[0]);
if ((<any>bufferLine).isWrapped) {
result[result.length - 1] += lineText;
} else {
Expand Down Expand Up @@ -413,7 +412,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
this._model.selectionEnd = null;

// Ensure the line exists
const line = this._buffer.get(this._model.selectionStart[1]);
const line = this._buffer.lines.get(this._model.selectionStart[1]);
if (!line) {
return;
}
Expand Down Expand Up @@ -493,8 +492,8 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
// If the character is a wide character include the cell to the right in the
// selection. Note that selections at the very end of the line will never
// have a character.
if (this._model.selectionEnd[1] < this._buffer.length) {
const char = this._buffer.get(this._model.selectionEnd[1])[this._model.selectionEnd[0]];
if (this._model.selectionEnd[1] < this._buffer.lines.length) {
const char = this._buffer.lines.get(this._model.selectionEnd[1])[this._model.selectionEnd[0]];
if (char && char[2] === 0) {
this._model.selectionEnd[0]++;
}
Expand Down Expand Up @@ -562,12 +561,12 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
* @param coords The coordinates to get the word at.
*/
private _getWordAt(coords: [number, number]): IWordPosition {
const bufferLine = this._buffer.get(coords[1]);
const bufferLine = this._buffer.lines.get(coords[1]);
if (!bufferLine) {
return null;
}

const line = translateBufferLineToString(bufferLine, false);
const line = this._buffer.translateBufferLineToString(coords[1], false);

// Get actual index, taking into consideration wide characters
let endIndex = this._convertViewportColToCharacterIndex(bufferLine, coords);
Expand Down
5 changes: 2 additions & 3 deletions src/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import * as Browser from './utils/Browser';
import * as Mouse from './utils/Mouse';
import { CHARSETS } from './Charsets';
import { getRawByteCoords } from './utils/Mouse';
import { translateBufferLineToString } from './utils/BufferLine';
import { CustomKeyEventHandler, Charset, LinkMatcherHandler, LinkMatcherValidationCallback, CharData, LineData, Option, StringOption, BooleanOption, StringArrayOption, NumberOption, GeometryOption, HandlerOption } from './Types';
import { ITerminal, IBrowser, ITerminalOptions, IInputHandlingTerminal, ILinkMatcherOptions } from './Interfaces';

Expand Down Expand Up @@ -379,7 +378,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT

// Ensure the selection manager has the correct buffer
if (this.selectionManager) {
this.selectionManager.setBuffer(this.buffer.lines);
this.selectionManager.setBuffer(this.buffer);
}

this.setupStops();
Expand Down Expand Up @@ -744,7 +743,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT

this.viewport = new Viewport(this, this.viewportElement, this.viewportScrollArea, this.charMeasure);
this.renderer = new Renderer(this);
this.selectionManager = new SelectionManager(this, this.buffer.lines, this.rowContainer, this.charMeasure);
this.selectionManager = new SelectionManager(this, this.buffer, this.rowContainer, this.charMeasure);
this.selectionManager.on('refresh', data => {
this.renderer.refreshSelection(data.start, data.end);
});
Expand Down
55 changes: 0 additions & 55 deletions src/utils/BufferLine.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/utils/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,7 @@ export class MockBuffer implements IBuffer {
scrollTop: number;
savedY: number;
savedX: number;
translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol?: number, endCol?: number): string {
throw new Error('Method not implemented.');
}
}

0 comments on commit c2bb507

Please sign in to comment.