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.
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;
}
/**
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;
}
/**
* 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]];
}
/**
* 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]);
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;