diff --git a/Processes/CliProcess.cs b/Processes/CliProcess.cs index a5623df..6f3ad73 100644 --- a/Processes/CliProcess.cs +++ b/Processes/CliProcess.cs @@ -7,6 +7,9 @@ internal class CliProcess() : Process("Cli") { private string currentInput = string.Empty; + private readonly List 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)) { diff --git a/Processes/TerminalProcess.cs b/Processes/TerminalProcess.cs index 7ac94af..169f86b 100644 --- a/Processes/TerminalProcess.cs +++ b/Processes/TerminalProcess.cs @@ -13,6 +13,8 @@ public class TerminalProcess() : Process("Terminal") { private Window window = null!; private readonly List history = []; + private readonly List commandHistory = []; + private int historyIndex = -1; private string currentInput = ""; private readonly List 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;