deliver SIGINT to self after Ctrl-C in survey

This commit is contained in:
Alan Donovan 2021-09-09 16:31:15 -04:00
parent af541ad5f2
commit 22f9824ec8

View file

@ -6,9 +6,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"os"
"sort" "sort"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/github/ghcs/api" "github.com/github/ghcs/api"
"golang.org/x/term" "golang.org/x/term"
) )
@ -97,5 +99,16 @@ func ask(qs []*survey.Question, response interface{}) error {
if !hasTTY { if !hasTTY {
return fmt.Errorf("no terminal") return fmt.Errorf("no terminal")
} }
return survey.Ask(qs, response, survey.WithShowCursor(true)) err := survey.Ask(qs, response, survey.WithShowCursor(true))
// The survey package temporarily clears the terminal's ISIG mode bit
// (see tcsetattr(3)) so the QUIT button (Ctrl-C) is reported as
// ASCII \x03 (ETX) instead of delivering SIGINT to the application.
// So we have to serve ourselves the SIGINT.
//
// https://github.com/AlecAivazis/survey/#why-isnt-sending-a-sigint-aka-ctrl-c-signal-working
if err == terminal.InterruptErr {
self, _ := os.FindProcess(os.Getpid())
_ = self.Signal(os.Interrupt) // assumes POSIX
}
return err
} }