How can I check from my Swift script whether or not I am outputting to a terminal?
In a bash or zsh script, for example, this can be accomplished with the -t 1
conditional expression to check if file descriptor 1 (stdout) is open and associated with a terminal device:
if [[ -t 1 ]]; then
echo "\e[31m"HELLO"\e[0m" # colorize output
else
echo hello
fi
Notice the difference in formatting when the script is run in an interactive shell and not being piped to another program:
./script
↪︎ HELLO
./script | tee
↪︎ hello
What is the equivalent in Swift?
#!/usr/bin/env swift
if ❓ {
print("\u{001B}[31m" "HELLO" "\u{001B}[0m")
} else {
print("hello")
}
CodePudding user response:
The isatty
function returns 1
if the given file descriptor is associated with a terminal. Like many C library functions, isatty
can be called from Swift via the Darwin module:
import Darwin
if isatty(STDOUT_FILENO) == 1 {
print("\u{001B}[31m" "HELLO" "\u{001B}[0m")
} else {
print("hello")
}
To make this run on both macOS and Linux, replace the import Darwin
by
#if os(Linux)
import Glibc
#else
import Darwin
#endif