mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-04 09:06:23 +02:00
Refactor CLI command system to support async execution and central command history
This commit is contained in:
@@ -4,6 +4,8 @@ public static class CommandManager
|
||||
{
|
||||
private static readonly Dictionary<string, ICommand> commands = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private static readonly List<string> commandHistory = [];
|
||||
|
||||
public static void RegisterCommand(ICommand command)
|
||||
{
|
||||
commands[command.Name] = command;
|
||||
@@ -22,7 +24,12 @@ public static class CommandManager
|
||||
return commands.Values.OrderBy(command => command.Name);
|
||||
}
|
||||
|
||||
public static bool TryExecute(string input, Action<string> printLine)
|
||||
public static IList<string> GetCommandHistory()
|
||||
{
|
||||
return commandHistory;
|
||||
}
|
||||
|
||||
public static async Task<bool> TryExecute(string input, Action<string> printLine)
|
||||
{
|
||||
string trimmedInput = input.Trim();
|
||||
|
||||
@@ -31,6 +38,11 @@ public static class CommandManager
|
||||
return false;
|
||||
}
|
||||
|
||||
if (commandHistory.Count == 0 || commandHistory[^1] != trimmedInput)
|
||||
{
|
||||
commandHistory.Add(trimmedInput);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, ICommand> entry in commands
|
||||
.OrderByDescending(entry => entry.Key.Length))
|
||||
{
|
||||
@@ -49,7 +61,7 @@ public static class CommandManager
|
||||
arguments = trimmedInput[commandName.Length..].TrimStart();
|
||||
}
|
||||
|
||||
entry.Value.Execute(arguments, printLine);
|
||||
await entry.Value.ExecuteAsync(arguments, printLine);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,9 @@ public sealed class ClearCommand : ICommand
|
||||
|
||||
public string Description => "Clear the screen";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
Console.Clear();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,10 @@ public sealed class ShutdownCommand : ICommand
|
||||
|
||||
public string Description => "Shutdown the system";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Shutting down system...");
|
||||
Sys.Power.Shutdown();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ public sealed class HelpCommand : ICommand
|
||||
|
||||
public string Description => "Show this help message";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Available commands:");
|
||||
|
||||
@@ -14,5 +14,7 @@ public sealed class HelpCommand : ICommand
|
||||
{
|
||||
printLine($" {command.Name,-12} - {command.Description}");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ public sealed class SpawnProcessCommand : ICommand
|
||||
|
||||
public string Description => "Spawn a new process";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
arguments = arguments?.Trim();
|
||||
|
||||
@@ -29,6 +29,7 @@ public sealed class SpawnProcessCommand : ICommand
|
||||
printLine("Usage: spawn <process-name>");
|
||||
printLine("Available processes: test, terminal");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +39,7 @@ public sealed class ListProcessesCommand : ICommand
|
||||
|
||||
public string Description => "List running processes";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
IEnumerable<Process> processes = ProcessManager.GetAllProcesses();
|
||||
|
||||
@@ -48,6 +49,8 @@ public sealed class ListProcessesCommand : ICommand
|
||||
{
|
||||
printLine($" ID: {process.Id}, Name: {process.Name}");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,23 +60,24 @@ public sealed class StopProcessCommand : ICommand
|
||||
|
||||
public string Description => "Stop a process by ID";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
string? idText = arguments;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(idText))
|
||||
{
|
||||
printLine("Usage: stop <process-id>");
|
||||
return;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
if (int.TryParse(idText, out int processId))
|
||||
{
|
||||
ProcessManager.StopProcess(processId);
|
||||
printLine($"Stopped process with ID {processId}");
|
||||
return;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
printLine("Invalid process ID");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,10 @@ public class RebootCommand : ICommand
|
||||
|
||||
public string Description => "Reboot the system";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Rebooting system...");
|
||||
Sys.Power.Reboot();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ public class StartGuiCommand : ICommand
|
||||
public string Name => "start-gui";
|
||||
public string Description => "Starts the Graphical User Interface (Desktop Process)";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Starting Desktop Process...");
|
||||
|
||||
if (ProcessManager.IsProcessRunning<DesktopProcess>())
|
||||
{
|
||||
printLine("Desktop Process is already running.");
|
||||
return;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
foreach (Process process in ProcessManager.GetProcessesOfType<CliProcess>())
|
||||
@@ -24,5 +24,6 @@ public class StartGuiCommand : ICommand
|
||||
}
|
||||
|
||||
_ = ProcessManager.SpawnProcess<DesktopProcess>();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public class StopGuiCommand : ICommand
|
||||
public string Name => "stop-gui";
|
||||
public string Description => "Stops the Graphical User Interface (Desktop Process)";
|
||||
|
||||
public async void Execute(string? arguments, Action<string> printLine)
|
||||
public async Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Stopping Desktop Process...");
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ public class ViewProcessLogs : ICommand
|
||||
|
||||
public string Description => "View logs for a process";
|
||||
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
public Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
if (int.TryParse(arguments, out int processId))
|
||||
{
|
||||
@@ -21,6 +21,7 @@ public class ViewProcessLogs : ICommand
|
||||
IEnumerable<LogEntry> logs = ProcessManager.GetLogs();
|
||||
PrintLogs(logs, printLine);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static void PrintLogs(IEnumerable<LogEntry> logs, Action<string> printLine)
|
||||
|
||||
+1
-1
@@ -19,5 +19,5 @@ public interface ICommand
|
||||
/// </summary>
|
||||
/// <param name="arguments">The arguments provided to the command.</param>
|
||||
/// <param name="printLine">A delegate to stream output lines to the current console or terminal.</param>
|
||||
void Execute(string? arguments, Action<string> printLine);
|
||||
Task ExecuteAsync(string? arguments, Action<string> printLine);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user