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

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

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

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

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
@yorickpeterse yorickpeterse self-assigned this Sep 2, 2024
@yorickpeterse yorickpeterse added this to the 0.17.0 milestone Sep 2, 2024
yorickpeterse added a commit that referenced this issue Sep 2, 2024
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
yorickpeterse added a commit that referenced this issue Sep 2, 2024
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
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