Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor terminal startup method #42

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ To be Released.
* Removed commands sorting code in `CommandContextBase`. [[#38]]
* Integrated usage-related properties(`Summary`, `Description`, `Example`) into
the `CommandUsage` property. [[#40]]
* Added ₩StepProgress₩ to make it easier to work with progress. [[#41]]
* Added `StepProgress` to make it easier to work with progress. [[#41]]
* Added `RunAsync` and `StopAsync` methods to `SystemTerminalBase`. [[#42]]

[#13]: https://github.com/s2quake/commands/pull/13
[#17]: https://github.com/s2quake/commands/pull/17
Expand All @@ -48,6 +49,7 @@ To be Released.
[#38]: https://github.com/s2quake/commands/pull/38
[#40]: https://github.com/s2quake/commands/pull/40
[#41]: https://github.com/s2quake/commands/pull/41
[#42]: https://github.com/s2quake/commands/pull/42


6.0.1
Expand Down
2 changes: 1 addition & 1 deletion example/JSSoft.Commands.Repl/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Task StartAsync()

_terminal = _container.GetExportedValue<SystemTerminal>();
_cancellationTokenSource = new();
return _terminal.StartAsync(_cancellationTokenSource.Token);
return _terminal.RunAsync(_cancellationTokenSource.Token);
}

public void Dispose()
Expand Down
67 changes: 60 additions & 7 deletions src/JSSoft.Terminals/SystemTerminalBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public abstract class SystemTerminalBase : IDisposable
private readonly TextWriter _error;
private string _prompt = string.Empty;
private bool _isDisposed;
private CancellationTokenSource? _cancellationTokenSource;
private Task? _runningTask;

protected SystemTerminalBase()
{
Expand All @@ -44,20 +46,50 @@ protected SystemTerminalBase()

public async Task StartAsync(CancellationToken cancellationToken)
{
if (_cancellationTokenSource is not null)
{
throw new InvalidOperationException("The terminal is already running.");
}

_cancellationTokenSource = new CancellationTokenSource();
OnInitialize(_out, _error);
_runningTask = Task.Run(() => ProcessAsync(_cancellationTokenSource.Token), default);
await Task.CompletedTask;
}

public async Task StopAsync(CancellationToken cancellationToken)
{
if (_cancellationTokenSource is null)
{
throw new InvalidOperationException("The terminal is not running.");
}

_cancellationTokenSource.Cancel();
if (_runningTask is not null)
{
await _runningTask;
}

_cancellationTokenSource.Dispose();
_cancellationTokenSource = null;
}

public async Task RunAsync(CancellationToken cancellationToken)
{
await StartAsync(cancellationToken);
while (cancellationToken.IsCancellationRequested is false)
{
var isEnabled = _terminal.IsEnabled;
var prompt = Prompt;
if (isEnabled is true && _terminal.ReadStringInternal(prompt, cancellationToken) is { } text)
try
{
await ExecuteAsync(_error, text);
await Task.Delay(1, default);
}
catch (TaskCanceledException)
{
break;
}
#pragma warning disable CA2016
await Task.Delay(1);
#pragma warning restore
}

await StopAsync(cancellationToken);
}

public string? ReadString(string prompt, string command) => _terminal.ReadString(prompt, command);
Expand Down Expand Up @@ -116,6 +148,27 @@ protected virtual void OnDispose()

protected abstract Task OnExecuteAsync(string command, CancellationToken cancellationToken);

private async Task ProcessAsync(CancellationToken cancellationToken)
{
while (cancellationToken.IsCancellationRequested is false)
{
var isEnabled = _terminal.IsEnabled;
var prompt = Prompt;
if (isEnabled is true && _terminal.ReadStringInternal(prompt, cancellationToken) is { } text)
{
await ExecuteAsync(_error, text);
}
try
{
await Task.Delay(1, default);
}
catch (TaskCanceledException)
{
break;
}
}
}

private async Task ExecuteAsync(TextWriter error, string text)
{
var consoleControlC = Console.IsInputRedirected is false && Console.TreatControlCAsInput;
Expand Down
Loading