mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-05 23:41:28 +02:00
Refactor process lifecycle to use Start, Tick, Stop methods and metrics
This commit is contained in:
+50
-13
@@ -5,29 +5,66 @@ namespace RemSox.Processes;
|
||||
|
||||
internal class CliProcess() : Process("Cli")
|
||||
{
|
||||
internal override void Run(string[] args)
|
||||
private string currentInput = string.Empty;
|
||||
|
||||
internal override void Start(string[] args)
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine("Welcome RemSox!");
|
||||
Console.WriteLine("Type 'help' to see available commands.");
|
||||
Console.Write("> ");
|
||||
}
|
||||
|
||||
while (!StopRequested)
|
||||
internal override void Tick()
|
||||
{
|
||||
while (Console.KeyAvailable)
|
||||
{
|
||||
Console.Write("> ");
|
||||
ConsoleKeyInfo key = Console.ReadKey(intercept: true);
|
||||
|
||||
string? input = Console.ReadLine();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
switch (key.Key)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
case ConsoleKey.Enter:
|
||||
Console.WriteLine();
|
||||
|
||||
bool handled = CommandManager.TryExecute(input, line => Console.WriteLine(line));
|
||||
HandleCommand(currentInput);
|
||||
|
||||
if (!handled)
|
||||
{
|
||||
Console.WriteLine($"\"{input}\" is not a command");
|
||||
currentInput = string.Empty;
|
||||
Console.Write("> ");
|
||||
break;
|
||||
|
||||
case ConsoleKey.Backspace:
|
||||
if (currentInput.Length > 0)
|
||||
{
|
||||
currentInput = currentInput[..^1];
|
||||
Console.Write("\b \b");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
currentInput += key.KeyChar;
|
||||
Console.Write(key.KeyChar);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal override void Stop()
|
||||
{
|
||||
Console.Clear();
|
||||
}
|
||||
|
||||
private static void HandleCommand(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
return;
|
||||
|
||||
bool handled = CommandManager.TryExecute(
|
||||
input,
|
||||
line => Console.WriteLine(line));
|
||||
|
||||
if (!handled)
|
||||
{
|
||||
Console.WriteLine($"\"{input}\" is not a command");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user