| 1 | |
| 2 | |
| 3 | |
| 4 | package uiutil |
| 5 | |
| 6 | import ( |
| 7 | "io" |
| 8 | "os" |
| 9 | |
| 10 | "github.com/AlecAivazis/survey/v2" |
| 11 | "github.com/AlecAivazis/survey/v2/terminal" |
| 12 | "github.com/mattn/go-isatty" |
| 13 | ) |
| 14 | |
| 15 | var InterruptErr = terminal.InterruptErr |
| 16 | |
| 17 | |
| 18 | func Confirm(message string, defaultParam bool) (bool, error) { |
| 19 | var ans bool |
| 20 | prompt := &survey.Confirm{ |
| 21 | Message: message, |
| 22 | Default: defaultParam, |
| 23 | } |
| 24 | if err := survey.AskOne(prompt, &ans); err != nil { |
| 25 | return false, err |
| 26 | } |
| 27 | return ans, nil |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | func Select(message string, options []string) (int, error) { |
| 33 | var ans int |
| 34 | prompt := &survey.Select{ |
| 35 | Message: message, |
| 36 | Options: options, |
| 37 | } |
| 38 | if err := survey.AskOne(prompt, &ans); err != nil { |
| 39 | return -1, err |
| 40 | } |
| 41 | return ans, nil |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | func InputIsTTY(reader io.Reader) bool { |
| 47 | return reader == os.Stdin && (isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd())) |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | func OutputIsTTY(writer io.Writer) bool { |
| 53 | |
| 54 | |
| 55 | if os.Getenv("_LIMA_OUTPUT_IS_TTY") != "" { |
| 56 | return true |
| 57 | } |
| 58 | return writer == os.Stdout && (isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())) |
| 59 | } |