Refactor process lifecycle to use Start, Tick, Stop methods and metrics

This commit is contained in:
Stone_Red
2026-06-10 02:28:57 +02:00
parent 31e54c1637
commit b3c98b1b1d
7 changed files with 163 additions and 81 deletions
+50 -13
View File
@@ -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");
}
}
}
+8 -6
View File
@@ -10,7 +10,7 @@ internal class DesktopProcess() : Process("Desktop Manager")
{
private static bool isGraphicsInitialized = false;
internal override void Run(string[] args)
internal override void Start(string[] args)
{
if (!isGraphicsInitialized)
{
@@ -54,13 +54,15 @@ internal class DesktopProcess() : Process("Desktop Manager")
testWindow.AutoFlush = true;
testWindow.Flush();
}
while (!StopRequested)
internal override void Tick()
{
WindowManager.Update();
if (!ProcessManager.IsProcessRunning<TerminalProcess>())
{
WindowManager.Update();
// Sleep slightly to yield CPU to the main CLI thread (approx 60 FPS)
//Thread.Sleep(16);
ProcessManager.SpawnProcess<TerminalProcess>();
}
}
}
+11 -10
View File
@@ -11,7 +11,7 @@ namespace RemSox.Processes;
public class TerminalProcess : Process
{
private Window? window;
private Window window = null!;
private readonly List<string> history = [];
private string currentInput = "";
private readonly List<Text> textLines = [];
@@ -23,7 +23,7 @@ public class TerminalProcess : Process
{
}
internal override void Run(string[] args)
internal override void Start(string[] args)
{
window = WindowManager.CreateWindow(this, "Terminal", new Point(50, 50), new Size(400, 300));
@@ -32,19 +32,20 @@ public class TerminalProcess : Process
window.Flush();
window.OnKeyEvent += HandleKey;
}
while (!StopRequested)
internal override void Tick()
{
if (window.Size != lastSize)
{
if (window.Size != lastSize)
{
lastSize = window.Size;
UpdateDisplay();
}
Thread.Sleep(50);
lastSize = window.Size;
UpdateDisplay();
}
}
internal override void Stop()
{
window.OnKeyEvent -= HandleKey;
WindowManager.CloseWindow(window);
}
private void HandleKey(KeyEvent keyEvent)