mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-05 15:56:32 +02:00
Add command interruption support with Ctrl+C and cancellation tokens
This commit is contained in:
@@ -29,7 +29,7 @@ public static class CommandManager
|
||||
return commandHistory;
|
||||
}
|
||||
|
||||
public static async Task<bool> TryExecute(string input, Action<string> printLine)
|
||||
public static async Task<bool> TryExecuteAsync(string input, Action<string> printLine, CancellationToken cancellationToken = default)
|
||||
{
|
||||
string trimmedInput = input.Trim();
|
||||
|
||||
@@ -43,8 +43,7 @@ public static class CommandManager
|
||||
commandHistory.Add(trimmedInput);
|
||||
}
|
||||
|
||||
foreach (KeyValuePair<string, ICommand> entry in commands
|
||||
.OrderByDescending(entry => entry.Key.Length))
|
||||
foreach (KeyValuePair<string, ICommand> entry in commands.OrderByDescending(entry => entry.Key.Length))
|
||||
{
|
||||
string commandName = entry.Key;
|
||||
|
||||
@@ -61,6 +60,8 @@ public static class CommandManager
|
||||
arguments = trimmedInput[commandName.Length..].TrimStart();
|
||||
}
|
||||
|
||||
cancellationToken.Register(async () => await entry.Value.StopAsync());
|
||||
|
||||
await entry.Value.ExecuteAsync(arguments, printLine);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -18,11 +18,6 @@ public class StartGuiCommand : ICommand
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
foreach (Process process in ProcessManager.GetProcessesOfType<CliProcess>())
|
||||
{
|
||||
ProcessManager.StopProcess(process.Id);
|
||||
}
|
||||
|
||||
_ = ProcessManager.SpawnProcess<DesktopProcess>();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,5 @@ public class StopGuiCommand : ICommand
|
||||
{
|
||||
await ProcessManager.StopProcessAndWaitAsync(process.Id);
|
||||
}
|
||||
|
||||
_ = ProcessManager.SpawnProcess<CliProcess>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,18 @@ namespace RemSox.UI.CLI.Commands
|
||||
|
||||
public string Description => "Start the YesNt interpreter";
|
||||
|
||||
private int processId;
|
||||
|
||||
public async Task ExecuteAsync(string? arguments, Action<string> printLine)
|
||||
{
|
||||
int processId = ProcessManager.SpawnProcess<Processes.YesNtInterpreterProcess>(arguments?.Split(',') ?? []);
|
||||
processId = ProcessManager.SpawnProcess<Processes.YesNtInterpreterProcess>(arguments?.Split(',') ?? []);
|
||||
await ProcessManager.WaitForProcessExitAsync(processId);
|
||||
}
|
||||
|
||||
public Task StopAsync()
|
||||
{
|
||||
ProcessManager.StopProcess(processId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,9 @@ public interface ICommand
|
||||
/// <param name="arguments">The arguments provided to the command.</param>
|
||||
/// <param name="printLine">A delegate to stream output lines to the current console or terminal.</param>
|
||||
Task ExecuteAsync(string? arguments, Action<string> printLine);
|
||||
|
||||
/// <summary>
|
||||
/// Interrupts the command if it's currently running. This is called when the user presses Ctrl+C in the CLI.
|
||||
/// </summary>
|
||||
Task StopAsync() => Task.CompletedTask;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user