5 import { ITerminal } from './Interfaces';
8 * Represents a selection within the buffer. This model only cares about column
9 * and row coordinates, not wide characters.
11 export class SelectionModel {
13 * Whether select all is currently active.
15 public isSelectAllActive: boolean;
18 * The [x, y] position the selection starts at.
20 public selectionStart: [number, number];
23 * The minimal length of the selection from the start position. When double
24 * clicking on a word, the word will be selected which makes the selection
25 * start at the start of the word and makes this variable the length.
27 public selectionStartLength: number;
30 * The [x, y] position the selection ends at.
32 public selectionEnd: [number, number];
35 private _terminal: ITerminal
37 this.clearSelection();
41 * Clears the current selection.
43 public clearSelection(): void {
44 this.selectionStart = null;
45 this.selectionEnd = null;
46 this.isSelectAllActive = false;
47 this.selectionStartLength = 0;
51 * The final selection start, taking into consideration select all.
53 public get finalSelectionStart(): [number, number] {
54 if (this.isSelectAllActive) {
58 if (!this.selectionEnd || !this.selectionStart) {
59 return this.selectionStart;
62 return this.areSelectionValuesReversed() ? this.selectionEnd : this.selectionStart;
66 * The final selection end, taking into consideration select all, double click
67 * word selection and triple click line selection.
69 public get finalSelectionEnd(): [number, number] {
70 if (this.isSelectAllActive) {
71 return [this._terminal.cols, this._terminal.buffer.ybase + this._terminal.rows - 1];
74 if (!this.selectionStart) {
78 // Use the selection start if the end doesn't exist or they're reversed
79 if (!this.selectionEnd || this.areSelectionValuesReversed()) {
80 return [this.selectionStart[0] + this.selectionStartLength, this.selectionStart[1]];
83 // Ensure the the word/line is selected after a double/triple click
84 if (this.selectionStartLength) {
85 // Select the larger of the two when start and end are on the same line
86 if (this.selectionEnd[1] === this.selectionStart[1]) {
87 return [Math.max(this.selectionStart[0] + this.selectionStartLength, this.selectionEnd[0]), this.selectionEnd[1]];
90 return this.selectionEnd;
94 * Returns whether the selection start and end are reversed.
96 public areSelectionValuesReversed(): boolean {
97 const start = this.selectionStart;
98 const end = this.selectionEnd;
99 return start[1] > end[1] || (start[1] === end[1] && start[0] > end[0]);
103 * Handle the buffer being trimmed, adjust the selection position.
104 * @param amount The amount the buffer is being trimmed.
105 * @return Whether a refresh is necessary.
107 public onTrim(amount: number): boolean {
108 // Adjust the selection position based on the trimmed amount.
109 if (this.selectionStart) {
110 this.selectionStart[1] -= amount;
112 if (this.selectionEnd) {
113 this.selectionEnd[1] -= amount;
116 // The selection has moved off the buffer, clear it.
117 if (this.selectionEnd && this.selectionEnd[1] < 0) {
118 this.clearSelection();
122 // If the selection start is trimmed, ensure the start column is 0.
123 if (this.selectionStart && this.selectionStart[1] < 0) {
124 this.selectionStart[1] = 0;