mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-05 15:56:32 +02:00
Refactor CLI command system to support async execution and central command history
This commit is contained in:
+19
-19
@@ -7,20 +7,23 @@ internal class CliProcess() : Process("Cli")
|
||||
{
|
||||
private string currentInput = string.Empty;
|
||||
|
||||
private readonly List<string> commandHistory = [];
|
||||
private int historyIndex = -1;
|
||||
|
||||
private bool commandRunning = false;
|
||||
|
||||
internal override void Start(string[] args)
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine("Welcome RemSox!");
|
||||
Console.WriteLine("Type 'help' to see available commands.");
|
||||
Console.Write("> ");
|
||||
|
||||
historyIndex = CommandManager.GetCommandHistory().Count;
|
||||
}
|
||||
|
||||
internal override void Tick()
|
||||
internal override async void Tick()
|
||||
{
|
||||
while (Console.KeyAvailable)
|
||||
while (Console.KeyAvailable && !commandRunning)
|
||||
{
|
||||
ConsoleKeyInfo key = Console.ReadKey(intercept: true);
|
||||
|
||||
@@ -29,14 +32,9 @@ internal class CliProcess() : Process("Cli")
|
||||
case ConsoleKey.Enter:
|
||||
Console.WriteLine();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(currentInput) && (commandHistory.Count == 0 || commandHistory[^1] != currentInput))
|
||||
{
|
||||
commandHistory.Add(currentInput);
|
||||
}
|
||||
await HandleCommand(currentInput);
|
||||
|
||||
historyIndex = commandHistory.Count;
|
||||
|
||||
HandleCommand(currentInput);
|
||||
historyIndex = CommandManager.GetCommandHistory().Count;
|
||||
|
||||
currentInput = string.Empty;
|
||||
Console.Write("> ");
|
||||
@@ -50,10 +48,10 @@ internal class CliProcess() : Process("Cli")
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.UpArrow:
|
||||
if (commandHistory.Count > 0)
|
||||
if (CommandManager.GetCommandHistory().Count > 0)
|
||||
{
|
||||
historyIndex = Math.Max(historyIndex - 1, 0);
|
||||
currentInput = commandHistory[historyIndex];
|
||||
currentInput = CommandManager.GetCommandHistory()[historyIndex];
|
||||
Console.Write("\r> " + currentInput + new string('#', Console.WindowWidth - currentInput.Length - 2));
|
||||
Console.CursorLeft = 0;
|
||||
Console.CursorTop--;
|
||||
@@ -61,10 +59,10 @@ internal class CliProcess() : Process("Cli")
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
if (commandHistory.Count > 0)
|
||||
if (CommandManager.GetCommandHistory().Count > 0)
|
||||
{
|
||||
historyIndex = Math.Min(historyIndex + 1, commandHistory.Count - 1);
|
||||
currentInput = commandHistory[historyIndex];
|
||||
historyIndex = Math.Min(historyIndex + 1, CommandManager.GetCommandHistory().Count - 1);
|
||||
currentInput = CommandManager.GetCommandHistory()[historyIndex];
|
||||
Console.Write("\r> " + currentInput + new string('#', Console.WindowWidth - currentInput.Length - 2));
|
||||
Console.CursorLeft = 0;
|
||||
Console.CursorTop--;
|
||||
@@ -82,20 +80,22 @@ internal class CliProcess() : Process("Cli")
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleCommand(string input)
|
||||
private async Task HandleCommand(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool handled = CommandManager.TryExecute(
|
||||
input,
|
||||
line => Console.WriteLine(line));
|
||||
commandRunning = true;
|
||||
|
||||
bool handled = await CommandManager.TryExecute(input, Console.WriteLine);
|
||||
|
||||
if (!handled)
|
||||
{
|
||||
Console.WriteLine($"\"{input}\" is not a command");
|
||||
}
|
||||
|
||||
commandRunning = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user