SwiftShell
SwiftShell copied to clipboard
How to observe `AsyncShellTask` for user input required
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?
Hi, good question. I will take a look when I get back in 2 days. Just a couple of questions:
- are you using SwiftShell from a command-line application/script or a GUI application?
- which version of macOS or Linux?
- is the external script launched using bash?
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 :)
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.
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.
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.
Are there any updates on this? I also need this for Mint https://github.com/yonaskolb/Mint/issues/37
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.
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