Skip to content

Implement a method to check if a stream is a terminal/TTY #634

@yorickpeterse

Description

@yorickpeterse

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:

# 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.

Blocked by

Related work

Metadata

Metadata

Assignees

Labels

featureNew things to add to Inko, such as a new standard library modulestdChanges related to the standard library

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions