waf / CSharpRepl

A command line C# REPL with syntax highlighting – explore the language, libraries and nuget packages interactively.
https://fuqua.io/CSharpRepl/
Mozilla Public License 2.0
2.95k stars 112 forks source link

Throws on redirected stdin #282

Closed Emik03 closed 1 year ago

Emik03 commented 1 year ago

Version

0.6.3

What happened?

Run any of echo "Hello, World!" | csharprepl, csharprepl </dev/null, or otherwise redirects the standard input in any way.

Expected

Detect that stdin is redirected, then if possible, collect stdin in its entirety, evaluate it, then exit. Otherwise, print to stderr along the lines of "Cannot read redirected stdin", and exit.

If this feature is supported, it should be mentioned that the case of yes | csharprepl can happen. I suppose freezing indefinitely is the only logical output as it collects yyyyyyyyyy... forever until memory runs out.

Actual

Throws an exception.

emik@spicy-linux:~$ echo "Hello, World!" | csharprepl
Welcome to the C# REPL (Read Eval Print Loop)!
Type C# expressions and statements at the prompt and press Enter to evaluate them.
Type help to learn more, exit to quit, and clear to clear your terminal.

> Unhandled exception. System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
   at System.ConsolePal.ReadKey(Boolean intercept)
   at PrettyPrompt.Consoles.SystemConsole.ReadKey(Boolean intercept)
   at PrettyPrompt.Consoles.KeyPress.ReadForever(IConsole console)+MoveNext()
   at PrettyPrompt.Prompt.ReadLineAsync()
   at CSharpRepl.ReadEvalPrintLoop.RunAsync(Configuration config) in D:\a\CSharpRepl\CSharpRepl\CSharpRepl\ReadEvalPrintLoop.cs:line 48
   at CSharpRepl.Program.Main(String[] args) in D:\a\CSharpRepl\CSharpRepl\CSharpRepl\Program.cs:line 51
   at CSharpRepl.Program.Main(String[] args) in D:\a\CSharpRepl\CSharpRepl\CSharpRepl\Program.cs:line 57
   at CSharpRepl.Program.<Main>(String[] args)
Aborted (core dumped)
waf commented 1 year ago

Howdy. This is not something that's really supported by CSharpRepl -- possibly it's a better fit for dotnet-script?

It probably shouldn't throw an exception, though, so that could definitely be improved. I'll leave this open to have some sort of sensible behavior here. Thanks!

Emik03 commented 1 year ago

Ah, it does appear to be a better fit, but considering it's essentially 1 implementation detail from working, I don't see why not to implement it anyway while you're already working on preventing the exception from occuring.

Emik03 commented 1 year ago

Plus, it doesn't support preview features, which is a bit of a bummer too considering I just made the full switch to it.

image

waf commented 1 year ago

I'll add stdin support; you're right it's a very easy feature to add. I might not document it as a fully supported scenario, though, because I'm not really wanting to turn CSharpRepl into a scriptCS / dotnet-script environment.

Emik03 commented 1 year ago

Alright, thank you so much, and good luck with the implementation!

waf commented 1 year ago

Hi, this is now available in the main branch. I'm looking at getting a few other fixes in before making a release. Thanks for opening the issue with this suggestion!