Core\Cli::isTty
Core\Cli::isTty(Core\Cli::Stream $stream): boolCore\Cli::isTty( Core\Cli::Stream $stream, ): bool
Reports whether one of the three standard streams is a terminal.
A program's output goes to a terminal, or to a file, or to another program. Core\Cli::isTty tells you which. Ask about standard output before you print colours or draw a progress line: a file keeps those characters, and the person who reads the file later sees them as noise. Ask about standard input before you prompt, because nobody is there to answer when the input comes from a file.
The answer is resolved once, when the program starts, so two calls in one run always agree.
Good to know: each of the three streams can answer differently. Standard output is a terminal while standard input is a pipe whenever somebody runs cat orders.txt | your-program.
The examples below ask about each stream, choose what to print when there is no terminal, and skip a question nobody can answer.
Parameters
$streamCore\Cli::StreamWhich stream to check. Each stream is checked on its own. A common case is standard output on a terminal while standard input is a pipe.
Return value
Returns bool. true when that stream is a terminal, false when it is a pipe, a file or closed.
Examples
Ask about each stream
<?nvs// Shows how to ask whether a stream is a terminal. `Core\Cli::isTty` takes one// of the three `Core\Cli::Stream` cases and returns `true` or `false`.
// Standard input is where a person types. Standard output carries the result.// Standard error carries the messages about the work.array<string> $names = ['input', 'output', 'error'];array<bool> $terminal = [ Core\Cli::isTty(Core\Cli::Stream::In), Core\Cli::isTty(Core\Cli::Stream::Out), Core\Cli::isTty(Core\Cli::Stream::Err),];
int $i = 0;while ($i < 3) { // Each stream is reported on its own line. All three say "no" here, because // this program runs without a terminal. echo $names[$i], ' ', $terminal[$i] ? 'yes' : 'no', "\n"; $i = $i + 1;}input nooutput noerror noChoose what to print when there is no terminal
<?nvs// Shows how a program prints one way for a person and another way for a file.// A separator line as wide as the window is useful on screen. In a log file it// is noise.
if (Core\Cli::isTty(Core\Cli::Stream::Out)) { // There is a terminal, so draw a line across it. echo Core\Str::repeat('-', Core\Cli::width()), "\n"; echo "working\n";} else { // The output goes to a file or to another program. This prints one short // line and nothing else. echo "working\n";}workingSkip a question nobody can answer
<?nvs// Shows the everyday use: a script asks a question when a person is there, and// takes a sensible value when nobody is.
tainted string $branch = 'main';
// Standard input is not a terminal when a build server runs the program, or// when the input comes from a file. Nobody can answer, so do not ask.if (Core\Cli::isTty(Core\Cli::Stream::In)) { $branch = Core\Cli::ask('Which branch?', { default: 'main' });}
echo 'deploying ', $branch, "\n";deploying main