diff --git a/cmd_wait.go b/cmd_wait.go index 35b24ea..d2ac64c 100644 --- a/cmd_wait.go +++ b/cmd_wait.go @@ -1,7 +1,10 @@ package main import ( + "context" "errors" + "fmt" + "os" "strings" "time" @@ -13,6 +16,7 @@ type waitCmd struct { Closed bool `help:"wait until the window is closed"` Intervals gli.Duration `cli:"intervals,i=DURATION" default:"1s"` + Timeout gli.Duration `cli:"timeout=DURATION" default:"0s" help:"zelo value means ininite"` } func (c waitCmd) Run(args []string) error { @@ -23,6 +27,14 @@ func (c waitCmd) Run(args []string) error { an := ancestors() t := strings.ToLower(args[0]) + var ctx context.Context + if c.Timeout == 0 { + ctx = context.Background() + } else { + ctx, _ = context.WithTimeout(context.Background(), c.Timeout.Duration()) + } + +waitLoop: for { wins, err := listAllWindows() if err != nil { @@ -40,6 +52,14 @@ func (c waitCmd) Run(args []string) error { } } + select { + case <-ctx.Done(): + fmt.Fprintln(os.Stderr, "cancelled") + break waitLoop + default: + //nop + } + time.Sleep(c.Intervals.Duration()) }