]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/SelectionModel.ts
Little code clean up.
[mirror_xterm.js.git] / src / SelectionModel.ts
index bcb7a9ced60aa6718a2765a004e7c57abb81e8d2..410af3b3a681b96c5b9261b5b90ae144171dc60c 100644 (file)
@@ -4,6 +4,10 @@
 
 import { ITerminal } from './Interfaces';
 
+/**
+ * Represents a selection within the buffer. This model only cares about column
+ * and row coordinates, not wide characters.
+ */
 export class SelectionModel {
   /**
    * Whether select all is currently active.
@@ -30,6 +34,17 @@ export class SelectionModel {
   constructor(
     private _terminal: ITerminal
   ) {
+    this.clearSelection();
+  }
+
+  /**
+   * Clears the current selection.
+   */
+  public clearSelection(): void {
+    this.selectionStart = null;
+    this.selectionEnd = null;
+    this.isSelectAllActive = false;
+    this.selectionStartLength = 0;
   }
 
   /**
@@ -40,11 +55,11 @@ export class SelectionModel {
       return [0, 0];
     }
 
-    if (!this.selectionEnd) {
+    if (!this.selectionEnd || !this.selectionStart) {
       return this.selectionStart;
     }
 
-    return this._areSelectionValuesReversed() ? this.selectionEnd : this.selectionStart;
+    return this.areSelectionValuesReversed() ? this.selectionEnd : this.selectionStart;
   }
 
   /**
@@ -52,16 +67,16 @@ export class SelectionModel {
    * word selection and triple click line selection.
    */
   public get finalSelectionEnd(): [number, number] {
-    if (!this.selectionStart) {
-      return null;
+    if (this.isSelectAllActive) {
+      return [this._terminal.cols, this._terminal.ybase + this._terminal.rows - 1];
     }
 
-    if (this.isSelectAllActive) {
-      return [this._terminal.cols - 1, this._terminal.ydisp + this._terminal.rows - 1];
+    if (!this.selectionStart) {
+      return null;
     }
 
     // Use the selection start if the end doesn't exist or they're reversed
-    if (!this.selectionEnd || this._areSelectionValuesReversed()) {
+    if (!this.selectionEnd || this.areSelectionValuesReversed()) {
       return [this.selectionStart[0] + this.selectionStartLength, this.selectionStart[1]];
     }
 
@@ -78,7 +93,7 @@ export class SelectionModel {
   /**
    * Returns whether the selection start and end are reversed.
    */
-  private _areSelectionValuesReversed(): boolean {
+  public areSelectionValuesReversed(): boolean {
     const start = this.selectionStart;
     const end = this.selectionEnd;
     return start[1] > end[1] || (start[1] === end[1] && start[0] > end[0]);
@@ -92,21 +107,20 @@ export class SelectionModel {
   public onTrim(amount: number): boolean {
     // Adjust the selection position based on the trimmed amount.
     if (this.selectionStart) {
-      this.selectionStart[0] -= amount;
+      this.selectionStart[1] -= amount;
     }
     if (this.selectionEnd) {
-      this.selectionEnd[0] -= amount;
+      this.selectionEnd[1] -= amount;
     }
 
     // The selection has moved off the buffer, clear it.
-    if (this.selectionEnd && this.selectionEnd[0] < 0) {
-      this.selectionStart = null;
-      this.selectionEnd = null;
+    if (this.selectionEnd && this.selectionEnd[1] < 0) {
+      this.clearSelection();
       return true;
     }
 
     // If the selection start is trimmed, ensure the start column is 0.
-    if (this.selectionStart && this.selectionStart[0] < 0) {
+    if (this.selectionStart && this.selectionStart[1] < 0) {
       this.selectionStart[1] = 0;
     }
     return false;