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
+32 -1
View File
@@ -7,6 +7,9 @@ internal class CliProcess() : Process("Cli")
{
private string currentInput = string.Empty;
private readonly List<string> commandHistory = [];
private int historyIndex = -1;
internal override void Start(string[] args)
{
Console.Clear();
@@ -26,6 +29,13 @@ internal class CliProcess() : Process("Cli")
case ConsoleKey.Enter:
Console.WriteLine();
if (!string.IsNullOrWhiteSpace(currentInput) && (commandHistory.Count == 0 || commandHistory[^1] != currentInput))
{
commandHistory.Add(currentInput);
}
historyIndex = commandHistory.Count;
HandleCommand(currentInput);
currentInput = string.Empty;
@@ -39,7 +49,28 @@ internal class CliProcess() : Process("Cli")
Console.Write("\b \b");
}
break;
case ConsoleKey.UpArrow:
if (commandHistory.Count > 0)
{
historyIndex = Math.Max(historyIndex - 1, 0);
currentInput = commandHistory[historyIndex];
Console.Write("\r> " + currentInput + new string('#', Console.WindowWidth - currentInput.Length - 2));
Console.CursorLeft = 0;
Console.CursorTop--;
Console.Write("> " + currentInput);
}
break;
case ConsoleKey.DownArrow:
if (commandHistory.Count > 0)
{
historyIndex = Math.Min(historyIndex + 1, commandHistory.Count - 1);
currentInput = commandHistory[historyIndex];
Console.Write("\r> " + currentInput + new string('#', Console.WindowWidth - currentInput.Length - 2));
Console.CursorLeft = 0;
Console.CursorTop--;
Console.Write("> " + currentInput);
}
break;
default:
if (!char.IsControl(key.KeyChar))
{