Search code examples
stdincrystal-lang

how to override stdin with /dev/tty?


I'm writing a git pre-commit hook, but it requires user input and hooks don't run in an interactive terminal. With Python I could do something like this to get access to user input:

#!/usr/bin/python

import sys

# This is required because git hooks are run in non-interactive
# mode. You aren't technically supposed to have access to stdin.
# This hack works on MaxOS and Linux. Mileage may vary on Windows.
sys.stdin = open('/dev/tty')

result = input("Gimme some input: ")

What is the appropriate way to do this in Crystal?


Solution

  • by combining @julian-portalier's answer and @asterite's we have working way to redefine stdin:

    STDIN.reopen(File.open("/dev/tty", "a+"))
    

    Which, I believe, is just FileDescriptor#reopen

    STDIN, STDOUT, and STDERR can all be reopened this way.