madelson/MedallionShell

Tee?

Closed this issue · 3 comments

I'm trying to find a way to use Command as a tee, so it's both outputs to writer (console) and returns standard output as result. Is that possible with current API?

Hi @yevhen there isn't currently a way to both have it retain the buffered content and stream it out to console. However, it shouldn't be too difficult to do this yourself:

var teeTextWriter = new TeeTextWriter();

await Command.Run(...).RedirectTo(teeTextWriter).Task; // will print to stdout
var output = teeTextWriter.ToString();

private class TeeTextWriter : TextWriter
{
    private readonly StringBuilder _stringBuilder = new StringBuilder();

    public override void Write(char ch)
    {
        this._stringBuilder.Append(ch);
        Console.Out.Write(ch);
    }

    public override string ToString() => this._stringBuilder.ToString();
}

Filed #71 as a potential enhancement request for the future.

@madelson ye, thanks! I did something very similar ))

BTW, thanks for the great project!