Fix #16 by having WriteableStream use ‘print’ when writing to standard output.
This way print's buffering when standard output is not a terminal does not rearrange the order in which output is printed when using both 'print' and 'main.stdout.writeln'. See also https://bugs.swift.org/browse/SR-1127 and http://stackoverflow.com/a/13933741/96587 .
This commit is contained in:
@@ -59,19 +59,27 @@ public final class WriteableStream : OutputStreamType {
|
||||
public let filehandle: NSFileHandle
|
||||
let encoding: NSStringEncoding
|
||||
|
||||
/** Write anything to the stream. */
|
||||
/** Write the textual representation of `x` to the stream. */
|
||||
public func write <T> (x: T) {
|
||||
filehandle.write(x, encoding: encoding)
|
||||
if filehandle.fileDescriptor == STDOUT_FILENO {
|
||||
print(x, terminator: "")
|
||||
} else {
|
||||
filehandle.write(x, encoding: encoding)
|
||||
}
|
||||
}
|
||||
|
||||
/** Write anything to the stream, and add a newline. */
|
||||
/** Write the textual representation of `x` to the stream, and add a newline. */
|
||||
public func writeln <T> (x: T) {
|
||||
filehandle.writeln(x, encoding: encoding)
|
||||
if filehandle.fileDescriptor == STDOUT_FILENO {
|
||||
print(x)
|
||||
} else {
|
||||
filehandle.writeln(x, encoding: encoding)
|
||||
}
|
||||
}
|
||||
|
||||
/** Write a newline to the stream. */
|
||||
public func writeln () {
|
||||
filehandle.writeln("")
|
||||
writeln("")
|
||||
}
|
||||
|
||||
/** Close the stream. Must be called on local streams when finished writing. */
|
||||
|
||||
Reference in New Issue
Block a user