AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.08k stars 351 forks source link

Shell hangs when used in $(...) command-substitution #267

Closed pbnj closed 2 years ago

pbnj commented 4 years ago

Thank you for this library.

I have run into a reproducible bug when survey is used in a command-substitution, i.e. $(...).

Here is an exmaple using the examples/simple.go in this repo.

This works fine:

$ go run simple.go
? What is your name? Pbnj
? Choose a color: red
Pbnj chose red.

This hangs:

$ TEST=$(go run simple.go)
# shell hangs completely
# ^Z doesn't work
# ^D doesn't work
# ^C doesn't work
# [ENTER] doesn't work

I was expecting to get the same prompts and once answered, echo $TEST would output Pbnj chose red.

I have also tried handling interrupts per the FAQ:

if err == terminal.InterruptErr {
    fmt.Println("interrupted")
    os.Exit(1)
} else if err != nil {
    panic(err)
}

But that didn't work in this case.

Input, Multiline, Password, and Confirm seem to be the most problematic ones as they hang the entire shell session, where as Select and MultiSelect can exit with [ENTER] or terminate with ^C without a problem (although the output is not what I expected, but that can be a separate issue).

What I am trying to accomplish is a CLI utility inspired by this Rust project

mislav commented 2 years ago

Thanks for reporting. Shell command substitution captures stdout of the command, but Survey only works if both stdin and stdout are attached to a terminal. Right now, what you are describing is not possible.

Follow https://github.com/AlecAivazis/survey/issues/328 for updates