diff --git a/README.md b/README.md index 4b7a76c..4bafede 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ print( "The time and date is " + $("date -u") ) #### Pipe several commands together ```swift -run("echo piped to the next command") |> run("wc -w") |> writeTo(standardoutput) +run("echo piped to the next command") |> run("wc -w") |>> standardoutput ``` #### Read a file line by line @@ -40,7 +40,7 @@ for line in open(filename).lines() { ```swift let directories = environment["PATH"]!.split(":") for directory in directories { - run("find \"\(directory)\" -type f -perm +ugo+x -print") |> writeTo(standardoutput) + run("find \"\(directory)\" -type f -perm +ugo+x -print") |>> standardoutput } ``` @@ -49,7 +49,7 @@ or more Functionally: ```swift environment["PATH"]! |> split(":") |> map { dir in run("find \"\(dir)\" -type f -perm +ugo+x -print") } - |> writeTo(standardoutput) + |>> standardoutput ``` #### Print standard input with line numbers diff --git a/SwiftShell/Stream.swift b/SwiftShell/Stream.swift index a97d330..6c23918 100644 --- a/SwiftShell/Stream.swift +++ b/SwiftShell/Stream.swift @@ -142,6 +142,7 @@ public func split (delimiter: String = "\n")(stream: ReadableStreamType) -> Sequ return SequenceOf({StringStreamGenerator (stream: stream, delimiter: delimiter)}) } + /** Write something to a stream. @@ -151,7 +152,6 @@ public func writeTo (stream: WriteableStreamType)(input: T) { stream.write( toString(input) ) } -// needed to avoid `write(SequenceType)` being called instead, // needed to avoid `writeTo(SequenceType)` being called instead, // treating the string as a sequence of characters. /** Write a String to a writable stream. */ @@ -165,3 +165,26 @@ public func writeTo (stream: WriteableStreamType)(seq: S) { item |> writeTo(stream) } } + +infix operator |>> { precedence 50 associativity left } + +/** +Write something to a stream. + +something |>> writablestream +*/ +public func |>> (input: T, stream: WriteableStreamType) { + writeTo(stream)(input: input) +} + +// needed to avoid `func |>> ` being called instead, +// treating the string as a sequence of characters. +/** Write a String to a writable stream. */ +public func |>> (text: String, stream: WriteableStreamType) { + writeTo(stream)(input: text) +} + +/** Write a sequence to a stream. */ +public func |>> (seq: S, stream: WriteableStreamType) { + writeTo(stream)(seq: seq) +} diff --git a/SwiftShellTests/Scripts/callswiftscriptfromswift.swift b/SwiftShellTests/Scripts/callswiftscriptfromswift.swift index 65cebc2..4381539 100755 --- a/SwiftShellTests/Scripts/callswiftscriptfromswift.swift +++ b/SwiftShellTests/Scripts/callswiftscriptfromswift.swift @@ -2,4 +2,4 @@ import SwiftShell -run("cat onetwothree.txt") |> run("./print_linenumbers.swift") |> writeTo(standardoutput) +run("cat onetwothree.txt") |> run("./print_linenumbers.swift") |>> standardoutput diff --git a/SwiftShellTests/Scripts/print_linenumbers.swift b/SwiftShellTests/Scripts/print_linenumbers.swift index 19c8523..64be275 100755 --- a/SwiftShellTests/Scripts/print_linenumbers.swift +++ b/SwiftShellTests/Scripts/print_linenumbers.swift @@ -11,4 +11,4 @@ for line in standardinput.lines() { */ var i = 1 -standardinput.lines() |> map {line in "line \(i++): \(line)\n"} |> writeTo(standardoutput) +standardinput.lines() |> map {line in "line \(i++): \(line)\n"} |>> standardoutput diff --git a/SwiftShellTests/Scripts/stream_out.swift b/SwiftShellTests/Scripts/stream_out.swift index 14b078e..73f6493 100755 --- a/SwiftShellTests/Scripts/stream_out.swift +++ b/SwiftShellTests/Scripts/stream_out.swift @@ -2,4 +2,4 @@ import SwiftShell -run("echo this is streamed") |> run("wc -w") |> writeTo(standardoutput) +run("echo this is streamed") |> run("wc -w") |>> standardoutput diff --git a/SwiftShellTests/Stream_Tests.swift b/SwiftShellTests/Stream_Tests.swift index d841a16..1290270 100644 --- a/SwiftShellTests/Stream_Tests.swift +++ b/SwiftShellTests/Stream_Tests.swift @@ -38,7 +38,7 @@ class Stream_Tests: XCTestCase { func testPrintStreamToStream () { var (writable, readable) = streams() - stream("this goes in") |> writeTo(writable) + stream("this goes in") |>> writable XCTAssertEqual( readable.readSome()!, "this goes in") } @@ -46,7 +46,7 @@ class Stream_Tests: XCTestCase { func testPrintStreamToStreamInPieces () { var (writable, readable) = streams() - stream(["this ", "goes", " in"]) |> writeTo(writable) + stream(["this ", "goes", " in"]) |>> writable writable.closeStream() XCTAssertEqual( readable.read(), "this goes in") @@ -55,7 +55,7 @@ class Stream_Tests: XCTestCase { func testPrintStringToStream () { var (writable, readable) = streams() - "this goes in" |> writeTo(writable) + "this goes in" |>> writable XCTAssertEqual( readable.readSome()!, "this goes in") } @@ -63,7 +63,7 @@ class Stream_Tests: XCTestCase { func testCommandChainToStream () { var (writable, readable) = streams() - SwiftShell.run("echo this is streamed") |> SwiftShell.run("wc -w") |> writeTo(writable) + SwiftShell.run("echo this is streamed") |> SwiftShell.run("wc -w") |>> writable XCTAssertEqual( readable.readSome()!.trim(), "3") } @@ -72,7 +72,7 @@ class Stream_Tests: XCTestCase { var (writable, readable) = streams() // make sure the array isn't printed as a Printable. - SequenceOf([stream("line 1"), stream("line 2"), stream("line 3")].generate()) |> writeTo(writable) + SequenceOf([stream("line 1"), stream("line 2"), stream("line 3")].generate()) |>> writable XCTAssertEqual( readable.readSome()!.trim(), "line 1line 2line 3") } @@ -83,7 +83,7 @@ class Stream_Tests: XCTestCase { dict["test"]! |> split(":") |> map { line in SwiftShell.run("echo \(line)") } - |> writeTo(writable) + |>> writable XCTAssertEqual( readable.readSome()!.trim(), "line 1\nline 2\nline 3") } @@ -92,7 +92,7 @@ class Stream_Tests: XCTestCase { var (writable, readable) = streams() var i = 1 - stream("line 1\nline 2\nline 3").lines() |> map {line in "line \(i++): \(line)\n"} |> writeTo(writable) + stream("line 1\nline 2\nline 3").lines() |> map {line in "line \(i++): \(line)\n"} |>> writable XCTAssertEqual( readable.readSome()!.trim(), "line 1: line 1\nline 2: line 2\nline 3: line 3") }