Os.stdout
func Os.stdout(): File.HandleOs.stdout returns the program's standard output, which print and println write to as well. It is not buffered; File.buffered(Os.stdout()) buffers a program that writes much.
Details
- Every write is handed to the OS before it returns, so
print,println, writes to the handle, and the output of child programs come out in the order they were written. - A write whose reader has gone, a broken pipe, ends the program as SIGPIPE does, and a shell reports status 141, so
ant run gen.ant | headstops onceheadhas read enough. Where no signal comes, as on Windows,printandprintlnstill end the program with status 141, and theFilefunctions returnErr. - File.close on it does nothing and is
Ok; standard output stays open.
Basic examples
Write a line to standard output through its handle:
try Os.stdout().writeLine("Hello, Ant")Hello, Antprintln writes to the same stream, and the two come out in program order:
println("first")
try Os.stdout().writeLine("second")
println("third")first
second
thirdBuffer it for a loop of many small writes, and flush it at the end:
let out = File.buffered(Os.stdout())
for i in Iter.range(1, 4) {
try out.writeLine("row ${i}")
}
try out.flush()row 1
row 2
row 3Scope
File.writeText writes without a newline, and File.writeChunk writes bytes as they are:
let out = Os.stdout()
try out.writeText("no newline, ")
try out.writeChunk("then bytes\n".toBytes())no newline, then bytesA function that takes a File.Handle writes to standard output or to a file alike. Its final File.flush does nothing on standard output and writes out a buffered handle:
func report(out: File.Handle, counts: Map[string, int]): Result[void, Error] {
for { key, value } in counts {
try out.writeLine("${key}: ${value}")
}
out.flush()
}
let counts = Map{ "ants": 3, "bees": 1 }
try report(Os.stdout(), counts)
let file = try File.create("counts.txt")
defer { file.close() }
try report(file, counts)ants: 3
bees: 1A child program started with Cmd.run writes to the same standard output, in order with the program's own writes:
println("before")
try Cmd.run(Cmd.new("echo", ["from the child"]))
println("after")before
from the child
afterApplications
Copy standard input to standard output, as cat does, with File.pipe:
try File.pipe(Os.stdin(), Os.stdout())one
twoPrompt for a reply. Standard output is not buffered, so the prompt shows before the read waits, with no flush:
try Os.stdout().writeText("Name: ")
let name = try Os.stdin().readLine() ?? "stranger"
println("Hello, ${name}")Name: Hello, AdaHere the reply comes from a pipe, so it does not echo; at a terminal it shows after the prompt.
Color the output only when it goes to a terminal. Here it goes to a pipe, so the text is plain:
let out = Os.stdout()
let ok = if out.isTerminal() { "\u{1b}[32mok\u{1b}[0m" } else { "ok" }
try out.writeLine("build: ${ok}")build: okReport a failed write as an error, which println does not do:
func emit(lines: Array[string]): Result[void, Error] {
let text = lines | String.join("\n")
Os.stdout().writeLine(text) | Result.wrapErr("write output")
}
try emit(["alpha", "beta"])alpha
betaProperties and relations
Standard output is not buffered, so a flush does nothing and is Ok; a close is Ok and leaves it open:
let out = Os.stdout()
assert(out.flush() is Ok)
assert(out.close() is Ok)
assert(out.writeLine("still open") is Ok)still openprintln(s) writes what File.writeLine on Os.stdout() writes:
let s = "same line"
println(s)
try Os.stdout().writeLine(s)same line
same linePossible issues
A println goes past the buffer of a buffered standard output, so it comes out before what the buffer holds:
let out = File.buffered(Os.stdout())
try out.writeLine("written first")
println("printed second")
try out.flush()printed second
written firstA buffered standard output that is neither flushed nor closed loses what it holds when the program ends. This program prints nothing:
let out = File.buffered(Os.stdout())
try out.writeLine("lost")Close the buffered handle right after making it. The close writes out what the buffer holds and leaves standard output open:
let out = File.buffered(Os.stdout())
defer { out.close() }
try out.writeLine("kept")keptSee also
Os.stderr · Os.stdin · print · println · File.buffered · File.writeLine · File.isTerminal
