From 82a41774560a60cb2eca372c72b13f986f975451 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Fri, 6 Feb 2026 01:39:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B9=E5=90=91=E9=94=AE=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E6=9F=A5=E6=89=BE=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shell/Shell.cs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/shell/Shell.cs b/shell/Shell.cs index 18b4006..3d5da3b 100644 --- a/shell/Shell.cs +++ b/shell/Shell.cs @@ -144,6 +144,7 @@ namespace CMLeonOS int cursorPos = 0; int startLine = Console.CursorTop; int startCol = Console.CursorLeft; + int historyIndex = -1; while (true) { @@ -164,6 +165,42 @@ namespace CMLeonOS cursorPos = input.Length; } } + else if (key.Key == ConsoleKey.UpArrow) + { + if (commandHistory.Count > 0) + { + if (historyIndex < commandHistory.Count - 1) + { + historyIndex++; + string historyCommand = commandHistory[commandHistory.Count - 1 - historyIndex]; + + ClearCurrentInput(startCol, startLine, input.Length); + Console.Write(historyCommand); + input = historyCommand; + cursorPos = input.Length; + } + } + } + else if (key.Key == ConsoleKey.DownArrow) + { + if (historyIndex > 0) + { + historyIndex--; + string historyCommand = commandHistory[commandHistory.Count - 1 - historyIndex]; + + ClearCurrentInput(startCol, startLine, input.Length); + Console.Write(historyCommand); + input = historyCommand; + cursorPos = input.Length; + } + else if (historyIndex == 0) + { + historyIndex = -1; + ClearCurrentInput(startCol, startLine, input.Length); + input = ""; + cursorPos = 0; + } + } else if (key.Key == ConsoleKey.Backspace) { if (cursorPos > 0) @@ -182,6 +219,7 @@ namespace CMLeonOS { input = ""; cursorPos = 0; + historyIndex = -1; Console.SetCursorPosition(startCol, startLine); Console.Write(new string(' ', Console.WindowWidth - startCol)); Console.SetCursorPosition(startCol, startLine); @@ -192,10 +230,33 @@ namespace CMLeonOS input = input.Substring(0, cursorPos) + key.KeyChar + input.Substring(cursorPos); cursorPos++; Console.Write(key.KeyChar); + historyIndex = -1; } } } + private void ClearCurrentInput(int startCol, int startLine, int length) + { + int currentLine = startLine; + int currentCol = startCol; + int remaining = length; + + while (remaining > 0) + { + int spaceInLine = Console.WindowWidth - currentCol; + int toClear = Math.Min(remaining, spaceInLine); + + Console.SetCursorPosition(currentCol, currentLine); + Console.Write(new string(' ', toClear)); + + remaining -= toClear; + currentLine++; + currentCol = 0; + } + + Console.SetCursorPosition(startCol, startLine); + } + private string AutoComplete(string input) { if (string.IsNullOrWhiteSpace(input))