5 import { ITerminal } from './Interfaces';
7 export class SelectionModel {
9 * Whether select all is currently active.
11 public isSelectAllActive: boolean;
14 * The [x, y] position the selection starts at.
16 public selectionStart: [number, number];
19 * The minimal length of the selection from the start position. When double
20 * clicking on a word, the word will be selected which makes the selection
21 * start at the start of the word and makes this variable the length.
23 public selectionStartLength: number;
26 * The [x, y] position the selection ends at.
28 public selectionEnd: [number, number];
31 private _terminal: ITerminal
33 this.clearSelection();
37 * Clears the current selection.
39 public clearSelection(): void {
40 this.selectionStart = null;
41 this.selectionEnd = null;
42 this.isSelectAllActive = false;
43 this.selectionStartLength = 0;
47 * The final selection start, taking into consideration select all.
49 public get finalSelectionStart(): [number, number] {
50 if (this.isSelectAllActive) {
54 if (!this.selectionEnd || !this.selectionStart) {
55 return this.selectionStart;
58 return this._areSelectionValuesReversed() ? this.selectionEnd : this.selectionStart;
62 * The final selection end, taking into consideration select all, double click
63 * word selection and triple click line selection.
65 public get finalSelectionEnd(): [number, number] {
66 if (this.isSelectAllActive) {
67 return [this._terminal.cols, this._terminal.ybase + this._terminal.rows - 1];
70 if (!this.selectionStart) {
74 // Use the selection start if the end doesn't exist or they're reversed
75 if (!this.selectionEnd || this._areSelectionValuesReversed()) {
76 if (this.selectionStartLength === 0) {
79 return [this.selectionStart[0] + this.selectionStartLength, this.selectionStart[1]];
82 // Ensure the the word/line is selected after a double/triple click
83 if (this.selectionStartLength) {
84 // Select the larger of the two when start and end are on the same line
85 if (this.selectionEnd[1] === this.selectionStart[1]) {
86 return [Math.max(this.selectionStart[0] + this.selectionStartLength, this.selectionEnd[0]), this.selectionEnd[1]];
89 return this.selectionEnd;
93 * Returns whether the selection start and end are reversed.
95 protected _areSelectionValuesReversed(): boolean {
96 const start = this.selectionStart;
97 const end = this.selectionEnd;
98 return start[1] > end[1] || (start[1] === end[1] && start[0] > end[0]);
102 * Handle the buffer being trimmed, adjust the selection position.
103 * @param amount The amount the buffer is being trimmed.
104 * @return Whether a refresh is necessary.
106 public onTrim(amount: number): boolean {
107 // Adjust the selection position based on the trimmed amount.
108 if (this.selectionStart) {
109 this.selectionStart[1] -= amount;
111 if (this.selectionEnd) {
112 this.selectionEnd[1] -= amount;
115 // The selection has moved off the buffer, clear it.
116 if (this.selectionEnd && this.selectionEnd[1] < 0) {
117 this.clearSelection();
121 // If the selection start is trimmed, ensure the start column is 0.
122 if (this.selectionStart && this.selectionStart[1] < 0) {
123 this.selectionStart[1] = 0;