mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-07 15:56:31 +02:00
Initial commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI;
|
||||
|
||||
public static class CommandManager
|
||||
{
|
||||
private static readonly Dictionary<string, ICommand> commands = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public static void RegisterCommand(ICommand command)
|
||||
{
|
||||
commands[command.Name] = command;
|
||||
}
|
||||
|
||||
public static void RegisterCommands(IEnumerable<ICommand> commandsToRegister)
|
||||
{
|
||||
foreach (ICommand command in commandsToRegister)
|
||||
{
|
||||
RegisterCommand(command);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<ICommand> GetCommands()
|
||||
{
|
||||
return commands.Values.OrderBy(command => command.Name);
|
||||
}
|
||||
|
||||
public static bool TryExecute(string input)
|
||||
{
|
||||
string trimmedInput = input.Trim();
|
||||
|
||||
if (trimmedInput.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, ICommand> entry in commands
|
||||
.OrderByDescending(entry => entry.Key.Length))
|
||||
{
|
||||
string commandName = entry.Key;
|
||||
|
||||
if (!trimmedInput.Equals(commandName, StringComparison.OrdinalIgnoreCase) &&
|
||||
!trimmedInput.StartsWith(commandName + " ", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string? arguments = null;
|
||||
|
||||
if (trimmedInput.Length > commandName.Length)
|
||||
{
|
||||
arguments = trimmedInput.Substring(commandName.Length).TrimStart();
|
||||
}
|
||||
|
||||
entry.Value.Execute(arguments);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
|
||||
public sealed class ClearCommand : ICommand
|
||||
{
|
||||
public string Name => "clear";
|
||||
|
||||
public string Description => "Clear the screen";
|
||||
|
||||
public void Execute(string? arguments)
|
||||
{
|
||||
Console.Clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
|
||||
public sealed class HaltCommand : ICommand
|
||||
{
|
||||
public string Name => "halt";
|
||||
|
||||
public string Description => "Halt the system";
|
||||
|
||||
public void Execute(string? arguments)
|
||||
{
|
||||
Console.WriteLine("Halting system...");
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
|
||||
public sealed class HelpCommand : ICommand
|
||||
{
|
||||
public string Name => "help";
|
||||
|
||||
public string Description => "Show this help message";
|
||||
|
||||
public void Execute(string? arguments)
|
||||
{
|
||||
Console.WriteLine("Available commands:");
|
||||
|
||||
foreach (ICommand command in CommandManager.GetCommands())
|
||||
{
|
||||
Console.WriteLine($" {command.Name,-12} - {command.Description}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using RemSox.Processing;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
|
||||
public sealed class SpawnTestProcessCommand : ICommand
|
||||
{
|
||||
public string Name => "spawn test";
|
||||
|
||||
public string Description => "Spawn the test process";
|
||||
|
||||
public void Execute(string? arguments)
|
||||
{
|
||||
int processId = ProcessManager.SpawnProcess<TestProcess>();
|
||||
Console.WriteLine($"Spawned TestProcess with ID {processId}");
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ListProcessesCommand : ICommand
|
||||
{
|
||||
public string Name => "ps";
|
||||
|
||||
public string Description => "List running processes";
|
||||
|
||||
public void Execute(string? arguments)
|
||||
{
|
||||
IEnumerable<Process> processes = ProcessManager.GetAllProcesses();
|
||||
|
||||
Console.WriteLine("Running processes:");
|
||||
|
||||
foreach (Process process in processes)
|
||||
{
|
||||
Console.WriteLine($" ID: {process.Id}, Name: {process.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class StopProcessCommand : ICommand
|
||||
{
|
||||
public string Name => "stop";
|
||||
|
||||
public string Description => "Stop a process by ID";
|
||||
|
||||
public void Execute(string? arguments)
|
||||
{
|
||||
string? idText = arguments;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(idText))
|
||||
{
|
||||
Console.WriteLine("Usage: stop <process-id>");
|
||||
return;
|
||||
}
|
||||
|
||||
if (int.TryParse(idText, out int processId))
|
||||
{
|
||||
ProcessManager.StopProcess(processId);
|
||||
Console.WriteLine($"Stopped process with ID {processId}");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Invalid process ID");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace RemSox.UI.GUI.CLI;
|
||||
|
||||
public interface ICommand
|
||||
{
|
||||
string Name { get; }
|
||||
|
||||
string Description { get; }
|
||||
|
||||
void Execute(string? arguments);
|
||||
}
|
||||
Reference in New Issue
Block a user