vezel-dev / cathode

A terminal-centric replacement for the .NET console APIs.
https://docs.vezel.dev/cathode
BSD Zero Clause License
91 stars 7 forks source link

Add Terminal.ReadRaw Span? #62

Closed xoofx closed 2 years ago

xoofx commented 2 years ago

Would it be ok to add the following? When processing input in raw mode, you can receive a batch of bytes at once that is more suited for span parsing (e.g mouse events, paste of printable text...etc.)

Terminal.ReadRaw(Span<byte>);
Terminal.ReadRawAsync(Memory<byte>);
VirtualTerminal.ReadRaw(Span<byte>);
VirtualTerminal.ReadRawAsync(Memory<byte>);

I can make a PR if you want.

alexrp commented 2 years ago

Note that these methods sort of exist already on TerminalReader, they just lack convenience wrappers on Terminal and VirtualTerminal.

namespace System.IO;

public abstract class TerminalReader : TerminalHandle
{
    public int Read(Span<byte> value, CancellationToken cancellationToken = default);
    public async ValueTask<int> ReadAsync(Memory<byte> value, CancellationToken cancellationToken = default);
}

They will read as many bytes as you request or return at EOF (or cancellation).

Alternatively, if you want it to return at any non-zero amount of bytes or EOF, use ReadBuffer{Async}.

I do plan to add convenience wrappers for these, and also to get rid of ReadRaw in its current form. Will get around to it soon.