Skip to content

Commit

Permalink
Fix redirected input
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Nov 23, 2023
1 parent a4644b1 commit 86581ba
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public bool TryParse(CommonReplaceCommandOptions options, ParseContext context)
pipeMode = pipeMode2;
}

string? redirectedInput = ConsoleHelpers.ReadRedirectedInput();

if (pipeMode == CommandLine.PipeMode.Paths
|| pipeMode == CommandLine.PipeMode.Text)
{
if (!Console.IsInputRedirected)
if (redirectedInput is null)
{
context.WriteError("Redirected/piped input is required "
+ $"when option '{OptionNames.GetHelpText(OptionNames.Pipe)}' is specified.");
Expand Down Expand Up @@ -143,7 +145,7 @@ public bool TryParse(CommonReplaceCommandOptions options, ParseContext context)

if (options.IsDefaultPath()
&& pipeMode != CommandLine.PipeMode.Paths
&& Console.IsInputRedirected)
&& redirectedInput is not null)
{
if (input is not null)
{
Expand All @@ -161,7 +163,7 @@ public bool TryParse(CommonReplaceCommandOptions options, ParseContext context)
return false;
}

input = ConsoleHelpers.ReadRedirectedInput();
input = redirectedInput;
}

var displayParts = DisplayParts.None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@ protected virtual bool TryParsePaths(out ImmutableArray<PathInfo> paths, ParseCo
paths = paths.AddRange(pathsFromFile);
}

if (Console.IsInputRedirected
ImmutableArray<string> redirectedInput = ConsoleHelpers.ReadRedirectedInputAsLines();

if (!redirectedInput.IsDefault
&& PipeMode == CommandLine.PipeMode.Paths)
{
if (!TryEnsureFullPath(
ConsoleHelpers.ReadRedirectedInputAsLines().Where(f => !string.IsNullOrEmpty(f)),
redirectedInput.Where(f => !string.IsNullOrEmpty(f)),
PathOrigin.RedirectedInput,
context,
out ImmutableArray<PathInfo> pathsFromInput))
Expand Down
8 changes: 5 additions & 3 deletions src/CommandLine/CommandLineOptions/FindCommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public bool TryParse(FindCommandOptions options, ParseContext context)
pipeMode = pipeMode2;
}

string? redirectedInput = ConsoleHelpers.ReadRedirectedInput();

if (pipeMode == CommandLine.PipeMode.Paths
|| pipeMode == CommandLine.PipeMode.Text)
{
if (!Console.IsInputRedirected)
if (redirectedInput is null)
{
context.WriteError("Redirected/piped input is required "
+ $"when option '{OptionNames.GetHelpText(OptionNames.Pipe)}' is specified.");
Expand All @@ -86,7 +88,7 @@ public bool TryParse(FindCommandOptions options, ParseContext context)

if (options.IsDefaultPath()
&& pipeMode != CommandLine.PipeMode.Paths
&& Console.IsInputRedirected)
&& redirectedInput is not null)
{
if (options.ContentFilter is null)
{
Expand All @@ -96,7 +98,7 @@ public bool TryParse(FindCommandOptions options, ParseContext context)
return false;
}

input = ConsoleHelpers.ReadRedirectedInput();
input = redirectedInput;

if (options.Paths.Length == 1
&& options.Paths[0].Origin == PathOrigin.CurrentDirectory
Expand Down
28 changes: 25 additions & 3 deletions src/CommandLine/ConsoleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Orang.CommandLine;

Expand All @@ -29,26 +30,47 @@ public static void WaitForKeyPress(string? message = null)
using (Stream stream = Console.OpenStandardInput())
using (var streamReader = new StreamReader(stream, Console.InputEncoding))
{
return streamReader.ReadToEnd();
Task<string> readToEndTask = streamReader.ReadToEndAsync();

// https://github.com/dotnet/runtime/issues/95079
if (!readToEndTask.Wait(TimeSpan.FromMilliseconds(500)))
return null;

return readToEndTask.Result;
}
}

return null;
}

public static IEnumerable<string> ReadRedirectedInputAsLines()
public static ImmutableArray<string> ReadRedirectedInputAsLines()
{
if (Console.IsInputRedirected)
{
ImmutableArray<string>.Builder lines = ImmutableArray.CreateBuilder<string>();

using (Stream stream = Console.OpenStandardInput())
using (var streamReader = new StreamReader(stream, Console.InputEncoding))
{
Task<string?> readLineTask = streamReader.ReadLineAsync();

// https://github.com/dotnet/runtime/issues/95079
if (!readLineTask.Wait(TimeSpan.FromMilliseconds(500)))
return default;

if (readLineTask.Result is null)
return ImmutableArray<string>.Empty;

string? line;

while ((line = streamReader.ReadLine()) is not null)
yield return line;
lines.Add(line);
}

return lines.ToImmutableArray();
}

return ImmutableArray<string>.Empty;
}

public static bool AskToExecute(string text, string? indent = null)
Expand Down
2 changes: 1 addition & 1 deletion src/CommandLine/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"CommandLine": {
"commandName": "Project",
"commandLineArgs": "help -v d"
"commandLineArgs": "find -e dll"
}
}
}

0 comments on commit 86581ba

Please sign in to comment.