Add operator |>> for printing anything to a writable stream.
Replaced all uses of writeTo with the new operator in unit tests, test scripts and readme.
This commit is contained in:
@@ -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
|
||||
|
||||
+24
-1
@@ -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 <T>(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 <S : SequenceType>(stream: WriteableStreamType)(seq: S) {
|
||||
item |> writeTo(stream)
|
||||
}
|
||||
}
|
||||
|
||||
infix operator |>> { precedence 50 associativity left }
|
||||
|
||||
/**
|
||||
Write something to a stream.
|
||||
|
||||
something |>> writablestream
|
||||
*/
|
||||
public func |>> <T>(input: T, stream: WriteableStreamType) {
|
||||
writeTo(stream)(input: input)
|
||||
}
|
||||
|
||||
// needed to avoid `func |>> <S : SequenceType>` 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 |>> <S : SequenceType>(seq: S, stream: WriteableStreamType) {
|
||||
writeTo(stream)(seq: seq)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user