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

Support of reading the is_tty status of stdin #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Gordon01
Copy link
Contributor

This PR brings the initial support of reading piped stdin.

This issue is mentioned multiple times here and in dialoguer: #76 #35 console-rs/dialoguer#296 console-rs/dialoguer#170

Most importantly, this PR adds the Term.features().is_input_a_tty() method.
I've added a new type Tty which holds the statuses of all application streams. It encompasses the logic to detect the attendance of the terminal.
It also contains is_tty moved from the Term which follows the old behavior, where only the output streams were considered.

It's debatable, whether should we change the behavior of the Term.is_term() method to also check the input stream attendance.

This itself doesn't solve the problem with reading piped input, the next step would be updating dialoguer to use the new method is_input_a_tty(). I tried the following code and it works. The main drawback is that piped input can only be read once during the application's lifetime. So if the user has multiple inputs, the second and following will be an empty string.

pub fn interact_text_on(mut self, term: &Term) -> Result<T> {
    if !term.features().is_input_a_tty() {
        let mut input = String::new();
        io::stdin().read_to_string(&mut input)?;
        return input
            .trim()
            .parse::<T>()
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()).into());
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant