Search code examples
crystal-lang

Is there a way of using "native" Crystal to be determine if it's running in an interactive shell session?


If a Crystal executable is running, what would be the recommended way of determining if it's running in an interactive environment?

For example:

`tty`.strip != "not a tty"

But, using native Crystal.


Solution

  • It's IO#tty? method.

    Compile a file (foo.cr) with the content:

    puts STDIN.tty?
    

    Run it from the shell:

    ./foo              # => true
    echo "foo" | ./foo # => false