diff --git a/CliWrap.Tests/CancellationSpecs.cs b/CliWrap.Tests/CancellationSpecs.cs index 6f4ed0c0..d8e0c530 100644 --- a/CliWrap.Tests/CancellationSpecs.cs +++ b/CliWrap.Tests/CancellationSpecs.cs @@ -141,8 +141,8 @@ public async Task I_can_execute_a_command_with_buffering_and_cancel_it_gracefull var ex = await Assert.ThrowsAnyAsync( async () => await cmd.ExecuteBufferedAsync( - Console.OutputEncoding, - Console.OutputEncoding, + Encoding.Default, + Encoding.Default, CancellationToken.None, cts.Token ) @@ -209,8 +209,8 @@ public async Task I_can_execute_a_command_as_a_pull_based_event_stream_and_cance { await foreach ( var cmdEvent in cmd.ListenAsync( - Console.OutputEncoding, - Console.OutputEncoding, + Encoding.Default, + Encoding.Default, CancellationToken.None, cts.Token ) @@ -289,8 +289,8 @@ public async Task I_can_execute_a_command_as_a_push_based_event_stream_and_cance var ex = await Assert.ThrowsAnyAsync( async () => await cmd.Observe( - Console.OutputEncoding, - Console.OutputEncoding, + Encoding.Default, + Encoding.Default, CancellationToken.None, cts.Token ) diff --git a/CliWrap/Buffered/BufferedCommandExtensions.cs b/CliWrap/Buffered/BufferedCommandExtensions.cs index 4cf9ff8c..290bd0ed 100644 --- a/CliWrap/Buffered/BufferedCommandExtensions.cs +++ b/CliWrap/Buffered/BufferedCommandExtensions.cs @@ -121,7 +121,7 @@ public static CommandTask ExecuteBufferedAsync( /// Executes the command asynchronously with buffering. /// Data written to the standard output and standard error streams is decoded as text /// and returned as part of the result object. - /// Uses for decoding. + /// Uses for decoding. /// /// /// This method can be awaited. @@ -131,6 +131,6 @@ public static CommandTask ExecuteBufferedAsync( CancellationToken cancellationToken = default ) { - return command.ExecuteBufferedAsync(Console.OutputEncoding, cancellationToken); + return command.ExecuteBufferedAsync(Encoding.Default, cancellationToken); } } diff --git a/CliWrap/Command.PipeOperators.cs b/CliWrap/Command.PipeOperators.cs index 54fa75e0..91f6d35c 100644 --- a/CliWrap/Command.PipeOperators.cs +++ b/CliWrap/Command.PipeOperators.cs @@ -25,7 +25,7 @@ public partial class Command /// /// Creates a new command that pipes its standard output to the specified string builder. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |(Command source, StringBuilder target) => @@ -34,7 +34,7 @@ public partial class Command /// /// Creates a new command that pipes its standard output line-by-line to the specified /// asynchronous delegate. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |( @@ -45,7 +45,7 @@ Func target /// /// Creates a new command that pipes its standard output line-by-line to the specified /// asynchronous delegate. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |(Command source, Func target) => @@ -54,7 +54,7 @@ Func target /// /// Creates a new command that pipes its standard output line-by-line to the specified /// synchronous delegate. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |(Command source, Action target) => @@ -81,7 +81,7 @@ Func target /// /// Creates a new command that pipes its standard output and standard error to the /// specified string builders. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |( @@ -94,7 +94,7 @@ Func target /// /// Creates a new command that pipes its standard output and standard error line-by-line /// to the specified asynchronous delegates. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |( @@ -108,7 +108,7 @@ Func stdErr /// /// Creates a new command that pipes its standard output and standard error line-by-line /// to the specified asynchronous delegates. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |( @@ -119,7 +119,7 @@ Func stdErr /// /// Creates a new command that pipes its standard output and standard error line-by-line /// to the specified synchronous delegates. - /// Uses for decoding. + /// Uses for decoding. /// [Pure] public static Command operator |( diff --git a/CliWrap/EventStream/PullEventStreamCommandExtensions.cs b/CliWrap/EventStream/PullEventStreamCommandExtensions.cs index 6d4fd15f..b6a01269 100644 --- a/CliWrap/EventStream/PullEventStreamCommandExtensions.cs +++ b/CliWrap/EventStream/PullEventStreamCommandExtensions.cs @@ -67,6 +67,7 @@ await channel forcefulCancellationToken, gracefulCancellationToken ); + yield return new StartedCommandEvent(commandTask.ProcessId); // Close the channel once the command completes, so that ReceiveAsync() can finish @@ -83,7 +84,9 @@ await channel await foreach ( var cmdEvent in channel.ReceiveAsync(forcefulCancellationToken).ConfigureAwait(false) ) + { yield return cmdEvent; + } var exitCode = await commandTask.Select(r => r.ExitCode).ConfigureAwait(false); yield return new ExitedCommandEvent(exitCode); @@ -127,7 +130,7 @@ public static IAsyncEnumerable ListenAsync( /// /// Executes the command as a pull-based event stream. - /// Uses for decoding. + /// Uses for decoding. /// /// /// Use pattern matching to handle specific instances of . @@ -137,6 +140,6 @@ public static IAsyncEnumerable ListenAsync( CancellationToken cancellationToken = default ) { - return command.ListenAsync(Console.OutputEncoding, cancellationToken); + return command.ListenAsync(Encoding.Default, cancellationToken); } } diff --git a/CliWrap/EventStream/PushEventStreamCommandExtensions.cs b/CliWrap/EventStream/PushEventStreamCommandExtensions.cs index 1f95f126..d398726b 100644 --- a/CliWrap/EventStream/PushEventStreamCommandExtensions.cs +++ b/CliWrap/EventStream/PushEventStreamCommandExtensions.cs @@ -122,7 +122,7 @@ public static IObservable Observe( /// /// Executes the command as a push-based event stream. - /// Uses for decoding. + /// Uses for decoding. /// /// /// Use pattern matching to handle specific instances of . @@ -132,6 +132,6 @@ public static IObservable Observe( CancellationToken cancellationToken = default ) { - return command.Observe(Console.OutputEncoding, cancellationToken); + return command.Observe(Encoding.Default, cancellationToken); } } diff --git a/CliWrap/PipeTarget.cs b/CliWrap/PipeTarget.cs index 4ed7946b..1034421d 100644 --- a/CliWrap/PipeTarget.cs +++ b/CliWrap/PipeTarget.cs @@ -217,10 +217,10 @@ public static PipeTarget ToStringBuilder(StringBuilder stringBuilder, Encoding e /// /// Creates a pipe target that writes to the specified string builder. - /// Uses for decoding. + /// Uses for decoding. /// public static PipeTarget ToStringBuilder(StringBuilder stringBuilder) => - ToStringBuilder(stringBuilder, Console.OutputEncoding); + ToStringBuilder(stringBuilder, Encoding.Default); /// /// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream. @@ -239,19 +239,22 @@ Encoding encoding BufferSizes.StreamReader, true ); + await foreach ( var line in reader.ReadAllLinesAsync(cancellationToken).ConfigureAwait(false) ) + { await handleLineAsync(line, cancellationToken).ConfigureAwait(false); + } } ); /// /// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream. - /// Uses for decoding. + /// Uses for decoding. /// public static PipeTarget ToDelegate(Func handleLineAsync) => - ToDelegate(handleLineAsync, Console.OutputEncoding); + ToDelegate(handleLineAsync, Encoding.Default); /// /// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream. @@ -261,10 +264,10 @@ public static PipeTarget ToDelegate(Func handleLineAsync, Encoding /// /// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream. - /// Uses for decoding. + /// Uses for decoding. /// public static PipeTarget ToDelegate(Func handleLineAsync) => - ToDelegate(handleLineAsync, Console.OutputEncoding); + ToDelegate(handleLineAsync, Encoding.Default); /// /// Creates a pipe target that invokes the specified synchronous delegate on every line written to the stream. @@ -281,10 +284,10 @@ public static PipeTarget ToDelegate(Action handleLine, Encoding encoding /// /// Creates a pipe target that invokes the specified synchronous delegate on every line written to the stream. - /// Uses for decoding. + /// Uses for decoding. /// public static PipeTarget ToDelegate(Action handleLine) => - ToDelegate(handleLine, Console.OutputEncoding); + ToDelegate(handleLine, Encoding.Default); /// /// Creates a pipe target that replicates data over multiple inner targets. diff --git a/Readme.md b/Readme.md index b2765eee..aa581d4a 100644 --- a/Readme.md +++ b/Readme.md @@ -531,7 +531,7 @@ var stdOut = result.StandardOutput; var stdErr = result.StandardError; ``` -By default, `ExecuteBufferedAsync()` assumes that the underlying process uses the default encoding (`Console.OutputEncoding`) for writing text to the console. +By default, `ExecuteBufferedAsync()` assumes that the underlying process uses the default encoding (`Encoding.Default`) for writing text to the console. To override this, specify the encoding explicitly by using one of the available overloads: ```csharp