You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Command-line programs may wish to check if their input/output streams are connected to a terminal/TTY or not, such as when deciding to use colors or not for output. To do so, we introduce the Terminal trait, defined like so:
# A type that may refer to a terminal/TTY.
trait pub Terminal {
# Returns `true` if `self` refers to a terminal/TTY.
#
# If `self` doesn't refer to a valid file descriptor, we can't
# determine if it refers to a terminal/TTY, or an error occurred, `false` is
# returned.
fn pub terminal? -> Bool
}
This trait is then implemented by STDIN, STDOUT, STDERR, ReadOnlyFile, and the other file types. It's not implemented by sockets, because that doesn't make any sense. Once implemented, you'd use it like so:
import std.stdio.STDOUT
let stdout = STDOUT.new
...
if stdout.terminal? { ... } else { ... }
Implementation wise this just uses isatty() from libc on Unix platforms, similar to the atty crate.
For this to work, we need access to the raw file descriptors of files. That in turn means moving the file IO over to the standard library, because I don't want to add a runtime function just to get a raw file descriptor.
The text was updated successfully, but these errors were encountered:
yorickpeterse
added
feature
New things to add to Inko, such as a new standard library module
std
Changes related to the standard library
labels
Oct 31, 2023
This adds Stdout.terminal?, Stderr.terminal? and Stdin.terminal?. These
methods are used for checking if the stream is connected to a
terminal/TTY or not.
This fixes#634.
Changelog: added
This adds Stdout.terminal?, Stderr.terminal? and Stdin.terminal?. These
methods are used for checking if the stream is connected to a
terminal/TTY or not.
This fixes#634.
Changelog: added
Description
Command-line programs may wish to check if their input/output streams are connected to a terminal/TTY or not, such as when deciding to use colors or not for output. To do so, we introduce the
Terminal
trait, defined like so:This trait is then implemented by
STDIN
,STDOUT
,STDERR
,ReadOnlyFile
, and the other file types. It's not implemented by sockets, because that doesn't make any sense. Once implemented, you'd use it like so:Implementation wise this just uses
isatty()
from libc on Unix platforms, similar to the atty crate.For this to work, we need access to the raw file descriptors of files. That in turn means moving the file IO over to the standard library, because I don't want to add a runtime function just to get a raw file descriptor.
Blocked by
Related work
The text was updated successfully, but these errors were encountered: