add: accept Ctrl+C while wait-ing
This commit is contained in:
parent
01d16d4cfd
commit
40f3f2163c
1 changed files with 10 additions and 0 deletions
10
cmd_wait.go
10
cmd_wait.go
|
@ -5,7 +5,9 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shu-go/gli"
|
"github.com/shu-go/gli"
|
||||||
|
@ -34,6 +36,11 @@ func (c waitCmd) Run(args []string) error {
|
||||||
ctx, _ = context.WithTimeout(context.Background(), c.Timeout.Duration())
|
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:
|
waitLoop:
|
||||||
for {
|
for {
|
||||||
wins, err := listAllWindows()
|
wins, err := listAllWindows()
|
||||||
|
@ -53,6 +60,9 @@ waitLoop:
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
case <-signalChan:
|
||||||
|
fmt.Fprintln(os.Stderr, "cancelled")
|
||||||
|
break waitLoop
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
fmt.Fprintln(os.Stderr, "cancelled")
|
fmt.Fprintln(os.Stderr, "cancelled")
|
||||||
break waitLoop
|
break waitLoop
|
||||||
|
|
Loading…
Reference in a new issue