From 14498104fc409d82e15b25b2c9d7a7ca43fad2ee Mon Sep 17 00:00:00 2001 From: npezza93 Date: Thu, 17 Aug 2017 18:19:56 -0400 Subject: [PATCH 01/26] Alt+click will move the prompt cursor to that position Fixes #890 --- src/Terminal.ts | 2 + src/handlers/AltClickHandler.ts | 130 ++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/handlers/AltClickHandler.ts diff --git a/src/Terminal.ts b/src/Terminal.ts index 044f5351..b6a94ddb 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -27,6 +27,7 @@ import { CompositionHelper } from './CompositionHelper'; import { EventEmitter } from './EventEmitter'; import { Viewport } from './Viewport'; import { rightClickHandler, moveTextAreaUnderMouseCursor, pasteHandler, copyHandler } from './handlers/Clipboard'; +import { AltClickHandler } from './handlers/AltClickHandler'; import { CircularList } from './utils/CircularList'; import { C0 } from './EscapeSequences'; import { InputHandler } from './InputHandler'; @@ -953,6 +954,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT // if the selection manager is having selection forced (ie. a modifier is // held). if (!this.mouseEvents || this.selectionManager.shouldForceSelection(ev)) { + (new AltClickHandler(ev, this)).move(); return; } diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts new file mode 100644 index 00000000..827e9fdc --- /dev/null +++ b/src/handlers/AltClickHandler.ts @@ -0,0 +1,130 @@ +/** + * Alt+Click handler module: exports methods for handling all alt+click-related events in the + * terminal. + * @module xterm/handlers/AltClickHandler + * @license MIT + */ + +import { Terminal } from '../Terminal'; +import { CHAR_DATA_WIDTH_INDEX } from '../Buffer'; +import { C0 } from '../EscapeSequences'; + +export class AltClickHandler { + private _terminal: Terminal; + private _mouseRow: number; + private _mouseCol: number; + private _mouseEvent: MouseEvent; + + constructor(mouseEvent: MouseEvent, terminal: Terminal) { + this._terminal = terminal; + + [this._mouseCol, this._mouseRow] = this._terminal.mouseHelper.getCoords( + (this._mouseEvent = mouseEvent), + this._terminal.element, + this._terminal.charMeasure, + this._terminal.options.lineHeight, + this._terminal.cols, + this._terminal.rows, + true + ).map((coordinate: number) => { + return coordinate - 1; + }); + } + + public move(): void { + if (!this._mouseEvent.altKey) return; + + let keyboardArrows; + + if (this._terminal.buffer === this._terminal.buffers.normal) { + keyboardArrows = this.buildArrowSequence(this.normalCharCount(), this.horizontalCursorCommand(this.normalMoveForward())); + } else { + keyboardArrows = this.buildArrowSequence(this.altVerticalCharCount(), this.verticalCursorCommand(this.altMoveUpward())); + keyboardArrows += this.buildArrowSequence(this.altHorizontalCharCount(), this.horizontalCursorCommand(this.altMoveForward())); + } + + this._terminal.send(keyboardArrows); + } + + private buildArrowSequence(count: number, sequence: string): string { + return Array(count).join(sequence); + } + + private altMoveUpward(): boolean { + return this._terminal.buffer.y > this._mouseRow; + } + + private altMoveForward(): boolean { + return this._terminal.buffer.x < this._mouseCol; + } + + private normalMoveForward(): boolean { + return (this._terminal.buffer.x < this._mouseCol && + this._terminal.buffer.y <= this._mouseRow) || // down/right or same row/right + (this._terminal.buffer.x >= this._mouseCol && + this._terminal.buffer.y < this._mouseRow); // down/left or same row/left + } + + private horizontalCursorCommand(moveForward: boolean): string { + let mod = this._terminal.applicationCursor ? 'O' : '['; + + if (moveForward) { + return C0.ESC + mod + 'C'; + } else { + return C0.ESC + mod + 'D'; + } + } + + private verticalCursorCommand(moveUp: boolean): string { + let mod = this._terminal.applicationCursor ? 'O' : '['; + + if (moveUp) { + return C0.ESC + mod + 'A'; + } else { + return C0.ESC + mod + 'B'; + } + } + + private altVerticalCharCount(): number { + return Math.abs(this._terminal.buffer.y - this._mouseRow) + 1; + } + + private altHorizontalCharCount(): number { + return Math.abs(this._terminal.buffer.x - this._mouseCol) + 1; + } + + private normalCharCount(): number { + let currentX = this._terminal.buffer.x; + let currentY = this._terminal.buffer.y; + let startCol = this._terminal.buffer.x; + let bufferStr = ''; + + while (currentX !== this._mouseCol || (currentY !== this._mouseRow)) { + if (this.normalMoveForward()) { + currentX++; + if (currentX > this._terminal.cols - 1) { + bufferStr += this._terminal.buffer.translateBufferLineToString(currentY, false, startCol, currentX); + currentX = 0; + startCol = 0; + currentY++; + } + } else { + currentX--; + if (currentX < 0) { + bufferStr += this._terminal.buffer.translateBufferLineToString(currentY, false, 0, startCol + 1); + currentX = this._terminal.cols - 1; + startCol = currentX; + currentY--; + } + } + } + + if (this.normalMoveForward()) { + currentX++; + } else { + currentX--; + } + bufferStr += this._terminal.buffer.translateBufferLineToString(currentY, false, startCol, currentX); + return bufferStr.length; + } +} From 7e0a4840004cbea7c3efd73d67cf080e17554c7a Mon Sep 17 00:00:00 2001 From: npezza93 Date: Mon, 23 Oct 2017 19:49:39 -0400 Subject: [PATCH 02/26] Handle going back and up in the alt buffer from a long line to a short line --- src/handlers/AltClickHandler.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 827e9fdc..42fdc443 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -39,8 +39,14 @@ export class AltClickHandler { if (this._terminal.buffer === this._terminal.buffers.normal) { keyboardArrows = this.buildArrowSequence(this.normalCharCount(), this.horizontalCursorCommand(this.normalMoveForward())); } else { - keyboardArrows = this.buildArrowSequence(this.altVerticalCharCount(), this.verticalCursorCommand(this.altMoveUpward())); - keyboardArrows += this.buildArrowSequence(this.altHorizontalCharCount(), this.horizontalCursorCommand(this.altMoveForward())); + let verticalChars = this.buildArrowSequence(this.altVerticalCharCount(), this.verticalCursorCommand(this.altMoveUpward())); + let horizontalChars = this.buildArrowSequence(this.altHorizontalCharCount(), this.horizontalCursorCommand(this.altMoveForward())); + + if (this.altMoveForward()) { + keyboardArrows = verticalChars + horizontalChars + } else { + keyboardArrows = horizontalChars + verticalChars + } } this._terminal.send(keyboardArrows); From 8d41c4c603a07e5ec3ace62d5eb4a72b9ee0e363 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Mon, 23 Oct 2017 19:55:24 -0400 Subject: [PATCH 03/26] Fix lints --- src/handlers/AltClickHandler.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 42fdc443..e595ce49 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -43,9 +43,9 @@ export class AltClickHandler { let horizontalChars = this.buildArrowSequence(this.altHorizontalCharCount(), this.horizontalCursorCommand(this.altMoveForward())); if (this.altMoveForward()) { - keyboardArrows = verticalChars + horizontalChars + keyboardArrows = verticalChars + horizontalChars; } else { - keyboardArrows = horizontalChars + verticalChars + keyboardArrows = horizontalChars + verticalChars; } } From 027bc9535ef66ba59dbc2f2b4f2c64086c1252a2 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Tue, 23 Jan 2018 20:57:31 -0500 Subject: [PATCH 04/26] Pass false for isSelection so the right half of the cell wont select the next element --- src/handlers/AltClickHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index e595ce49..59962d96 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -25,7 +25,7 @@ export class AltClickHandler { this._terminal.options.lineHeight, this._terminal.cols, this._terminal.rows, - true + false ).map((coordinate: number) => { return coordinate - 1; }); From 1dce76dbbb02ee562ec7517b513b00cd3242a28e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 25 Jan 2018 12:17:56 -0800 Subject: [PATCH 05/26] Add curly to tslint, fix errors Fixes #1248 --- src/CharWidth.ts | 34 ++++++++++++++++++++++------------ src/InputHandler.ts | 3 ++- src/Parser.ts | 3 ++- src/SelectionManager.ts | 3 ++- src/Terminal.ts | 14 ++++++++------ tslint.json | 4 ++++ 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/CharWidth.ts b/src/CharWidth.ts index d296b435..512ed5f0 100644 --- a/src/CharWidth.ts +++ b/src/CharWidth.ts @@ -63,28 +63,33 @@ export const wcwidth = (function(opts: {nul: number, control: number}): (ucs: nu let min = 0; let max = data.length - 1; let mid; - if (ucs < data[0][0] || ucs > data[max][1]) + if (ucs < data[0][0] || ucs > data[max][1]) { return false; + } while (max >= min) { mid = (min + max) >> 1; - if (ucs > data[mid][1]) + if (ucs > data[mid][1]) { min = mid + 1; - else if (ucs < data[mid][0]) + } else if (ucs < data[mid][0]) { max = mid - 1; - else + } else { return true; + } } return false; } function wcwidthBMP(ucs: number): number { // test for 8-bit control characters - if (ucs === 0) + if (ucs === 0) { return opts.nul; - if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) + } + if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0)) { return opts.control; + } // binary search in table of non-spacing characters - if (bisearch(ucs, COMBINING_BMP)) + if (bisearch(ucs, COMBINING_BMP)) { return 0; + } // if we arrive here, ucs is not a combining or C0/C1 control character if (isWideBMP(ucs)) { return 2; @@ -106,8 +111,9 @@ export const wcwidth = (function(opts: {nul: number, control: number}): (ucs: nu (ucs >= 0xffe0 && ucs <= 0xffe6))); } function wcwidthHigh(ucs: number): 0 | 1 | 2 { - if (bisearch(ucs, COMBINING_HIGH)) + if (bisearch(ucs, COMBINING_HIGH)) { return 0; + } if ((ucs >= 0x20000 && ucs <= 0x2fffd) || (ucs >= 0x30000 && ucs <= 0x3fffd)) { return 2; } @@ -128,8 +134,9 @@ export const wcwidth = (function(opts: {nul: number, control: number}): (ucs: nu for (let i = 0; i < CONTAINERSIZE; ++i) { let num = 0; let pos = CODEPOINTS_PER_ITEM; - while (pos--) + while (pos--) { num = (num << 2) | wcwidthBMP(CODEPOINTS_PER_ITEM * i + pos); + } table[i] = num; } return table; @@ -148,13 +155,16 @@ export const wcwidth = (function(opts: {nul: number, control: number}): (ucs: nu // ==> n = n & 3 e.g. 000000000000000000000000000000XX return function (num: number): number { num = num | 0; // get asm.js like optimization under V8 - if (num < 32) + if (num < 32) { return control | 0; - if (num < 127) + } + if (num < 127) { return 1; + } let t = table || init_table(); - if (num < 65536) + if (num < 65536) { return t[num >> 4] >> ((num & 15) << 1) & 3; + } // do a full search for high codepoints return wcwidthHigh(num); }; diff --git a/src/InputHandler.ts b/src/InputHandler.ts index f71117f8..9cdaddaa 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -69,8 +69,9 @@ export class InputHandler implements IInputHandler { (this._terminal.buffer.lines.get(this._terminal.buffer.y)).isWrapped = true; } } else { - if (chWidth === 2) // FIXME: check for xterm behavior + if (chWidth === 2) { // FIXME: check for xterm behavior return; + } } } row = this._terminal.buffer.y + this._terminal.buffer.ybase; diff --git a/src/Parser.ts b/src/Parser.ts index 3ac03e4f..03b4a39e 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -224,8 +224,9 @@ export class Parser { ch += data.charAt(this._position + 1); } // surrogate low - already handled above - if (0xDC00 <= code && code <= 0xDFFF) + if (0xDC00 <= code && code <= 0xDFFF) { continue; + } switch (this._state) { case ParserState.NORMAL: diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 10612c5c..383427b9 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -538,8 +538,9 @@ export class SelectionManager extends EventEmitter implements ISelectionManager private _onMouseUp(event: MouseEvent): void { this._removeMouseDownListeners(); - if (this.hasSelection) + if (this.hasSelection) { this._terminal.emit('selection'); + } } private _onBufferActivate(e: {activeBuffer: IBuffer, inactiveBuffer: IBuffer}): void { diff --git a/src/Terminal.ts b/src/Terminal.ts index 5536cf03..a894f8c0 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -1580,21 +1580,23 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT break; case 36: // home - if (modifiers) + if (modifiers) { result.key = C0.ESC + '[1;' + (modifiers + 1) + 'H'; - else if (this.applicationCursor) + } else if (this.applicationCursor) { result.key = C0.ESC + 'OH'; - else + } else { result.key = C0.ESC + '[H'; + } break; case 35: // end - if (modifiers) + if (modifiers) { result.key = C0.ESC + '[1;' + (modifiers + 1) + 'F'; - else if (this.applicationCursor) + } else if (this.applicationCursor) { result.key = C0.ESC + 'OF'; - else + } else { result.key = C0.ESC + '[F'; + } break; case 33: // page up diff --git a/tslint.json b/tslint.json index f6ad759a..37ede7fe 100644 --- a/tslint.json +++ b/tslint.json @@ -9,6 +9,10 @@ true, "check-space" ], + "curly": [ + true, + "ignore-same-line" + ], "indent": [ true, "spaces" From 2c065b66bec4a84083339575b8163645d0d7ecfe Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 25 Jan 2018 12:31:48 -0800 Subject: [PATCH 06/26] Add SQL Operations Studio to real world uses --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 730fc9af..a5d34632 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,7 @@ computational environment for Jupyter, supporting interactive data science and s - [**rtty**](https://github.com/zhaojh329/rtty): A reverse proxy WebTTY. It is composed of the client and the server. - [**Pisth**](https://github.com/ColdGrub1384/Pisth): An SFTP and SSH client for iOS - [**abstruse**](https://github.com/bleenco/abstruse): Abstruse CI is a continuous integration platform based on Node.JS and Docker. +- [**Microsoft SQL Operations Studio**](https://github.com/Microsoft/sqlopsstudio): A data management tool that enables working with SQL Server, Azure SQL DB and SQL DW from Windows, macOS and Linux Do you use xterm.js in your application as well? Please [open a Pull Request](https://github.com/sourcelair/xterm.js/pulls) to include it here. We would love to have it in our list. From 327b16ac2e038892a1bc1fa4ecf619f0f69f77d0 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 21:30:24 -0500 Subject: [PATCH 07/26] Add repeat polyfill for repeating a string --- src/utils/Generic.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/utils/Generic.ts b/src/utils/Generic.ts index 4bc6c487..9f9d2666 100644 --- a/src/utils/Generic.ts +++ b/src/utils/Generic.ts @@ -11,3 +11,31 @@ export function contains(arr: any[], el: any): boolean { return arr.indexOf(el) >= 0; } + +/** + * Returns a string repeated a given number of times + * Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat + * @param {Number} count The number of times to repeat the string + * @param {String} string The string that is to be repeated + */ +export function repeat(count: number, str: string): string { + if (count < 0) throw new RangeError('repeat count must be non-negative'); + if (count === Infinity) throw new RangeError('repeat count must be less than infinity'); + + count = Math.floor(count); + if (str.length === 0 || count === 0) return ''; + + // Ensuring count is a 31-bit integer allows us to heavily optimize the + // main part. But anyway, most current (August 2014) browsers can't handle + // strings 1 << 28 chars or longer, so: + if (str.length * count >= 1 << 28) { + throw new RangeError('repeat count must not overflow maximum string size'); + } + + let rpt = ''; + for (let i = 0; i < count; i++) { + rpt += str; + } + + return rpt; +} From 833d8144990393ec7e50a1ccbf78a6cce3dd4aa1 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 21:30:53 -0500 Subject: [PATCH 08/26] Refactor alt-click to work independently of which buffer is being used --- src/handlers/AltClickHandler.ts | 265 ++++++++++++++++++++++---------- 1 file changed, 182 insertions(+), 83 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 59962d96..829daf97 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -1,24 +1,32 @@ /** - * Alt+Click handler module: exports methods for handling all alt+click-related events in the - * terminal. + * Alt+Click handler module: exports methods for handling all alt+click-related + * events in the terminal. * @module xterm/handlers/AltClickHandler * @license MIT */ import { Terminal } from '../Terminal'; -import { CHAR_DATA_WIDTH_INDEX } from '../Buffer'; import { C0 } from '../EscapeSequences'; +import { repeat } from '../utils/Generic'; +import { CircularList } from '../utils/CircularList'; +import { LineData } from '../Types'; export class AltClickHandler { private _terminal: Terminal; - private _mouseRow: number; - private _mouseCol: number; + private _startRow: number; + private _startCol: number; + private _endRow: number; + private _endCol: number; + private _lines: CircularList; private _mouseEvent: MouseEvent; constructor(mouseEvent: MouseEvent, terminal: Terminal) { this._terminal = terminal; + this._lines = terminal.buffer.lines; + this._startCol = this._terminal.buffer.x; + this._startRow = this._terminal.buffer.y; - [this._mouseCol, this._mouseRow] = this._terminal.mouseHelper.getCoords( + [this._endCol, this._endRow] = this._terminal.mouseHelper.getCoords( (this._mouseEvent = mouseEvent), this._terminal.element, this._terminal.charMeasure, @@ -31,106 +39,197 @@ export class AltClickHandler { }); } + /** + * Writes the escape sequences of arrows to the terminal + */ public move(): void { - if (!this._mouseEvent.altKey) return; + if (this._mouseEvent.altKey) this._terminal.send(this._arrowSequences()); + } - let keyboardArrows; + /** + * Concatenates all the arrow sequences together. + * Resets the starting row to an unwrapped row, moves to the requested row, + * then moves to requested col. + */ + private _arrowSequences(): string { + return this._resetStartingRow() + + this._moveToRequestedRow() + + this._moveToRequestedCol(); + } - if (this._terminal.buffer === this._terminal.buffers.normal) { - keyboardArrows = this.buildArrowSequence(this.normalCharCount(), this.horizontalCursorCommand(this.normalMoveForward())); - } else { - let verticalChars = this.buildArrowSequence(this.altVerticalCharCount(), this.verticalCursorCommand(this.altMoveUpward())); - let horizontalChars = this.buildArrowSequence(this.altHorizontalCharCount(), this.horizontalCursorCommand(this.altMoveForward())); + /** + * If the initial position of the cursor is on a row that is wrapped, move the + * cursor up to the first row that is not wrapped to have accurate vertical + * positioning. + */ + private _resetStartingRow(): string { + return repeat(this._bufferLine( + this._startCol, this._startRow, this._startCol, + this._startRow - this._wrappedRowsForRow(this._startRow), false + ).length, this._colSequence(false)); + } - if (this.altMoveForward()) { - keyboardArrows = verticalChars + horizontalChars; - } else { - keyboardArrows = horizontalChars + verticalChars; + /** + * Using the reset starting and ending row, move to the requested row, + * ignoring wrapped rows + */ + private _moveToRequestedRow(): string { + let startRow = this._startRow - this._wrappedRowsForRow(this._startRow); + let endRow = this._endRow - this._wrappedRowsForRow(this._endRow); + + let rowsToMove = Math.abs(startRow - endRow) - this._wrappedRowsCount(); + + return repeat(rowsToMove, this._rowSequence(this._shouldMoveUp())); + } + + /** + * Move to the requested col on the ending row + */ + private _moveToRequestedCol(): string { + let startRow = this._endRow - this._wrappedRowsForRow(this._endRow); + let endRow = this._endRow; + let forward = this._shouldMoveForward(); + + return repeat(this._bufferLine( + this._startCol, startRow, this._endCol, endRow, forward + ).length, this._colSequence(forward)); + } + + /** + * Utility functions + */ + + /** + * Calculates the number of wrapped rows between the unwrapped starting and + * ending rows. These rows need to ignored since the cursor skips over them. + */ + private _wrappedRowsCount(): number { + let wrappedRows = 0; + let startRow = this._startRow - this._wrappedRowsForRow(this._startRow); + let endRow = this._endRow - this._wrappedRowsForRow(this._endRow); + + for (let i = 0; i < Math.abs(startRow - endRow); i++) { + let direction = this._shouldMoveUp() ? -1 : 1; + + if (this._lines.get(startRow + (direction * i)).isWrapped) wrappedRows++; + } + + return wrappedRows; + } + + /** + * Calculates the number of wrapped rows that make up a given row. + * @param currentRow The row to determine how many wrapped rows make it up + */ + private _wrappedRowsForRow(currentRow: number): number { + let rowCount = 0; + let lineWraps = this._lines.get(currentRow).isWrapped; + + while (lineWraps && currentRow >= 0 && currentRow < this._terminal.rows) { + rowCount++; + currentRow--; + lineWraps = this._lines.get(currentRow).isWrapped; + } + + return rowCount; + } + + /** + * Direction determiners + */ + + /** + * Determines if the right or left arrow is needed + */ + private _shouldMoveForward(): boolean { + let startRow = this._endRow - this._wrappedRowsForRow(this._endRow); + + return (this._startCol < this._endCol && + startRow <= this._endRow) || // down/right or same y/right + (this._startCol >= this._endCol && + startRow < this._endRow); // down/left or same y/left + } + + /** + * Determines if the up or down arrow is needed + */ + private _shouldMoveUp(): boolean { + return this._startRow > this._endRow; + } + + /** + * Constructs the string of chars in the buffer from a starting row and col + * to an ending row and col + * @param startCol The starting column position + * @param startRow The starting row position + * @param endCol The ending column position + * @param endRow The ending row position + * @param forward Direction to move + */ + private _bufferLine( + startCol: number, + startRow: number, + endCol: number, + endRow: number, + forward: boolean): string { + let currentRow = startCol; + let currentCol = startRow; + let bufferStr = ''; + + while (currentRow !== endCol || (currentCol !== endRow)) { + currentRow += forward ? 1 : -1; + + if (forward && currentRow > this._terminal.cols - 1) { + bufferStr += this._terminal.buffer.translateBufferLineToString( + currentCol, false, startCol, currentRow + ); + currentRow = 0; + startCol = 0; + currentCol++; + } else if (!forward && currentRow < 0) { + bufferStr += this._terminal.buffer.translateBufferLineToString( + currentCol, false, 0, startCol + 1 + ); + currentRow = this._terminal.cols - 1; + startCol = currentRow; + currentCol--; } } - this._terminal.send(keyboardArrows); + return bufferStr + this._terminal.buffer.translateBufferLineToString( + currentCol, false, startCol, currentRow + ); } - private buildArrowSequence(count: number, sequence: string): string { - return Array(count).join(sequence); - } + /** + * Arrow escape sequences + */ - private altMoveUpward(): boolean { - return this._terminal.buffer.y > this._mouseRow; - } - - private altMoveForward(): boolean { - return this._terminal.buffer.x < this._mouseCol; - } - - private normalMoveForward(): boolean { - return (this._terminal.buffer.x < this._mouseCol && - this._terminal.buffer.y <= this._mouseRow) || // down/right or same row/right - (this._terminal.buffer.x >= this._mouseCol && - this._terminal.buffer.y < this._mouseRow); // down/left or same row/left - } - - private horizontalCursorCommand(moveForward: boolean): string { + /** + * Constructs the escape sequence for the left or right arrow + * @param forward Right arrow or left arrow + */ + private _colSequence(forward: boolean): string { let mod = this._terminal.applicationCursor ? 'O' : '['; - if (moveForward) { + if (forward) { return C0.ESC + mod + 'C'; } else { return C0.ESC + mod + 'D'; } } - private verticalCursorCommand(moveUp: boolean): string { + /** + * Constructs the escape sequence for clicking the up or down arrow + * @param up Up arrow or down arrow + */ + private _rowSequence(up: boolean): string { let mod = this._terminal.applicationCursor ? 'O' : '['; - if (moveUp) { + if (up) { return C0.ESC + mod + 'A'; } else { return C0.ESC + mod + 'B'; } } - - private altVerticalCharCount(): number { - return Math.abs(this._terminal.buffer.y - this._mouseRow) + 1; - } - - private altHorizontalCharCount(): number { - return Math.abs(this._terminal.buffer.x - this._mouseCol) + 1; - } - - private normalCharCount(): number { - let currentX = this._terminal.buffer.x; - let currentY = this._terminal.buffer.y; - let startCol = this._terminal.buffer.x; - let bufferStr = ''; - - while (currentX !== this._mouseCol || (currentY !== this._mouseRow)) { - if (this.normalMoveForward()) { - currentX++; - if (currentX > this._terminal.cols - 1) { - bufferStr += this._terminal.buffer.translateBufferLineToString(currentY, false, startCol, currentX); - currentX = 0; - startCol = 0; - currentY++; - } - } else { - currentX--; - if (currentX < 0) { - bufferStr += this._terminal.buffer.translateBufferLineToString(currentY, false, 0, startCol + 1); - currentX = this._terminal.cols - 1; - startCol = currentX; - currentY--; - } - } - } - - if (this.normalMoveForward()) { - currentX++; - } else { - currentX--; - } - bufferStr += this._terminal.buffer.translateBufferLineToString(currentY, false, startCol, currentX); - return bufferStr.length; - } } From dc59f33ccf4a2075461285fff8fd5c1661bd7f57 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 22:06:31 -0500 Subject: [PATCH 09/26] Fix translateBufferToString bug --- src/Buffer.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 3e7d6122..9d47666f 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -217,7 +217,10 @@ export class Buffer implements IBuffer { // needed here because some chars are 0 characters long (eg. after wide // chars) and some chars are longer than 1 characters long (eg. emojis). let startIndex = startCol; - endCol = endCol || line.length; + // Only set endCol to the line length when it is null. 0 is a valid column. + if (endCol === null) { + endCol = line.length; + } let endIndex = endCol; for (let i = 0; i < line.length; i++) { From a4ee7236651564dcde2554c0968f5fae0c996126 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 22:06:58 -0500 Subject: [PATCH 10/26] Fix backward row/col definition --- src/handlers/AltClickHandler.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 829daf97..bddde6fb 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -172,32 +172,32 @@ export class AltClickHandler { endCol: number, endRow: number, forward: boolean): string { - let currentRow = startCol; - let currentCol = startRow; + let currentCol = startCol; + let currentRow = startRow; let bufferStr = ''; - while (currentRow !== endCol || (currentCol !== endRow)) { - currentRow += forward ? 1 : -1; + while (currentCol !== endCol || currentRow !== endRow) { + currentCol += forward ? 1 : -1; - if (forward && currentRow > this._terminal.cols - 1) { + if (forward && currentCol > this._terminal.cols - 1) { bufferStr += this._terminal.buffer.translateBufferLineToString( - currentCol, false, startCol, currentRow + currentRow, false, startCol, currentCol ); - currentRow = 0; + currentCol = 0; startCol = 0; - currentCol++; - } else if (!forward && currentRow < 0) { + currentRow++; + } else if (!forward && currentCol < 0) { bufferStr += this._terminal.buffer.translateBufferLineToString( - currentCol, false, 0, startCol + 1 + currentRow, false, 0, startCol + 1 ); - currentRow = this._terminal.cols - 1; - startCol = currentRow; - currentCol--; + currentCol = this._terminal.cols - 1; + startCol = currentCol; + currentRow--; } } return bufferStr + this._terminal.buffer.translateBufferLineToString( - currentCol, false, startCol, currentRow + currentRow, false, startCol, currentCol ); } From 6a48022a454aff1811bf6349f3389b07db5b81a8 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 22:28:41 -0500 Subject: [PATCH 11/26] Don't reset starting position if the row is not moved. Mainly due to the normal buffer --- src/handlers/AltClickHandler.ts | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index bddde6fb..94a1a7ec 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -63,10 +63,17 @@ export class AltClickHandler { * positioning. */ private _resetStartingRow(): string { - return repeat(this._bufferLine( - this._startCol, this._startRow, this._startCol, - this._startRow - this._wrappedRowsForRow(this._startRow), false - ).length, this._colSequence(false)); + let startRow = this._endRow - this._wrappedRowsForRow(this._endRow); + let endRow = this._endRow; + + if (this._moveToRequestedRow().length === 0) { + return "" + } else { + return repeat(this._bufferLine( + this._startCol, this._startRow, this._startCol, + this._startRow - this._wrappedRowsForRow(this._startRow), false + ).length, this._colSequence(false)); + } } /** @@ -86,7 +93,13 @@ export class AltClickHandler { * Move to the requested col on the ending row */ private _moveToRequestedCol(): string { - let startRow = this._endRow - this._wrappedRowsForRow(this._endRow); + let startRow; + if (this._moveToRequestedRow().length > 0) { + startRow = this._endRow - this._wrappedRowsForRow(this._endRow); + } else { + startRow = this._startRow; + } + let endRow = this._endRow; let forward = this._shouldMoveForward(); @@ -142,7 +155,12 @@ export class AltClickHandler { * Determines if the right or left arrow is needed */ private _shouldMoveForward(): boolean { - let startRow = this._endRow - this._wrappedRowsForRow(this._endRow); + let startRow; + if (this._moveToRequestedRow().length > 0) { + startRow = this._endRow - this._wrappedRowsForRow(this._endRow); + } else { + startRow = this._startRow; + } return (this._startCol < this._endCol && startRow <= this._endRow) || // down/right or same y/right From 544fa67851125413f84b110a22bb38ba3c243862 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 22:36:53 -0500 Subject: [PATCH 12/26] Move repeat out of generic --- src/handlers/AltClickHandler.ts | 28 ++++++++++++++++++++++++++++ src/utils/Generic.ts | 28 ---------------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 94a1a7ec..6955423b 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -251,3 +251,31 @@ export class AltClickHandler { } } } + +/** + * Returns a string repeated a given number of times + * Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat + * @param {Number} count The number of times to repeat the string + * @param {String} string The string that is to be repeated + */ +function repeat(count: number, str: string): string { + if (count < 0) throw new RangeError('repeat count must be non-negative'); + if (count === Infinity) throw new RangeError('repeat count must be less than infinity'); + + count = Math.floor(count); + if (str.length === 0 || count === 0) return ''; + + // Ensuring count is a 31-bit integer allows us to heavily optimize the + // main part. But anyway, most current (August 2014) browsers can't handle + // strings 1 << 28 chars or longer, so: + if (str.length * count >= 1 << 28) { + throw new RangeError('repeat count must not overflow maximum string size'); + } + + let rpt = ''; + for (let i = 0; i < count; i++) { + rpt += str; + } + + return rpt; +} diff --git a/src/utils/Generic.ts b/src/utils/Generic.ts index 9f9d2666..4bc6c487 100644 --- a/src/utils/Generic.ts +++ b/src/utils/Generic.ts @@ -11,31 +11,3 @@ export function contains(arr: any[], el: any): boolean { return arr.indexOf(el) >= 0; } - -/** - * Returns a string repeated a given number of times - * Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat - * @param {Number} count The number of times to repeat the string - * @param {String} string The string that is to be repeated - */ -export function repeat(count: number, str: string): string { - if (count < 0) throw new RangeError('repeat count must be non-negative'); - if (count === Infinity) throw new RangeError('repeat count must be less than infinity'); - - count = Math.floor(count); - if (str.length === 0 || count === 0) return ''; - - // Ensuring count is a 31-bit integer allows us to heavily optimize the - // main part. But anyway, most current (August 2014) browsers can't handle - // strings 1 << 28 chars or longer, so: - if (str.length * count >= 1 << 28) { - throw new RangeError('repeat count must not overflow maximum string size'); - } - - let rpt = ''; - for (let i = 0; i < count; i++) { - rpt += str; - } - - return rpt; -} From d61aa6e4706bc2b5c46051f5a80e8c90d7a7dda8 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 22:37:31 -0500 Subject: [PATCH 13/26] Fix lints --- src/handlers/AltClickHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 6955423b..eae491b8 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -67,7 +67,7 @@ export class AltClickHandler { let endRow = this._endRow; if (this._moveToRequestedRow().length === 0) { - return "" + return ''; } else { return repeat(this._bufferLine( this._startCol, this._startRow, this._startCol, From c41a4b5168ab9b3955b57438b4a7676340e92db2 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sat, 27 Jan 2018 22:37:57 -0500 Subject: [PATCH 14/26] Move repeat import --- src/handlers/AltClickHandler.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index eae491b8..bed9a863 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -7,7 +7,6 @@ import { Terminal } from '../Terminal'; import { C0 } from '../EscapeSequences'; -import { repeat } from '../utils/Generic'; import { CircularList } from '../utils/CircularList'; import { LineData } from '../Types'; From 65b6901170a0bea3a49491e63461ab3a29dae75d Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 27 Jan 2018 21:50:28 -0800 Subject: [PATCH 15/26] Fix compile error --- src/handlers/AltClickHandler.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index bed9a863..e52bd1a3 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -123,7 +123,9 @@ export class AltClickHandler { for (let i = 0; i < Math.abs(startRow - endRow); i++) { let direction = this._shouldMoveUp() ? -1 : 1; - if (this._lines.get(startRow + (direction * i)).isWrapped) wrappedRows++; + if ((this._lines.get(startRow + (direction * i))).isWrapped) { + wrappedRows++; + } } return wrappedRows; @@ -135,12 +137,12 @@ export class AltClickHandler { */ private _wrappedRowsForRow(currentRow: number): number { let rowCount = 0; - let lineWraps = this._lines.get(currentRow).isWrapped; + let lineWraps = (this._lines.get(currentRow)).isWrapped; while (lineWraps && currentRow >= 0 && currentRow < this._terminal.rows) { rowCount++; currentRow--; - lineWraps = this._lines.get(currentRow).isWrapped; + lineWraps = (this._lines.get(currentRow)).isWrapped; } return rowCount; From edadb6e0f1941b8c05b0eebd1dcc879948a7a7d9 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 10:44:01 -0500 Subject: [PATCH 16/26] Update the copyright header --- src/handlers/AltClickHandler.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index e52bd1a3..471977dd 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -1,7 +1,5 @@ /** - * Alt+Click handler module: exports methods for handling all alt+click-related - * events in the terminal. - * @module xterm/handlers/AltClickHandler + * Copyright (c) 2017 The xterm.js authors. All rights reserved. * @license MIT */ From 0202659c389c84009ca6998b7a8fc80b22276cc5 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 10:46:48 -0500 Subject: [PATCH 17/26] Set terminal and mouseevent inside constructor args --- src/handlers/AltClickHandler.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 471977dd..9cfda3ec 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -9,22 +9,19 @@ import { CircularList } from '../utils/CircularList'; import { LineData } from '../Types'; export class AltClickHandler { - private _terminal: Terminal; private _startRow: number; private _startCol: number; private _endRow: number; private _endCol: number; private _lines: CircularList; - private _mouseEvent: MouseEvent; - constructor(mouseEvent: MouseEvent, terminal: Terminal) { - this._terminal = terminal; - this._lines = terminal.buffer.lines; + constructor(private _mouseEvent: MouseEvent, private _terminal: Terminal) { + this._lines = this._terminal.buffer.lines; this._startCol = this._terminal.buffer.x; this._startRow = this._terminal.buffer.y; [this._endCol, this._endRow] = this._terminal.mouseHelper.getCoords( - (this._mouseEvent = mouseEvent), + this._mouseEvent, this._terminal.element, this._terminal.charMeasure, this._terminal.options.lineHeight, From 08af2ebeea472bee19b9d0509710eaa9cfb5ee54 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 12:29:52 -0500 Subject: [PATCH 18/26] Utilize enum to simplify sequence generation --- src/handlers/AltClickHandler.ts | 73 +++++++++++++++------------------ 1 file changed, 33 insertions(+), 40 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 9cfda3ec..c6703485 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -8,6 +8,13 @@ import { C0 } from '../EscapeSequences'; import { CircularList } from '../utils/CircularList'; import { LineData } from '../Types'; +enum Direction { + Up = 'A', + Down = 'B', + Right = 'C', + Left = 'D' +}; + export class AltClickHandler { private _startRow: number; private _startCol: number; @@ -66,7 +73,7 @@ export class AltClickHandler { return repeat(this._bufferLine( this._startCol, this._startRow, this._startCol, this._startRow - this._wrappedRowsForRow(this._startRow), false - ).length, this._colSequence(false)); + ).length, this._sequence(Direction.Left)); } } @@ -80,7 +87,7 @@ export class AltClickHandler { let rowsToMove = Math.abs(startRow - endRow) - this._wrappedRowsCount(); - return repeat(rowsToMove, this._rowSequence(this._shouldMoveUp())); + return repeat(rowsToMove, this._sequence(this._verticalDirection())); } /** @@ -95,11 +102,12 @@ export class AltClickHandler { } let endRow = this._endRow; - let forward = this._shouldMoveForward(); + let direction = this._horizontalDirection(); return repeat(this._bufferLine( - this._startCol, startRow, this._endCol, endRow, forward - ).length, this._colSequence(forward)); + this._startCol, startRow, this._endCol, endRow, + direction === Direction.Right + ).length, this._sequence(direction)); } /** @@ -116,7 +124,7 @@ export class AltClickHandler { let endRow = this._endRow - this._wrappedRowsForRow(this._endRow); for (let i = 0; i < Math.abs(startRow - endRow); i++) { - let direction = this._shouldMoveUp() ? -1 : 1; + let direction = this._verticalDirection() === Direction.Up ? -1 : 1; if ((this._lines.get(startRow + (direction * i))).isWrapped) { wrappedRows++; @@ -150,7 +158,7 @@ export class AltClickHandler { /** * Determines if the right or left arrow is needed */ - private _shouldMoveForward(): boolean { + private _horizontalDirection(): Direction { let startRow; if (this._moveToRequestedRow().length > 0) { startRow = this._endRow - this._wrappedRowsForRow(this._endRow); @@ -158,17 +166,25 @@ export class AltClickHandler { startRow = this._startRow; } - return (this._startCol < this._endCol && + if ((this._startCol < this._endCol && startRow <= this._endRow) || // down/right or same y/right (this._startCol >= this._endCol && - startRow < this._endRow); // down/left or same y/left + startRow < this._endRow)) { // down/left or same y/left + return Direction.Right; + } else { + return Direction.Left; + } } /** * Determines if the up or down arrow is needed */ - private _shouldMoveUp(): boolean { - return this._startRow > this._endRow; + private _verticalDirection(): Direction { + if (this._startRow > this._endRow) { + return Direction.Up; + } else { + return Direction.Down; + } } /** @@ -216,36 +232,13 @@ export class AltClickHandler { } /** - * Arrow escape sequences + * Constructs the escape sequence for clicking an arrow + * @param direction The direction to move */ - - /** - * Constructs the escape sequence for the left or right arrow - * @param forward Right arrow or left arrow - */ - private _colSequence(forward: boolean): string { - let mod = this._terminal.applicationCursor ? 'O' : '['; - - if (forward) { - return C0.ESC + mod + 'C'; - } else { - return C0.ESC + mod + 'D'; - } - } - - /** - * Constructs the escape sequence for clicking the up or down arrow - * @param up Up arrow or down arrow - */ - private _rowSequence(up: boolean): string { - let mod = this._terminal.applicationCursor ? 'O' : '['; - - if (up) { - return C0.ESC + mod + 'A'; - } else { - return C0.ESC + mod + 'B'; - } - } + private _sequence(direction: Direction): string { + const mod = this._terminal.applicationCursor ? 'O' : '['; + return C0.ESC + mod + direction; + } } /** From aeae40e1105f722a0b5e8a38d9c9884e1d2074f5 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 12:38:20 -0500 Subject: [PATCH 19/26] Use the terminal and circular list interface --- src/Types.ts | 3 ++- src/handlers/AltClickHandler.ts | 7 +++---- src/utils/TestUtils.test.ts | 4 ++++ 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Types.ts b/src/Types.ts index f3e9bb94..df52dd8a 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -188,12 +188,13 @@ export interface ITerminal extends PublicTerminal, IElementAccessor, IBufferAcce isFocused: boolean; mouseHelper: IMouseHelper; bracketedPasteMode: boolean; - + applicationCursor: boolean; /** * Emit the 'data' event and populate the given data. * @param data The data to populate in the event. */ handler(data: string): void; + send(data: string): void; scrollLines(disp: number, suppressScrollEvent?: boolean): void; cancel(ev: Event, force?: boolean): boolean | void; log(text: string): void; diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index c6703485..ccdec7ae 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -3,9 +3,8 @@ * @license MIT */ -import { Terminal } from '../Terminal'; +import { ITerminal, ICircularList } from '../Types'; import { C0 } from '../EscapeSequences'; -import { CircularList } from '../utils/CircularList'; import { LineData } from '../Types'; enum Direction { @@ -20,9 +19,9 @@ export class AltClickHandler { private _startCol: number; private _endRow: number; private _endCol: number; - private _lines: CircularList; + private _lines: ICircularList; - constructor(private _mouseEvent: MouseEvent, private _terminal: Terminal) { + constructor(private _mouseEvent: MouseEvent, private _terminal: ITerminal) { this._lines = this._terminal.buffer.lines; this._startCol = this._terminal.buffer.x; this._startRow = this._terminal.buffer.y; diff --git a/src/utils/TestUtils.test.ts b/src/utils/TestUtils.test.ts index 0744cd14..d53c8c58 100644 --- a/src/utils/TestUtils.test.ts +++ b/src/utils/TestUtils.test.ts @@ -70,6 +70,9 @@ export class MockTerminal implements ITerminal { write(data: string): void { throw new Error('Method not implemented.'); } + send(data: string): void { + throw new Error('Method not implemented.'); + } bracketedPasteMode: boolean; mouseHelper: IMouseHelper; renderer: IRenderer; @@ -93,6 +96,7 @@ export class MockTerminal implements ITerminal { scrollback: number; buffers: IBufferSet; buffer: IBuffer; + applicationCursor: boolean; handler(data: string): void { throw new Error('Method not implemented.'); } From e60704255f518a9feff106885849147011fe0df5 Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 13:00:11 -0500 Subject: [PATCH 20/26] Fix lints --- src/handlers/AltClickHandler.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index ccdec7ae..307fa14a 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -3,16 +3,15 @@ * @license MIT */ -import { ITerminal, ICircularList } from '../Types'; +import { ITerminal, ICircularList, LineData } from '../Types'; import { C0 } from '../EscapeSequences'; -import { LineData } from '../Types'; enum Direction { Up = 'A', Down = 'B', Right = 'C', Left = 'D' -}; +} export class AltClickHandler { private _startRow: number; From f726c1fd0d065fa6b43e5b14d7c7194bf754ebca Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 13:06:54 -0500 Subject: [PATCH 21/26] Allow selection and alt click to live conflict free --- src/SelectionManager.ts | 11 ++++++++++- src/Terminal.ts | 2 -- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 10612c5c..75ada79a 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -11,6 +11,7 @@ import { CircularList } from './utils/CircularList'; import { EventEmitter } from './EventEmitter'; import { SelectionModel } from './SelectionModel'; import { CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CHAR_INDEX } from './Buffer'; +import { AltClickHandler } from './handlers/AltClickHandler'; /** * The number of pixels the mouse needs to be above or below the viewport in @@ -96,6 +97,8 @@ export class SelectionManager extends EventEmitter implements ISelectionManager private _mouseUpListener: EventListener; private _trimListener: (...args: any[]) => void; + private _mouseDownTimeStamp: number; + constructor( private _terminal: ITerminal, private _charMeasure: CharMeasure @@ -317,6 +320,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager * @param event The mousedown event. */ public onMouseDown(event: MouseEvent): void { + this._mouseDownTimeStamp = event.timeStamp; // If we have selection, we want the context menu on right click even if the // terminal is in mouse mode. if (event.button === 2 && this.hasSelection) { @@ -536,10 +540,15 @@ export class SelectionManager extends EventEmitter implements ISelectionManager * @param event The mouseup event. */ private _onMouseUp(event: MouseEvent): void { + let timeElapsed = event.timeStamp - this._mouseDownTimeStamp; + this._removeMouseDownListeners(); - if (this.hasSelection) + if (this.selectionText.length <= 1 && timeElapsed < 500) { + (new AltClickHandler(event, this._terminal)).move(); + } else if (this.hasSelection) { this._terminal.emit('selection'); + } } private _onBufferActivate(e: {activeBuffer: IBuffer, inactiveBuffer: IBuffer}): void { diff --git a/src/Terminal.ts b/src/Terminal.ts index 2977154e..5536cf03 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -30,7 +30,6 @@ import { CompositionHelper } from './CompositionHelper'; import { EventEmitter } from './EventEmitter'; import { Viewport } from './Viewport'; import { rightClickHandler, moveTextAreaUnderMouseCursor, pasteHandler, copyHandler } from './handlers/Clipboard'; -import { AltClickHandler } from './handlers/AltClickHandler'; import { CircularList } from './utils/CircularList'; import { C0 } from './EscapeSequences'; import { InputHandler } from './InputHandler'; @@ -953,7 +952,6 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT // if the selection manager is having selection forced (ie. a modifier is // held). if (!this.mouseEvents || this.selectionManager.shouldForceSelection(ev)) { - (new AltClickHandler(ev, this)).move(); return; } From 3313716c87f9c1a25bf8d37c0895503ed0ca276c Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 13:15:42 -0500 Subject: [PATCH 22/26] Add new lines back in types --- src/Types.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Types.ts b/src/Types.ts index df52dd8a..da3cc9d2 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -189,6 +189,7 @@ export interface ITerminal extends PublicTerminal, IElementAccessor, IBufferAcce mouseHelper: IMouseHelper; bracketedPasteMode: boolean; applicationCursor: boolean; + /** * Emit the 'data' event and populate the given data. * @param data The data to populate in the event. From 3bdc069f1961d7c78773ad099173db97d754d60a Mon Sep 17 00:00:00 2001 From: npezza93 Date: Sun, 28 Jan 2018 13:16:20 -0500 Subject: [PATCH 23/26] Set copyright header to 2018 --- src/handlers/AltClickHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index 307fa14a..e0943883 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.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 */ From af375eadb4a14dc5c1b65414e77e9bc774c8cc60 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 28 Jan 2018 12:27:01 -0800 Subject: [PATCH 24/26] Run tsc as a travis job This will hard fail when there are semantic errors. Currently building does not do this and we will merge these in as a result. --- .travis.yml | 1 + package.json | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 7a37af1e..c41ebf79 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ before_install: - npm install -g npm@5.1.0 env: matrix: + - NPM_COMMAND=tsc - NPM_COMMAND=lint - NPM_COMMAND=test notifications: diff --git a/package.json b/package.json index cf3df57b..ba988d61 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "lint": "tslint src/*.ts src/**/*.ts", "test": "gulp test", "build:docs": "jsdoc -c jsdoc.json", + "tsc": "tsc", "build": "gulp build", "prepublish": "npm run build", "coveralls": "gulp coveralls", From e8666cafc92a21802526c3d9883703ba4ae7d373 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 28 Jan 2018 14:44:33 -0800 Subject: [PATCH 25/26] Small clean up --- src/SelectionManager.ts | 8 ++++++- src/handlers/AltClickHandler.ts | 37 +++++++++++++-------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 75ada79a..3c927839 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -29,6 +29,12 @@ const DRAG_SCROLL_MAX_SPEED = 15; */ const DRAG_SCROLL_INTERVAL = 50; +/** + * The maximum amount of time that can have elapsed for an alt click to move the + * cursor. + */ +const ALT_CLICK_MOVE_CURSOR_TIME = 500; + /** * A string containing all characters that are considered word separated by the * double click to select work logic. @@ -544,7 +550,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager this._removeMouseDownListeners(); - if (this.selectionText.length <= 1 && timeElapsed < 500) { + if (this.selectionText.length <= 1 && timeElapsed < ALT_CLICK_MOVE_CURSOR_TIME) { (new AltClickHandler(event, this._terminal)).move(); } else if (this.hasSelection) { this._terminal.emit('selection'); diff --git a/src/handlers/AltClickHandler.ts b/src/handlers/AltClickHandler.ts index e0943883..c9c51cbe 100644 --- a/src/handlers/AltClickHandler.ts +++ b/src/handlers/AltClickHandler.ts @@ -20,7 +20,10 @@ export class AltClickHandler { private _endCol: number; private _lines: ICircularList; - constructor(private _mouseEvent: MouseEvent, private _terminal: ITerminal) { + constructor( + private _mouseEvent: MouseEvent, + private _terminal: ITerminal + ) { this._lines = this._terminal.buffer.lines; this._startCol = this._terminal.buffer.x; this._startRow = this._terminal.buffer.y; @@ -42,7 +45,9 @@ export class AltClickHandler { * Writes the escape sequences of arrows to the terminal */ public move(): void { - if (this._mouseEvent.altKey) this._terminal.send(this._arrowSequences()); + if (this._mouseEvent.altKey) { + this._terminal.send(this._arrowSequences()); + } } /** @@ -199,7 +204,8 @@ export class AltClickHandler { startRow: number, endCol: number, endRow: number, - forward: boolean): string { + forward: boolean + ): string { let currentCol = startCol; let currentRow = startRow; let bufferStr = ''; @@ -233,36 +239,23 @@ export class AltClickHandler { * Constructs the escape sequence for clicking an arrow * @param direction The direction to move */ - private _sequence(direction: Direction): string { - const mod = this._terminal.applicationCursor ? 'O' : '['; - return C0.ESC + mod + direction; - } + private _sequence(direction: Direction): string { + const mod = this._terminal.applicationCursor ? 'O' : '['; + return C0.ESC + mod + direction; + } } /** * Returns a string repeated a given number of times * Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat - * @param {Number} count The number of times to repeat the string - * @param {String} string The string that is to be repeated + * @param count The number of times to repeat the string + * @param string The string that is to be repeated */ function repeat(count: number, str: string): string { - if (count < 0) throw new RangeError('repeat count must be non-negative'); - if (count === Infinity) throw new RangeError('repeat count must be less than infinity'); - count = Math.floor(count); - if (str.length === 0 || count === 0) return ''; - - // Ensuring count is a 31-bit integer allows us to heavily optimize the - // main part. But anyway, most current (August 2014) browsers can't handle - // strings 1 << 28 chars or longer, so: - if (str.length * count >= 1 << 28) { - throw new RangeError('repeat count must not overflow maximum string size'); - } - let rpt = ''; for (let i = 0; i < count; i++) { rpt += str; } - return rpt; } From 69ebb3e53e3ee8cfe35ab57844850a39b70a8503 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Mon, 29 Jan 2018 07:42:59 -0800 Subject: [PATCH 26/26] Fix recent typo in comment --- src/renderer/BaseRenderLayer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 35e1a57e..9423f65d 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -65,7 +65,7 @@ export abstract class BaseRenderLayer implements IRenderLayer { // Create new canvas and replace old one const oldCanvas = this._canvas; this._alpha = alpha; - // Closing preserves properties + // Cloning preserves properties this._canvas = this._canvas.cloneNode(); this._initCanvas(); this._container.replaceChild(this._canvas, oldCanvas);