mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-04 09:06:23 +02:00
Add command interruption support with Ctrl+C and cancellation tokens
This commit is contained in:
+17
-2
@@ -11,6 +11,8 @@ internal class CliProcess() : Process("Cli")
|
||||
|
||||
private bool commandRunning = false;
|
||||
|
||||
private CancellationTokenSource? commandCancellationTokenSource = null;
|
||||
|
||||
internal override void Start(string[] args)
|
||||
{
|
||||
Console.Clear();
|
||||
@@ -23,10 +25,20 @@ internal class CliProcess() : Process("Cli")
|
||||
|
||||
internal override async void Tick()
|
||||
{
|
||||
while (Console.KeyAvailable && !commandRunning)
|
||||
while (Console.KeyAvailable)
|
||||
{
|
||||
ConsoleKeyInfo key = Console.ReadKey(intercept: true);
|
||||
|
||||
if (commandRunning)
|
||||
{
|
||||
if (key.Key == ConsoleKey.C && key.Modifiers.HasFlag(ConsoleModifiers.Control) && commandCancellationTokenSource is not null)
|
||||
{
|
||||
await commandCancellationTokenSource.CancelAsync();
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (key.Key)
|
||||
{
|
||||
case ConsoleKey.Enter:
|
||||
@@ -87,9 +99,10 @@ internal class CliProcess() : Process("Cli")
|
||||
return;
|
||||
}
|
||||
|
||||
commandCancellationTokenSource = new CancellationTokenSource();
|
||||
commandRunning = true;
|
||||
|
||||
bool handled = await CommandManager.TryExecute(input, Console.WriteLine);
|
||||
bool handled = await CommandManager.TryExecuteAsync(input, Console.WriteLine, commandCancellationTokenSource.Token);
|
||||
|
||||
if (!handled)
|
||||
{
|
||||
@@ -97,5 +110,7 @@ internal class CliProcess() : Process("Cli")
|
||||
}
|
||||
|
||||
commandRunning = false;
|
||||
commandCancellationTokenSource.Dispose();
|
||||
commandCancellationTokenSource = null;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@ public class TerminalProcess() : Process("Terminal")
|
||||
private Window window = null!;
|
||||
private readonly List<string> history = [];
|
||||
private int historyIndex = -1;
|
||||
private bool commandRunning = false;
|
||||
private CancellationTokenSource? commandCancellationTokenSource = null;
|
||||
private string currentInput = "";
|
||||
private readonly List<Text> textLines = [];
|
||||
private readonly Lock textLinesLock = new();
|
||||
@@ -49,6 +51,16 @@ public class TerminalProcess() : Process("Terminal")
|
||||
|
||||
private async void HandleKey(KeyEvent keyEvent)
|
||||
{
|
||||
if (commandRunning)
|
||||
{
|
||||
if (keyEvent.Key == ConsoleKeyEx.C && keyEvent.Modifiers.HasFlag(ConsoleModifiers.Control) && commandCancellationTokenSource is not null)
|
||||
{
|
||||
await commandCancellationTokenSource.CancelAsync();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (keyEvent.Key == ConsoleKeyEx.Enter)
|
||||
{
|
||||
string cmd = currentInput;
|
||||
@@ -72,16 +84,19 @@ public class TerminalProcess() : Process("Terminal")
|
||||
}
|
||||
else
|
||||
{
|
||||
window.OnKeyEvent -= HandleKey;
|
||||
commandCancellationTokenSource = new CancellationTokenSource();
|
||||
commandRunning = true;
|
||||
|
||||
bool handled = await CommandManager.TryExecute(cmd, PrintLine);
|
||||
bool handled = await CommandManager.TryExecuteAsync(cmd, PrintLine, commandCancellationTokenSource.Token);
|
||||
|
||||
if (!handled)
|
||||
{
|
||||
PrintLine($"\"{cmd}\" is not a command");
|
||||
}
|
||||
|
||||
window.OnKeyEvent += HandleKey;
|
||||
commandRunning = false;
|
||||
commandCancellationTokenSource.Dispose();
|
||||
commandCancellationTokenSource = null;
|
||||
}
|
||||
|
||||
historyIndex = CommandManager.GetCommandHistory().Count;
|
||||
|
||||
Reference in New Issue
Block a user