add: --timeout option in watch command
This commit is contained in:
parent
abbe58ff5e
commit
01d16d4cfd
1 changed files with 20 additions and 0 deletions
20
cmd_wait.go
20
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())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue