Skip to content

Commit 47b2565

Browse files
Filter command history based on current prompt
1 parent 040d1e0 commit 47b2565

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

src/commandwindow/CommandWindow.ts

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ export default class CommandWindow implements vscode.Pseudoterminal {
9494
private readonly _commandHistory: string[] = [];
9595
private _historyIndex: number = 0;
9696
private _lastKnownCurrentLine: string = '';
97+
private _filteredCommandHistory: string[] = [];
98+
private _filteredHistoryIndex: number = 0;
99+
private _commandHistoryFilter: string = '';
97100

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

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

352-
if (direction === HistoryDirection.BACKWARDS && isCurrentlyAtBeginningOfHistory) {
353-
return false;
354-
}
354+
if (isCurrentlyAtEndOfHistory && this._stripCurrentPrompt(this._currentPromptLine) !== '') {
355+
// Only update the filtered history if the current line changes
356+
if (this._filteredCommandHistory.length === 0) {
357+
this._commandHistoryFilter = this._stripCurrentPrompt(this._currentPromptLine);
358+
359+
this._filteredCommandHistory = this._commandHistory.filter(cmd =>
360+
cmd.toLowerCase().startsWith(this._commandHistoryFilter.toLowerCase()));
361+
this._filteredHistoryIndex = this._filteredCommandHistory.length;
362+
}
355363

356-
if (direction === HistoryDirection.FORWARDS && isCurrentlyAtEndOfHistory) {
364+
// Filter history based on the current prompt text
365+
return this._navigateHistory(
366+
direction,
367+
this._filteredHistoryIndex,
368+
this._filteredCommandHistory,
369+
(newIndex) => { this._filteredHistoryIndex = newIndex }
370+
);
371+
}
372+
373+
return this._navigateHistory(
374+
direction,
375+
this._historyIndex,
376+
this._commandHistory,
377+
(newIndex) => { this._historyIndex = newIndex }
378+
);
379+
}
380+
381+
private _navigateHistory (
382+
direction: HistoryDirection,
383+
currentIndex: number,
384+
history: string[],
385+
updateIndex: (newIndex: number) => void
386+
): boolean {
387+
const isAtEnd = currentIndex === history.length;
388+
const isAtBeginning = currentIndex === 0;
389+
390+
if ((direction === HistoryDirection.BACKWARDS && isAtBeginning) ||
391+
(direction === HistoryDirection.FORWARDS && isAtEnd)) {
357392
return false;
358393
}
359394

360-
if (isCurrentlyAtEndOfHistory) {
395+
if (isAtEnd) {
361396
this._lastKnownCurrentLine = this._stripCurrentPrompt(this._currentPromptLine);
362397
}
363398

364-
this._historyIndex += direction === HistoryDirection.BACKWARDS ? -1 : 1;
365-
return this._replaceCurrentLineWithNewLine(this._currentPrompt + this._getHistoryItem(this._historyIndex));
399+
currentIndex += direction === HistoryDirection.BACKWARDS ? -1 : 1;
400+
updateIndex(currentIndex);
401+
const line = (currentIndex < history.length)
402+
? history[currentIndex]
403+
: this._lastKnownCurrentLine;
404+
return this._replaceCurrentLineWithNewLine(this._currentPrompt + line);
366405
}
367406

368407
private _markCurrentLineChanged (): void {
369408
this._historyIndex = this._commandHistory.length;
409+
this._filteredCommandHistory = [];
370410
this._lastKnownCurrentLine = '';
371411
}
372412

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

592-
private _getHistoryItem (n: number): string {
593-
if (this._historyIndex < this._commandHistory.length) {
594-
return this._commandHistory[n];
595-
} else {
596-
return this._lastKnownCurrentLine;
597-
}
598-
}
599-
600632
private _moveCursorToCurrent (lineOfInputCursorIsCurrentlyOn?: number): void {
601633
const lineNumberCursorShouldBeOn = Math.max(1, Math.ceil(this._getAbsoluteIndexOnLine(this._cursorIndex) / this._terminalDimensions.columns));
602634
if (lineOfInputCursorIsCurrentlyOn === undefined) {

0 commit comments

Comments
 (0)