From 40f3f2163ceea027be3c087eb84e72399f38e050 Mon Sep 17 00:00:00 2001 From: ShuheiKubota Date: Mon, 18 Nov 2019 16:09:56 +0900 Subject: [PATCH] add: accept Ctrl+C while wait-ing --- cmd_wait.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd_wait.go b/cmd_wait.go index d2ac64c..6acc21d 100644 --- a/cmd_wait.go +++ b/cmd_wait.go @@ -5,7 +5,9 @@ import ( "errors" "fmt" "os" + "os/signal" "strings" + "syscall" "time" "github.com/shu-go/gli" @@ -34,6 +36,11 @@ func (c waitCmd) Run(args []string) error { ctx, _ = context.WithTimeout(context.Background(), c.Timeout.Duration()) } + signalChan := make(chan os.Signal, 1) + signal.Notify(signalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt) + + fmt.Println("Press Ctrl+C to cancel.") + waitLoop: for { wins, err := listAllWindows() @@ -53,6 +60,9 @@ waitLoop: } select { + case <-signalChan: + fmt.Fprintln(os.Stderr, "cancelled") + break waitLoop case <-ctx.Done(): fmt.Fprintln(os.Stderr, "cancelled") break waitLoop