Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yorickpeterse opened this issue Oct 31, 2023 · 0 comments
Open

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

yorickpeterse opened this issue Oct 31, 2023 · 0 comments
Labels
feature New things to add to Inko, such as a new standard library module std Changes related to the standard library

Comments

@yorickpeterse
Copy link
Collaborator

yorickpeterse commented Oct 31, 2023

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

@yorickpeterse 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New things to add to Inko, such as a new standard library module std Changes related to the standard library
Projects
None yet
Development

No branches or pull requests

1 participant