]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/SelectionManager.ts
fixes #799 - lookup table is created on first wcwidth call for char > 127
[mirror_xterm.js.git] / src / SelectionManager.ts
index 4440ea4f4eb00215162004e373e5df0119fb52de..31ee8c2c8f677e6b34ee8ba2bade1680b43abd3b 100644 (file)
@@ -9,6 +9,7 @@ import { CircularList } from './utils/CircularList';
 import { EventEmitter } from './EventEmitter';
 import { ITerminal } from './Interfaces';
 import { SelectionModel } from './SelectionModel';
+import { translateBufferLineToString } from './utils/BufferLine';
 
 /**
  * The number of pixels the mouse needs to be above or below the viewport in
@@ -26,18 +27,6 @@ const DRAG_SCROLL_MAX_SPEED = 15;
  */
 const DRAG_SCROLL_INTERVAL = 50;
 
-/**
- * The amount of time before mousedown events are no longer stacked to create
- * double/triple click events.
- */
-const CLEAR_MOUSE_DOWN_TIME = 400;
-
-/**
- * The number of pixels in each direction that the mouse must move before
- * mousedown events are no longer stacked to create double/triple click events.
- */
-const CLEAR_MOUSE_DISTANCE = 10;
-
 /**
  * A string containing all characters that are considered word separated by the
  * double click to select work logic.
@@ -163,6 +152,9 @@ export class SelectionManager extends EventEmitter {
     this.clearSelection();
   }
 
+  public get selectionStart(): [number, number] { return this._model.finalSelectionStart; }
+  public get selectionEnd(): [number, number] { return this._model.finalSelectionEnd; }
+
   /**
    * Gets whether there is an active text selection.
    */
@@ -188,12 +180,12 @@ export class SelectionManager extends EventEmitter {
     // Get first row
     const startRowEndCol = start[1] === end[1] ? end[0] : null;
     let result: string[] = [];
-    result.push(this._translateBufferLineToString(this._buffer.get(start[1]), true, start[0], startRowEndCol));
+    result.push(translateBufferLineToString(this._buffer.get(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 = this._translateBufferLineToString(bufferLine, true);
+      const lineText = translateBufferLineToString(bufferLine, true);
       if (bufferLine.isWrapped) {
         result[result.length - 1] += lineText;
       } else {
@@ -204,7 +196,7 @@ export class SelectionManager extends EventEmitter {
     // Get final row
     if (start[1] !== end[1]) {
       const bufferLine = this._buffer.get(end[1]);
-      const lineText = this._translateBufferLineToString(bufferLine, true, 0, end[0]);
+      const lineText = translateBufferLineToString(bufferLine, true, 0, end[0]);
       if (bufferLine.isWrapped) {
         result[result.length - 1] += lineText;
       } else {
@@ -230,55 +222,6 @@ export class SelectionManager extends EventEmitter {
     this.refresh();
   }
 
-  /**
-   * 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.
-   */
-  private _translateBufferLineToString(line: any, trimRight: boolean, startCol: number = 0, endCol: number = null): string {
-    // TODO: This function should live in a buffer or buffer line class
-
-    // Get full line
-    let lineString = '';
-    let widthAdjustedStartCol = startCol;
-    let widthAdjustedEndCol = endCol;
-    for (let i = 0; i < line.length; i++) {
-      const char = line[i];
-      lineString += char[LINE_DATA_CHAR_INDEX];
-      // Adjust start and end cols for wide characters if they affect their
-      // column indexes
-      if (char[LINE_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);
-  }
-
   /**
    * Queues a refresh, redrawing the selection on the next opportunity.
    * @param isNewSelection Whether the selection should be registered as a new
@@ -565,13 +508,21 @@ export class SelectionManager extends EventEmitter {
     return charIndex;
   }
 
+  public setSelection(col: number, row: number, length: number): void {
+    this._model.clearSelection();
+    this._removeMouseDownListeners();
+    this._model.selectionStart = [col, row];
+    this._model.selectionStartLength = length;
+    this.refresh();
+  }
+
   /**
    * Gets positional information for the word at the coordinated specified.
    * @param coords The coordinates to get the word at.
    */
   private _getWordAt(coords: [number, number]): IWordPosition {
     const bufferLine = this._buffer.get(coords[1]);
-    const line = this._translateBufferLineToString(bufferLine, false);
+    const line = translateBufferLineToString(bufferLine, false);
 
     // Get actual index, taking into consideration wide characters
     let endIndex = this._convertViewportColToCharacterIndex(bufferLine, coords);