process/src/Process.Tests.Dummy/Commands/EchoStdinCommand.cs
Louis Seubert 7e67992a18
All checks were successful
default / default (8.0) (push) Successful in 46s
chore: refactored test dummy program
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.
2024-05-05 20:39:36 +02:00

38 lines
No EOL
975 B
C#

using System.Buffers;
using Spectre.Console.Cli;
namespace Geekeey.Extensions.Process.Tests.Dummy.Commands;
internal sealed class EchoStdinCommand : AsyncOutputCommand<EchoStdinCommand.Settings>
{
public sealed class Settings : OutputCommandSettings
{
[CommandOption("--length")] public long Length { get; init; } = long.MaxValue;
}
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
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 = await tty.Stdin.BaseStream.ReadAsync(buffer.Memory[..bytesWanted]);
if (bytesRead <= 0)
break;
foreach (var writer in tty.GetWriters(settings.Target))
{
await writer.BaseStream.WriteAsync(buffer.Memory[..bytesRead]);
}
count += bytesRead;
}
return 0;
}
}