terminalCommandTracker.ts 7.7 KB
Newer Older
D
Daniel Imms 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { Terminal, IMarker } from 'vscode-xterm';
import { ITerminalCommandTracker } from 'vs/workbench/parts/terminal/common/terminal';
8
import { IDisposable } from 'vs/base/common/lifecycle';
D
Daniel Imms 已提交
9 10 11 12 13 14 15 16 17 18 19

/**
 * The minimize size of the prompt in which to assume the line is a command.
 */
const MINIMUM_PROMPT_LENGTH = 2;

enum Boundary {
	Top,
	Bottom
}

20 21 22 23 24
export enum ScrollPosition {
	Top,
	Middle
}

25
export class TerminalCommandTracker implements ITerminalCommandTracker, IDisposable {
D
Daniel Imms 已提交
26 27
	private _currentMarker: IMarker | Boundary = Boundary.Bottom;
	private _selectionStart: IMarker | Boundary | null = null;
28
	private _isDisposable: boolean = false;
D
Daniel Imms 已提交
29 30 31 32 33 34 35

	constructor(
		private _xterm: Terminal
	) {
		this._xterm.on('key', key => this._onKey(key));
	}

36 37 38 39
	public dispose(): void {
		this._xterm = null;
	}

D
Daniel Imms 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	private _onKey(key: string): void {
		if (key === '\x0d') {
			this._onEnter();
		}

		// Clear the current marker so successive focus/selection actions are performed from the
		// bottom of the buffer
		this._currentMarker = Boundary.Bottom;
		this._selectionStart = null;
	}

	private _onEnter(): void {
		if (this._xterm.buffer.x >= MINIMUM_PROMPT_LENGTH) {
			this._xterm.addMarker(0);
		}
	}

57
	public scrollToPreviousCommand(scrollPosition: ScrollPosition = ScrollPosition.Top, retainSelection: boolean = false): void {
D
Daniel Imms 已提交
58 59 60 61 62 63 64 65 66
		if (!retainSelection) {
			this._selectionStart = null;
		}

		let markerIndex;
		if (this._currentMarker === Boundary.Bottom) {
			markerIndex = this._xterm.markers.length - 1;
		} else if (this._currentMarker === Boundary.Top) {
			markerIndex = -1;
67 68 69 70
		} else if (this._isDisposable) {
			markerIndex = this._findPreviousCommand();
			this._currentMarker.dispose();
			this._isDisposable = false;
D
Daniel Imms 已提交
71 72 73 74 75 76 77 78 79 80 81
		} else {
			markerIndex = this._xterm.markers.indexOf(this._currentMarker) - 1;
		}

		if (markerIndex < 0) {
			this._currentMarker = Boundary.Top;
			this._xterm.scrollToTop();
			return;
		}

		this._currentMarker = this._xterm.markers[markerIndex];
82
		this._scrollToMarker(this._currentMarker, scrollPosition);
D
Daniel Imms 已提交
83 84
	}

85
	public scrollToNextCommand(scrollPosition: ScrollPosition = ScrollPosition.Top, retainSelection: boolean = false): void {
D
Daniel Imms 已提交
86 87 88 89 90 91 92 93 94
		if (!retainSelection) {
			this._selectionStart = null;
		}

		let markerIndex;
		if (this._currentMarker === Boundary.Bottom) {
			markerIndex = this._xterm.markers.length;
		} else if (this._currentMarker === Boundary.Top) {
			markerIndex = 0;
95 96 97 98
		} else if (this._isDisposable) {
			markerIndex = this._findNextCommand();
			this._currentMarker.dispose();
			this._isDisposable = false;
D
Daniel Imms 已提交
99 100 101 102 103 104 105 106 107 108 109
		} else {
			markerIndex = this._xterm.markers.indexOf(this._currentMarker) + 1;
		}

		if (markerIndex >= this._xterm.markers.length) {
			this._currentMarker = Boundary.Bottom;
			this._xterm.scrollToBottom();
			return;
		}

		this._currentMarker = this._xterm.markers[markerIndex];
110 111 112 113 114 115 116 117 118
		this._scrollToMarker(this._currentMarker, scrollPosition);
	}

	private _scrollToMarker(marker: IMarker, position: ScrollPosition): void {
		let line = marker.line;
		if (position === ScrollPosition.Middle) {
			line = Math.max(line - this._xterm.rows / 2, 0);
		}
		this._xterm.scrollToLine(line);
D
Daniel Imms 已提交
119 120 121 122 123 124
	}

	public selectToPreviousCommand(): void {
		if (this._selectionStart === null) {
			this._selectionStart = this._currentMarker;
		}
125
		this.scrollToPreviousCommand(ScrollPosition.Middle, true);
D
Daniel Imms 已提交
126 127 128 129 130 131 132
		this._selectLines(this._currentMarker, this._selectionStart);
	}

