File.isTerminal
func File.isTerminal(f: File.Handle): boolFile.isTerminal reports whether f is a terminal, as [[ -t 1 ]] asks of standard output. It is false for a closed handle. A buffered handle answers for the handle under it.
Details
File.isTerminalis false for a file, a pipe, and a network connection.- When a program runs with its output redirected or piped, as in
ant run tool.ant > out.txtorant run tool.ant | less,Os.stdout()is not a terminal. Each stream answers for itself, soOs.stderr()can still be one.
Basic examples
Ask whether standard output is a terminal. The examples on this page run with their output captured, so it is not:
println(File.isTerminal(Os.stdout()))falseA file is never a terminal:
let f = File.create("notes.txt").unwrap()
defer { File.close(f) }
println(File.isTerminal(f))falseScope
Each of the three standard streams answers for itself, through .:
println(Os.stdin().isTerminal())
println(Os.stdout().isTerminal())
println(Os.stderr().isTerminal())false
false
falseBoth ends of a pipe and both ends of a network connection are not terminals:
let pipe = Os.openPipe().unwrap()
defer {
File.close(pipe.read)
File.close(pipe.write)
}
println(pipe.read.isTerminal())
println(pipe.write.isTerminal())
let l = Net.listen("127.0.0.1:0").unwrap()
defer { Net.close(l) }
let conn = Net.dial(l.address()).unwrap()
defer { File.close(conn) }
println(conn.isTerminal())false
false
falseA buffered handle answers for the handle under it:
let out = File.buffered(Os.stdout())
defer { File.close(out) }
println(out.isTerminal())falseApplications
Color the output only when a person is watching it. Captured, as here, the program prints plain text; at a terminal it prints green and red:
func paint(text: string, code: int): string {
if !Os.stdout().isTerminal() {
return text
}
"\u{1b}[${code}m${text}\u{1b}[0m"
}
println(paint("ok", 32))
println(paint("failed", 31))ok
failedPrompt only when the input comes from a keyboard. With input piped in, the prompt would only clutter the output:
func ask(question: string): Result[string, Error] {
if Os.stdin().isTerminal() {
print("${question} ")
}
let line = try File.readLine(Os.stdin())
Result.ok(line ?? "")
}
let name = ask("Your name?").unwrap()
println("Hello, ${name}")Hello, AnnShow progress on standard error, redrawn in place with \r, only when standard error is a terminal. The result still goes to standard output either way:
let steps = ["fetch", "build", "test"]
let live = Os.stderr().isTerminal()
var done = 0
for step in steps {
done += 1
if live {
eprint("\r${done}/${steps.length()} ${step}")
}
}
if live {
eprintln("")
}
println("${done} steps done")3 steps doneProperties and relations
File.buffered does not change the answer, and a file is never a terminal:
let f = File.create("log.txt").unwrap()
defer { File.close(f) }
assert(!f.isTerminal())
assert(File.buffered(f).isTerminal() == f.isTerminal())
assert(File.buffered(Os.stdout()).isTerminal() == Os.stdout().isTerminal())Possible issues
Ask about the handle you write to. Progress written to standard error but guarded by Os.stdout().isTerminal() disappears when the output is redirected to a file, although the person is still watching standard error; guard it with Os.stderr().isTerminal() instead, as in Applications.
See also
File · File.buffered · File.readLine · Os.stdout · Os.stderr · Os.stdin · Os.openPipe
