ShellOut icon indicating copy to clipboard operation
ShellOut copied to clipboard

Added `Handle` protocol to get asynchronous output

Open gwikiera opened this issue 7 years ago • 1 comments

I've created a protocol Handle to handle command output asynchronously. All previously FileHandle arguments of shellOut methods have been replaced by Handle. FileHandle implements Handle so it's completely backward compatible. StringHandle converts Data output into String one.

It fixes #11.

gwikiera avatar Jan 26 '18 08:01 gwikiera

This looks good. @JohnSundell would you like any changes before merging this?

We could then add the implementation from @SteveBarnegren in #22 so that users can use FileHandle.standardOuput & FileHandle.standardError. I've tested out #22 locally.

private extension FileHandle {
     var shouldBeClosed: Bool {
         return self !== FileHandle.standardOutput &&
             self !== FileHandle.standardError &&
             self !== FileHandle.standardInput
     }
 }

extension FileHandle: Handle {
    public func handle(data: Data) {
        write(data)
    }
    
    public func endHandling() {
        if shouldBeClosed { closeFile() }
       // alternative using naming from #22
       // if !isStandard { closeFile() }
    }
}

iainsmith avatar Apr 10 '18 23:04 iainsmith