	public selectToNextCommand(): void {
		if (this._selectionStart === null) {
			this._selectionStart = this._currentMarker;
		}
133
		this.scrollToNextCommand(ScrollPosition.Middle, true);
D
Daniel Imms 已提交
134 135 136
		this._selectLines(this._currentMarker, this._selectionStart);
	}

D
Daniel Imms 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	public selectToPreviousLine(): void {
		if (this._selectionStart === null) {
			this._selectionStart = this._currentMarker;
		}

		this.scrollToPreviousLine(ScrollPosition.Middle, true);
		this._selectLines(this._currentMarker, this._selectionStart);
	}

	public selectToNextLine(): void {
		if (this._selectionStart === null) {
			this._selectionStart = this._currentMarker;
		}

		this.scrollToNextLine(ScrollPosition.Middle, true);
		this._selectLines(this._currentMarker, this._selectionStart);
	}

D
Daniel Imms 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	private _selectLines(start: IMarker | Boundary, end: IMarker | Boundary | null): void {
		if (end === null) {
			end = Boundary.Bottom;
		}

		let startLine = this._getLine(start);
		let endLine = this._getLine(end);

		if (startLine > endLine) {
			const temp = startLine;
			startLine = endLine;
			endLine = temp;
		}

		// Subtract a line as the marker is on the line the command run, we do not want the next
		// command in the selection for the current command
		endLine -= 1;

		this._xterm.selectLines(startLine, endLine);
	}

	private _getLine(marker: IMarker | Boundary): number {
		// Use the _second last_ row as the last row is likely the prompt
		if (marker === Boundary.Bottom) {
			return this._xterm.buffer.ybase + this._xterm.rows - 1;
		}

		if (marker === Boundary.Top) {
			return 0;
		}

		return marker.line;
	}
188 189 190 191 192 193 194 195 196 197 198

	public scrollToPreviousLine(scrollPosition: ScrollPosition = ScrollPosition.Top, retainSelection: boolean = false): void {
		if (!retainSelection) {
			this._selectionStart = null;
		}

		if (this._currentMarker === Boundary.Top) {
			this._xterm.scrollToTop();
			return;
		}

D
Daniel Imms 已提交
199 200 201
		if (this._currentMarker === Boundary.Bottom) {
			this._currentMarker = this._xterm.addMarker(this._getOffset() - 1);
		} else {
D
Daniel Imms 已提交
202
			const offset = this._getOffset();
D
Daniel Imms 已提交
203 204 205 206
			if (this._isDisposable) {
				this._currentMarker.dispose();
			}
			this._currentMarker = this._xterm.addMarker(offset - 1);
207 208 209 210 211 212 213 214 215 216
		}
		this._isDisposable = true;
		this._scrollToMarker(this._currentMarker, scrollPosition);
	}

	public scrollToNextLine(scrollPosition: ScrollPosition = ScrollPosition.Top, retainSelection: boolean = false): void {
		if (!retainSelection) {
			this._selectionStart = null;
		}

D
Daniel Imms 已提交
217
		if (this._currentMarker === Boundary.Bottom) {
218 219 220 221
			this._xterm.scrollToBottom();
			return;
		}

D
Daniel Imms 已提交
222 223 224
		if (this._currentMarker === Boundary.Top) {
			this._currentMarker = this._xterm.addMarker(this._getOffset() + 1);
		} else {
D
Daniel Imms 已提交
225
			const offset = this._getOffset();
D
Daniel Imms 已提交
226 227 228 229
			if (this._isDisposable) {
				this._currentMarker.dispose();
			}
			this._currentMarker = this._xterm.addMarker(offset + 1);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		}
		this._isDisposable = true;
		this._scrollToMarker(this._currentMarker, scrollPosition);
	}

	private _getOffset(): number {
		if (this._currentMarker === Boundary.Bottom) {
			return 0;
		} else if (this._currentMarker === Boundary.Top) {
			return 0 - (this._xterm.buffer.ybase + this._xterm.buffer.y);
		} else {
			let offset = this._getLine(this._currentMarker);
			offset -= this._xterm.buffer.ybase + this._xterm.buffer.y;
			return offset;
		}
	}

	private _findPreviousCommand(): number {
		if (this._currentMarker === Boundary.Top) {
			return 0;
		} else if (this._currentMarker === Boundary.Bottom) {
			return this._xterm.markers.length - 1;
		}

		let i;
		for (i = this._xterm.markers.length - 1; i >= 0; i--) {
			if (this._xterm.markers[i].line < this._currentMarker.line) {
				return i;
			}
		}

		return -1;
	}

	private _findNextCommand(): number {
		if (this._currentMarker === Boundary.Top) {
			return 0;
		} else if (this._currentMarker === Boundary.Bottom) {
			return this._xterm.markers.length - 1;
		}

		let i;
		for (i = 0; i < this._xterm.markers.length; i++) {
			if (this._xterm.markers[i].line > this._currentMarker.line) {
				return i;
			}
		}

		return this._xterm.markers.length;
	}
D
Daniel Imms 已提交
280
}