Skip to content

Commit

Permalink
Use Encoding.Default instead of Console.OutputEncoding as the def…
Browse files Browse the repository at this point in the history
…ault encoding (#262)
  • Loading branch information
Tyrrrz authored Oct 25, 2024
1 parent 5e0bb8e commit 6b8abf8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 29 deletions.
12 changes: 6 additions & 6 deletions CliWrap.Tests/CancellationSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public async Task I_can_execute_a_command_with_buffering_and_cancel_it_gracefull
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(
async () =>
await cmd.ExecuteBufferedAsync(
Console.OutputEncoding,
Console.OutputEncoding,
Encoding.Default,
Encoding.Default,
CancellationToken.None,
cts.Token
)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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<OperationCanceledException>(
async () =>
await cmd.Observe(
Console.OutputEncoding,
Console.OutputEncoding,
Encoding.Default,
Encoding.Default,
CancellationToken.None,
cts.Token
)
Expand Down
4 changes: 2 additions & 2 deletions CliWrap/Buffered/BufferedCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static CommandTask<BufferedCommandResult> 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 <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
/// <remarks>
/// This method can be awaited.
Expand All @@ -131,6 +131,6 @@ public static CommandTask<BufferedCommandResult> ExecuteBufferedAsync(
CancellationToken cancellationToken = default
)
{
return command.ExecuteBufferedAsync(Console.OutputEncoding, cancellationToken);
return command.ExecuteBufferedAsync(Encoding.Default, cancellationToken);
}
}
16 changes: 8 additions & 8 deletions CliWrap/Command.PipeOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class Command

/// <summary>
/// Creates a new command that pipes its standard output to the specified string builder.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(Command source, StringBuilder target) =>
Expand All @@ -34,7 +34,7 @@ public partial class Command
/// <summary>
/// Creates a new command that pipes its standard output line-by-line to the specified
/// asynchronous delegate.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(
Expand All @@ -45,7 +45,7 @@ Func<string, CancellationToken, Task> target
/// <summary>
/// Creates a new command that pipes its standard output line-by-line to the specified
/// asynchronous delegate.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(Command source, Func<string, Task> target) =>
Expand All @@ -54,7 +54,7 @@ Func<string, CancellationToken, Task> target
/// <summary>
/// Creates a new command that pipes its standard output line-by-line to the specified
/// synchronous delegate.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(Command source, Action<string> target) =>
Expand All @@ -81,7 +81,7 @@ Func<string, CancellationToken, Task> target
/// <summary>
/// Creates a new command that pipes its standard output and standard error to the
/// specified string builders.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(
Expand All @@ -94,7 +94,7 @@ Func<string, CancellationToken, Task> target
/// <summary>
/// Creates a new command that pipes its standard output and standard error line-by-line
/// to the specified asynchronous delegates.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(
Expand All @@ -108,7 +108,7 @@ Func<string, CancellationToken, Task> stdErr
/// <summary>
/// Creates a new command that pipes its standard output and standard error line-by-line
/// to the specified asynchronous delegates.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(
Expand All @@ -119,7 +119,7 @@ Func<string, CancellationToken, Task> stdErr
/// <summary>
/// Creates a new command that pipes its standard output and standard error line-by-line
/// to the specified synchronous delegates.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
[Pure]
public static Command operator |(
Expand Down
7 changes: 5 additions & 2 deletions CliWrap/EventStream/PullEventStreamCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -127,7 +130,7 @@ public static IAsyncEnumerable<CommandEvent> ListenAsync(

/// <summary>
/// Executes the command as a pull-based event stream.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent" />.
Expand All @@ -137,6 +140,6 @@ public static IAsyncEnumerable<CommandEvent> ListenAsync(
CancellationToken cancellationToken = default
)
{
return command.ListenAsync(Console.OutputEncoding, cancellationToken);
return command.ListenAsync(Encoding.Default, cancellationToken);
}
}
4 changes: 2 additions & 2 deletions CliWrap/EventStream/PushEventStreamCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static IObservable<CommandEvent> Observe(

/// <summary>
/// Executes the command as a push-based event stream.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
/// <remarks>
/// Use pattern matching to handle specific instances of <see cref="CommandEvent" />.
Expand All @@ -132,6 +132,6 @@ public static IObservable<CommandEvent> Observe(
CancellationToken cancellationToken = default
)
{
return command.Observe(Console.OutputEncoding, cancellationToken);
return command.Observe(Encoding.Default, cancellationToken);
}
}
19 changes: 11 additions & 8 deletions CliWrap/PipeTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ public static PipeTarget ToStringBuilder(StringBuilder stringBuilder, Encoding e

/// <summary>
/// Creates a pipe target that writes to the specified string builder.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
public static PipeTarget ToStringBuilder(StringBuilder stringBuilder) =>
ToStringBuilder(stringBuilder, Console.OutputEncoding);
ToStringBuilder(stringBuilder, Encoding.Default);

/// <summary>
/// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream.
Expand All @@ -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);
}
}
);

/// <summary>
/// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
public static PipeTarget ToDelegate(Func<string, CancellationToken, Task> handleLineAsync) =>
ToDelegate(handleLineAsync, Console.OutputEncoding);
ToDelegate(handleLineAsync, Encoding.Default);

/// <summary>
/// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream.
Expand All @@ -261,10 +264,10 @@ public static PipeTarget ToDelegate(Func<string, Task> handleLineAsync, Encoding

/// <summary>
/// Creates a pipe target that invokes the specified asynchronous delegate on every line written to the stream.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
public static PipeTarget ToDelegate(Func<string, Task> handleLineAsync) =>
ToDelegate(handleLineAsync, Console.OutputEncoding);
ToDelegate(handleLineAsync, Encoding.Default);

/// <summary>
/// Creates a pipe target that invokes the specified synchronous delegate on every line written to the stream.
Expand All @@ -281,10 +284,10 @@ public static PipeTarget ToDelegate(Action<string> handleLine, Encoding encoding

/// <summary>
/// Creates a pipe target that invokes the specified synchronous delegate on every line written to the stream.
/// Uses <see cref="Console.OutputEncoding" /> for decoding.
/// Uses <see cref="Encoding.Default" /> for decoding.
/// </summary>
public static PipeTarget ToDelegate(Action<string> handleLine) =>
ToDelegate(handleLine, Console.OutputEncoding);
ToDelegate(handleLine, Encoding.Default);

/// <summary>
/// Creates a pipe target that replicates data over multiple inner targets.
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b8abf8

Please sign in to comment.