Skip to content

Commit

Permalink
Simplify stdin and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Sep 30, 2024
1 parent fc0bf0b commit bedb272
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 29 deletions.
10 changes: 4 additions & 6 deletions Src/GBX.NET.Tool.CLI/ArgsResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ArgsResolver(string[] args, HttpClient http)

public ToolSettings Resolve(ConsoleSettings consoleOptions)
{
if (!HasArgs)
if (!HasArgs && !Console.IsInputRedirected)
{
return new ToolSettings { ConsoleSettings = consoleOptions };
}
Expand All @@ -31,15 +31,13 @@ public ToolSettings Resolve(ConsoleSettings consoleOptions)
// - check for folders
// - check for stdin (maybe?)
// - check for configured user data path
var stream = new BufferedStream(Console.OpenStandardInput());

if (stream.CanRead)
if (Console.IsInputRedirected)
{
inputs.Add(new StandardInputArgument(stream));
inputs.Add(new StandardInputArgument(Console.OpenStandardInput()));
inputs.Add(new StandardTextInputArgument(Console.In));
}

inputs.Add(new StandardTextInputArgument(Console.In));

var argsEnumerator = args.AsEnumerable().GetEnumerator();

while (argsEnumerator.MoveNext())
Expand Down
22 changes: 2 additions & 20 deletions Src/GBX.NET.Tool.CLI/InputArguments/StandardInputArgument.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
using GBX.NET.Exceptions;

namespace GBX.NET.Tool.CLI.InputArguments;
namespace GBX.NET.Tool.CLI.InputArguments;

public sealed record StandardInputArgument(Stream Stream) : InputArgument
{
public override async Task<object?> ResolveAsync(CancellationToken cancellationToken)
public override Task<object?> ResolveAsync(CancellationToken cancellationToken)
{
var position = Stream.Position;

try
{
if (Gbx.IsCompressed(Stream))
{
Stream.Position = position;
return await Gbx.ParseAsync(Stream, cancellationToken: cancellationToken);
}
}
catch (NotAGbxException)
{

}

Stream.Position = position;
return Task.FromResult((object?)Stream);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

namespace GBX.NET.Tool.CLI.InputArguments;
namespace GBX.NET.Tool.CLI.InputArguments;

public sealed record StandardTextInputArgument(TextReader Reader) : InputArgument
{
Expand Down
2 changes: 1 addition & 1 deletion Src/GBX.NET.Tool.CLI/ToolConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static async Task<ToolConsoleRunResult<T>> RunAsync(string[] args, ToolCo
AnsiConsole.WriteException(ex);
}

if (!tool.noPause)
if (!tool.noPause && !Console.IsInputRedirected)
{
PressAnyKeyToContinue();
}
Expand Down

0 comments on commit bedb272

Please sign in to comment.