From fe8d7e4f05b04610c4fd180ea5f93f8e6773ce78 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 7 Aug 2017 15:37:22 -0700 Subject: [PATCH] Use a const for all refs to data char and width Part of #791 --- src/Buffer.ts | 4 +- src/InputHandler.ts | 7 +- src/Renderer.ts | 5 +- src/SelectionManager.ts | 20 +-- src/test/escape-sequences-test.js | 3 +- src/test/test.js | 236 +++++++++++++++--------------- 6 files changed, 138 insertions(+), 137 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index ed3309b6c0..46fe6ba8e5 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -6,8 +6,8 @@ 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; +export const CHAR_DATA_CHAR_INDEX = 1; +export const CHAR_DATA_WIDTH_INDEX = 2; /** * This class represents a terminal buffer (an internal state of the terminal), where the diff --git a/src/InputHandler.ts b/src/InputHandler.ts index defff54185..7a5232d9be 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -6,6 +6,7 @@ import { IInputHandler, ITerminal, IInputHandlingTerminal } from './Interfaces'; import { C0 } from './EscapeSequences'; import { DEFAULT_CHARSET } from './Charsets'; import { CharData } from './Types'; +import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX } from "./Buffer"; /** * The terminal's standard implementation of IInputHandler, this handles all @@ -34,14 +35,14 @@ export class InputHandler implements IInputHandler { if (!ch_width && this._terminal.buffer.x) { // dont overflow left if (this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1]) { - if (!this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1][2]) { + if (!this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1][CHAR_DATA_WIDTH_INDEX]) { // found empty cell after fullwidth, need to go 2 cells back if (this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 2]) - this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 2][1] += char; + this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 2][CHAR_DATA_CHAR_INDEX] += char; } else { - this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1][1] += char; + this._terminal.buffer.lines.get(row)[this._terminal.buffer.x - 1][CHAR_DATA_CHAR_INDEX] += char; } this._terminal.updateRange(this._terminal.buffer.y); } diff --git a/src/Renderer.ts b/src/Renderer.ts index f706bffcb3..f04b01a4d3 100644 --- a/src/Renderer.ts +++ b/src/Renderer.ts @@ -4,6 +4,7 @@ import { ITerminal } from './Interfaces'; import { DomElementObjectPool } from './utils/DomElementObjectPool'; +import { CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CHAR_INDEX } from "./Buffer"; /** * The maximum number of refresh frames to skip when the write buffer is non- @@ -168,8 +169,8 @@ export class Renderer { for (let i = 0; i < width; i++) { // TODO: Could data be a more specific type? let data: any = line[i][0]; - const ch = line[i][1]; - const ch_width: any = line[i][2]; + const ch = line[i][CHAR_DATA_CHAR_INDEX]; + const ch_width: any = line[i][CHAR_DATA_WIDTH_INDEX]; const isCursor: boolean = i === x; if (!ch_width) { continue; diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index f217e7213c..d18b689f43 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -10,6 +10,7 @@ import { EventEmitter } from './EventEmitter'; import { ITerminal, ICircularList, ISelectionManager, IBuffer } from './Interfaces'; import { SelectionModel } from './SelectionModel'; import { LineData } from './Types'; +import { CHAR_DATA_WIDTH_INDEX } from "./Buffer"; /** * The number of pixels the mouse needs to be above or below the viewport in @@ -33,11 +34,6 @@ const DRAG_SCROLL_INTERVAL = 50; */ const WORD_SEPARATORS = ' ()[]{}\'"'; -// TODO: Move these constants elsewhere, they belong in a buffer or buffer -// data/line class. -const LINE_DATA_CHAR_INDEX = 1; -const LINE_DATA_WIDTH_INDEX = 2; - const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160); const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g'); @@ -420,7 +416,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager // If the mouse is over the second half of a wide character, adjust the // selection to cover the whole character const char = line[this._model.selectionStart[0]]; - if (char[LINE_DATA_WIDTH_INDEX] === 0) { + if (char[CHAR_DATA_WIDTH_INDEX] === 0) { this._model.selectionStart[0]++; } } @@ -494,7 +490,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager // have a character. 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) { + if (char && char[CHAR_DATA_WIDTH_INDEX] === 0) { this._model.selectionEnd[0]++; } } @@ -541,7 +537,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager let charIndex = coords[0]; for (let i = 0; coords[0] >= i; i++) { const char = bufferLine[i]; - if (char[LINE_DATA_WIDTH_INDEX] === 0) { + if (char[CHAR_DATA_WIDTH_INDEX] === 0) { charIndex--; } } @@ -594,17 +590,17 @@ export class SelectionManager extends EventEmitter implements ISelectionManager let endCol = coords[0]; // Consider the initial position, skip it and increment the wide char // variable - if (bufferLine[startCol][LINE_DATA_WIDTH_INDEX] === 0) { + if (bufferLine[startCol][CHAR_DATA_WIDTH_INDEX] === 0) { leftWideCharCount++; startCol--; } - if (bufferLine[endCol][LINE_DATA_WIDTH_INDEX] === 2) { + if (bufferLine[endCol][CHAR_DATA_WIDTH_INDEX] === 2) { rightWideCharCount++; endCol++; } // Expand the string in both directions until a space is hit while (startIndex > 0 && !this._isCharWordSeparator(line.charAt(startIndex - 1))) { - if (bufferLine[startCol - 1][LINE_DATA_WIDTH_INDEX] === 0) { + if (bufferLine[startCol - 1][CHAR_DATA_WIDTH_INDEX] === 0) { // If the next character is a wide char, record it and skip the column leftWideCharCount++; startCol--; @@ -613,7 +609,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager startCol--; } while (endIndex + 1 < line.length && !this._isCharWordSeparator(line.charAt(endIndex + 1))) { - if (bufferLine[endCol + 1][LINE_DATA_WIDTH_INDEX] === 2) { + if (bufferLine[endCol + 1][CHAR_DATA_WIDTH_INDEX] === 2) { // If the next character is a wide char, record it and skip the column rightWideCharCount++; endCol++; diff --git a/src/test/escape-sequences-test.js b/src/test/escape-sequences-test.js index d0447451b4..51e5d017bb 100644 --- a/src/test/escape-sequences-test.js +++ b/src/test/escape-sequences-test.js @@ -3,6 +3,7 @@ var fs = require('fs'); var os = require('os'); var pty = require('node-pty'); var Terminal = require('../xterm'); +var CHAR_DATA_CHAR_INDEX = require('../Buffer').CHAR_DATA_CHAR_INDEX; if (os.platform() === 'win32') { // Skip tests on Windows since pty.open isn't supported @@ -65,7 +66,7 @@ function terminalToString(term) { for (var line = term.buffer.ybase; line < term.buffer.ybase + term.rows; line++) { line_s = ''; for (var cell=0; cell