Refactor CLI command system to support async execution and central command history

This commit is contained in:
Stone_Red
2026-06-11 20:40:26 +02:00
parent 2b091e6e7c
commit 160d0559e8
14 changed files with 114 additions and 63 deletions
+2 -1
View File
@@ -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;
}
}
+2 -1
View File
@@ -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;
}
}
+3 -1
View File
@@ -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 -5
View File
@@ -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;
}
}
+2 -1
View File
@@ -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;
}
}
+3 -2
View File
@@ -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;
}
}
+1 -1
View File
@@ -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...");
+2 -1
View File
@@ -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)