Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stop func to stop the task early. added async callbacks for Rea… #18

Merged
merged 2 commits into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/SwiftShell/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public final class AsyncShellTask {
exit(error, file: file, line: line)
}
}

/** Terminate task early. */
public func stop(){
task.terminate()
}

/**
Wait for this shell task to finish.
Expand All @@ -261,6 +266,15 @@ public final class AsyncShellTask {
}
}

extension AsyncShellTask {
public func onCompletion ( handler: ((AsyncShellTask) -> ())? ) -> AsyncShellTask {
task.terminationHandler = { (NSTask) in
handler?(self)
}
return self
}
}

extension ShellRunnable {

/**
Expand Down
23 changes: 18 additions & 5 deletions Sources/SwiftShell/Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@ public final class ReadableStream : Streamable {

/** Let ReadableStream run commands using itself as stdin. */
extension ReadableStream: ShellRunnable {
public var shellcontext: ShellContextType {
var context = ShellContext(main)
context.stdin = self
return context
}
public var shellcontext: ShellContextType {
var context = ShellContext(main)
context.stdin = self
return context
}
}

/** Callback with when ReadableStream has data.*/
extension ReadableStream {
public func onOutput ( handler: ((String) -> ())? ) {
if let h = handler{
filehandle.readabilityHandler = {(NSFileHandle) in
if let output = self.readSome(){
h(output)
}
}
}
}
}

/** An output stream, like standard output or a writeable file. */
Expand Down