SwiftShell icon indicating copy to clipboard operation
SwiftShell copied to clipboard

How to observe `AsyncShellTask` for user input required

Open fabiomassimo opened this issue 8 years ago • 8 comments

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?

fabiomassimo avatar Oct 24 '16 08:10 fabiomassimo

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?

kareman avatar Oct 24 '16 09:10 kareman

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 :)

fabiomassimo avatar Oct 24 '16 09:10 fabiomassimo

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 avatar Oct 27 '16 20:10 kareman

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 avatar Oct 27 '16 20:10 kareman

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.

kareman avatar Nov 10 '16 21:11 kareman

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

yonaskolb avatar Dec 18 '17 15:12 yonaskolb

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.

toshi0383 avatar Dec 18 '17 17:12 toshi0383

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

kareman avatar Dec 18 '17 17:12 kareman