chore: refactored test dummy program
All checks were successful
default / default (8.0) (push) Successful in 46s
All checks were successful
default / default (8.0) (push) Successful in 46s
Refactored the dummy program to always use async commands. Also added a Terminal abstractions to help the problem with char and byte interop when writing or reading data from the std pipes.
This commit is contained in:
parent
54185e7877
commit
7e67992a18
13 changed files with 159 additions and 51 deletions
|
@ -4,30 +4,30 @@ using Spectre.Console.Cli;
|
|||
|
||||
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
|
||||
|
||||
internal sealed class EchoStdinCommand : Command<EchoStdinCommand.Settings>
|
||||
internal sealed class EchoStdinCommand : AsyncOutputCommand<EchoStdinCommand.Settings>
|
||||
{
|
||||
public sealed class Settings : CommandSettings
|
||||
public sealed class Settings : OutputCommandSettings
|
||||
{
|
||||
[CommandOption("--target")] public OutputTarget Target { get; init; } = OutputTarget.StdOut;
|
||||
[CommandOption("--length")] public long Length { get; init; } = long.MaxValue;
|
||||
}
|
||||
|
||||
public override int Execute(CommandContext context, Settings settings)
|
||||
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
|
||||
{
|
||||
using var buffer = MemoryPool<char>.Shared.Rent(81920);
|
||||
using var tty = Terminal.Connect();
|
||||
using var buffer = MemoryPool<byte>.Shared.Rent(81920);
|
||||
|
||||
var count = 0L;
|
||||
while (count < settings.Length)
|
||||
{
|
||||
var bytesWanted = (int)Math.Min(buffer.Memory.Length, settings.Length - count);
|
||||
|
||||
var bytesRead = Console.In.Read(buffer.Memory.Span[..bytesWanted]);
|
||||
var bytesRead = await tty.Stdin.BaseStream.ReadAsync(buffer.Memory[..bytesWanted]);
|
||||
if (bytesRead <= 0)
|
||||
break;
|
||||
|
||||
foreach (var writer in settings.Target.GetWriters())
|
||||
foreach (var writer in tty.GetWriters(settings.Target))
|
||||
{
|
||||
writer.Write(buffer.Memory.Span[..bytesRead]);
|
||||
await writer.BaseStream.WriteAsync(buffer.Memory[..bytesRead]);
|
||||
}
|
||||
|
||||
count += bytesRead;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue