Skip to content

Commit 4981e5c

Browse files
Filter command history based on current prompt (#238)
* Filter command history based on current prompt * Rename _commandHistory -> _rawCommandHistory * Always act on filtered history instead of forking logic
1 parent 040d1e0 commit 4981e5c

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/commandwindow/CommandWindow.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ export default class CommandWindow implements vscode.Pseudoterminal {
9191

9292
private _lastOutputLine: string = '';
9393

94-
private readonly _commandHistory: string[] = [];
94+
private readonly _rawCommandHistory: string[] = [];
9595
private _historyIndex: number = 0;
9696
private _lastKnownCurrentLine: string = '';
97+
private _filteredCommandHistory: string[] = [];
9798

9899
private _terminalDimensions: vscode.TerminalDimensions;
99100
private _lastSentTerminalDimensions: vscode.TerminalDimensions | null = null;
@@ -346,27 +347,33 @@ export default class CommandWindow implements vscode.Pseudoterminal {
346347
}
347348

348349
private _handleNavigateHistory (direction: HistoryDirection): boolean {
349-
const isCurrentlyAtEndOfHistory = this._historyIndex === this._commandHistory.length;
350-
const isCurrentlyAtBeginningOfHistory = this._historyIndex === 0;
350+
const isAtEnd = this._historyIndex === this._filteredCommandHistory.length;
351+
const isAtBeginning = this._historyIndex === 0;
351352

352-
if (direction === HistoryDirection.BACKWARDS && isCurrentlyAtBeginningOfHistory) {
353+
if ((direction === HistoryDirection.BACKWARDS && isAtBeginning) ||
354+
(direction === HistoryDirection.FORWARDS && isAtEnd)) {
353355
return false;
354356
}
355357

356-
if (direction === HistoryDirection.FORWARDS && isCurrentlyAtEndOfHistory) {
357-
return false;
358-
}
359-
360-
if (isCurrentlyAtEndOfHistory) {
358+
if (isAtEnd) {
361359
this._lastKnownCurrentLine = this._stripCurrentPrompt(this._currentPromptLine);
362360
}
363361

364362
this._historyIndex += direction === HistoryDirection.BACKWARDS ? -1 : 1;
365-
return this._replaceCurrentLineWithNewLine(this._currentPrompt + this._getHistoryItem(this._historyIndex));
363+
const line = this._getHistoryItem(this._historyIndex);
364+
return this._replaceCurrentLineWithNewLine(this._currentPrompt + line);
366365
}
367366

368367
private _markCurrentLineChanged (): void {
369-
this._historyIndex = this._commandHistory.length;
368+
const commandHistoryFilter = this._stripCurrentPrompt(this._currentPromptLine);
369+
if (commandHistoryFilter !== '') {
370+
this._filteredCommandHistory = this._rawCommandHistory.filter(cmd =>
371+
cmd.toLowerCase().startsWith(commandHistoryFilter.toLowerCase()));
372+
} else {
373+
this._filteredCommandHistory = this._rawCommandHistory
374+
}
375+
376+
this._historyIndex = this._filteredCommandHistory.length;
370377
this._lastKnownCurrentLine = '';
371378
}
372379

@@ -582,19 +589,20 @@ export default class CommandWindow implements vscode.Pseudoterminal {
582589

583590
private _addToHistory (command: string): void {
584591
const isEmpty = command === '';
585-
const isLastInHistory = this._commandHistory.length !== 0 && command === this._commandHistory[this._commandHistory.length - 1];
592+
const isLastInHistory =
593+
this._rawCommandHistory.length !== 0 &&
594+
command === this._rawCommandHistory[this._rawCommandHistory.length - 1];
586595
if (!isEmpty && !isLastInHistory) {
587-
this._commandHistory.push(command);
596+
this._rawCommandHistory.push(command);
588597
}
589-
this._historyIndex = this._commandHistory.length;
598+
this._historyIndex = this._rawCommandHistory.length;
599+
this._filteredCommandHistory = this._rawCommandHistory
590600
}
591601

592602
private _getHistoryItem (n: number): string {
593-
if (this._historyIndex < this._commandHistory.length) {
594-
return this._commandHistory[n];
595-
} else {
596-
return this._lastKnownCurrentLine;
597-
}
603+
return (this._historyIndex < this._filteredCommandHistory.length)
604+
? this._filteredCommandHistory[n]
605+
: this._lastKnownCurrentLine;
598606
}
599607

600608
private _moveCursorToCurrent (lineOfInputCursorIsCurrentlyOn?: number): void {

0 commit comments

Comments
 (0)