Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions src/commandwindow/CommandWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export default class CommandWindow implements vscode.Pseudoterminal {
private readonly _commandHistory: string[] = [];
private _historyIndex: number = 0;
private _lastKnownCurrentLine: string = '';
private _filteredCommandHistory: string[] = [];
private _filteredHistoryIndex: number = 0;
private _commandHistoryFilter: string = '';

private _terminalDimensions: vscode.TerminalDimensions;
private _lastSentTerminalDimensions: vscode.TerminalDimensions | null = null;
Expand Down Expand Up @@ -347,26 +350,63 @@ export default class CommandWindow implements vscode.Pseudoterminal {

private _handleNavigateHistory (direction: HistoryDirection): boolean {
const isCurrentlyAtEndOfHistory = this._historyIndex === this._commandHistory.length;
const isCurrentlyAtBeginningOfHistory = this._historyIndex === 0;

if (direction === HistoryDirection.BACKWARDS && isCurrentlyAtBeginningOfHistory) {
return false;
}
if (isCurrentlyAtEndOfHistory && this._stripCurrentPrompt(this._currentPromptLine) !== '') {
// Only update the filtered history if the current line changes
if (this._filteredCommandHistory.length === 0) {
this._commandHistoryFilter = this._stripCurrentPrompt(this._currentPromptLine);

this._filteredCommandHistory = this._commandHistory.filter(cmd =>
cmd.toLowerCase().startsWith(this._commandHistoryFilter.toLowerCase()));
this._filteredHistoryIndex = this._filteredCommandHistory.length;
}

if (direction === HistoryDirection.FORWARDS && isCurrentlyAtEndOfHistory) {
// Filter history based on the current prompt text
return this._navigateHistory(
direction,
this._filteredHistoryIndex,
this._filteredCommandHistory,
(newIndex) => { this._filteredHistoryIndex = newIndex }
);
}

return this._navigateHistory(
direction,
this._historyIndex,
this._commandHistory,
(newIndex) => { this._historyIndex = newIndex }
);
}

private _navigateHistory (
direction: HistoryDirection,
currentIndex: number,
history: string[],
updateIndex: (newIndex: number) => void
): boolean {
const isAtEnd = currentIndex === history.length;
const isAtBeginning = currentIndex === 0;

if ((direction === HistoryDirection.BACKWARDS && isAtBeginning) ||
(direction === HistoryDirection.FORWARDS && isAtEnd)) {
return false;
}

if (isCurrentlyAtEndOfHistory) {
if (isAtEnd) {
this._lastKnownCurrentLine = this._stripCurrentPrompt(this._currentPromptLine);
}

this._historyIndex += direction === HistoryDirection.BACKWARDS ? -1 : 1;
return this._replaceCurrentLineWithNewLine(this._currentPrompt + this._getHistoryItem(this._historyIndex));
currentIndex += direction === HistoryDirection.BACKWARDS ? -1 : 1;
updateIndex(currentIndex);
const line = (currentIndex < history.length)
? history[currentIndex]
: this._lastKnownCurrentLine;
return this._replaceCurrentLineWithNewLine(this._currentPrompt + line);
}

private _markCurrentLineChanged (): void {
this._historyIndex = this._commandHistory.length;
this._filteredCommandHistory = [];
this._lastKnownCurrentLine = '';
}

Expand Down Expand Up @@ -589,14 +629,6 @@ export default class CommandWindow implements vscode.Pseudoterminal {
this._historyIndex = this._commandHistory.length;
}

private _getHistoryItem (n: number): string {
if (this._historyIndex < this._commandHistory.length) {
return this._commandHistory[n];
} else {
return this._lastKnownCurrentLine;
}
}

private _moveCursorToCurrent (lineOfInputCursorIsCurrentlyOn?: number): void {
const lineNumberCursorShouldBeOn = Math.max(1, Math.ceil(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns));
if (lineOfInputCursorIsCurrentlyOn === undefined) {
Expand Down