kareman / SwiftShell

A Swift framework for shell scripting.
https://kareman.github.io/SwiftShell
MIT License
1.03k stars 87 forks source link

How to observe `AsyncShellTask` for user input required #26

Open fabiomassimo opened 7 years ago

fabiomassimo commented 7 years ago

What is the best way to observing an AsyncShellTask to understand if it's waiting for user input?

Example

An external script is launched via SwiftShell by using runAsync such that the execution can return immediately. Although, the script could require some user input during its execution. How can we receive a callback for this event such that the application can properly ask to the user the required input?

kareman commented 7 years ago

Hi, good question. I will take a look when I get back in 2 days. Just a couple of questions:

fabiomassimo commented 7 years ago

are you using SwiftShell from a command-line application/script or a GUI application?

I'm using it as part of a WebSocket API I'm building. The user triggers the action and the result, or pending input, are sent back on the socket.

which version of macOS or Linux?

I'm planning to run my application on macOS 10.11.6

is the external script launched using bash?

It can be multiple tasks. In scope there is git command and some ruby executable applications. Does this answer your question?

If you have any input I could investigate to help you with this, let me know :)

kareman commented 7 years ago

This is something like what I had in mind. The problem is the closure you assign to onInput will be called many many times, even if the script/command you are running is not asking for input (that is, not pulling its standard input). Se the aptly named testFoundationWriteabilityHandlerBeingCalledWhenNoInputIsAskedFor unit test.

kareman commented 7 years ago

You will in any case also have to check the stdout of the command to see what input the script is asking for. Also I think many commands will wisely not ask for input if its stdin is not a terminal session. So we will have to trick them into thinking our filehandle actually belongs to a terminal.

kareman commented 7 years ago

I don't know how to be notified only when and if a command asks for input. I'm leaving this open in case someone else does.

yonaskolb commented 6 years ago

Are there any updates on this? I also need this for Mint https://github.com/yonaskolb/Mint/issues/37

toshi0383 commented 6 years ago

I implemented stdin in cmdshelf. You might be interested to the implementation, so FYI.👋 https://github.com/toshi0383/cmdshelf/blob/master/Sources/cmdshelf/Functions.swift#L10

The function name is "shellOut", but not using JohnSundell/ShellOut.

kareman commented 6 years ago

I don’t know any more about this than I did 1 year ago I’m afraid. FileHandle’s writeabilityHandler seems like the right tool for the job but I couldn’t get it to work properly. You can see my attempt here: https://github.com/kareman/SwiftShell/compare/writeablestream-oninput