Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Survey breaks when stdin is piped (eg unix pipe) #328

Open
2 tasks
noerw opened this issue Jan 11, 2021 · 3 comments
Open
2 tasks

Survey breaks when stdin is piped (eg unix pipe) #328

noerw opened this issue Jan 11, 2021 · 3 comments

Comments

@noerw
Copy link
Contributor

noerw commented Jan 11, 2021

Currently survey does not support reading from piped stdin.
The issue lies in several layers:

  • with piped stdin, Cursor.Location() (called by all prompts implicitly) fails with io.EOF:

    survey/terminal/cursor.go

    Lines 106 to 109 in f45c05b

    text, err := reader.ReadSlice(byte('R'))
    if err != nil {
    return nil, err
    }

    Subsequently, no input is read, and control commands printed instead of being applied.

  • RuneReader does not treat io.EOF as end of input, but aborts and returns it as error, which is unnecessary (e.g. MultiLine input waiting for 3 newlines until it continues)

I'm not exactly sure how a piped stdin differs from a terminal stdin, so I don't know how to fix this.
One step surely would be to change Cursor, so that it detects piped stdin, and switches to a NO-OP mode?

@cben
Copy link
Contributor

cben commented Jan 18, 2021

When input is a pipe, survey library is probably not best experience. Do you have a use for the prompts and escape sequences on stdout?
Consider detecting it with https:/mattn/go-isatty and just reading from stdin without prompts.

Or are you using a pipe for testing your program that uses survey?
(I'm playing with that now, with mixed success; I should probably stick to the advice at https:/AlecAivazis/survey#testing
about go-expect, vt10x, and tests inside this repo 😁)

Currently, reading from a pipe you are also likely to hit #282 — terminal state is not cleaned up.

@noerw
Copy link
Contributor Author

noerw commented Jan 18, 2021

When input is a pipe, survey library is probably not best experience. Do you have a use for the prompts and escape sequences on stdout?

Piping stdin & stdout are two separate things, with different use cases: Our program can benefit from being the last element in a pipeline, while primarily used interactively. The prompts also look nice, no matter if they are interactive.

Consider detecting it with https:/mattn/go-isatty and just reading from stdin without prompts.

That's what we currently do, until this is resolved. But there is no reason for Multiline to error on EOF.

@noerw noerw changed the title Multiline should handle EOF (piped stdin) RuneReader should handle EOF (piped stdin) Feb 27, 2021
@saurabhaditya
Copy link

I was trying to use a CLI with heredoc that errored and landed here debugging that. The library used is this 'survey' that does not take bash style heredoc for inputs.

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

No branches or pull requests

3 participants