Add command history navigation with up/down arrow keys

This commit is contained in:
Stone_Red
2026-06-11 18:23:48 +02:00
parent 6728835d77
commit 37c08fa3c5
2 changed files with 59 additions and 1 deletions
+27
View File
@@ -13,6 +13,8 @@ public class TerminalProcess() : Process("Terminal")
{
private Window window = null!;
private readonly List<string> history = [];
private readonly List<string> commandHistory = [];
private int historyIndex = -1;
private string currentInput = "";
private readonly List<Text> textLines = [];
private readonly Lock textLinesLock = new();
@@ -49,6 +51,13 @@ public class TerminalProcess() : Process("Terminal")
if (keyEvent.Key == ConsoleKeyEx.Enter)
{
string cmd = currentInput;
if (!string.IsNullOrWhiteSpace(currentInput) && (commandHistory.Count == 0 || commandHistory[^1] != currentInput))
{
commandHistory.Add(currentInput);
}
historyIndex = commandHistory.Count;
PrintLine("> " + cmd);
currentInput = "";
@@ -85,6 +94,24 @@ public class TerminalProcess() : Process("Terminal")
UpdateDisplay();
}
}
else if (keyEvent.Key == ConsoleKeyEx.UpArrow)
{
if (commandHistory.Count > 0)
{
historyIndex = Math.Max(historyIndex - 1, 0);
currentInput = commandHistory[historyIndex];
UpdateDisplay();
}
}
else if (keyEvent.Key == ConsoleKeyEx.DownArrow)
{
if (commandHistory.Count > 0)
{
historyIndex = Math.Min(historyIndex + 1, commandHistory.Count - 1);
currentInput = commandHistory[historyIndex];
UpdateDisplay();
}
}
else if (!char.IsControl(keyEvent.KeyChar))
{
currentInput += keyEvent.KeyChar;