From f013d67855055d7f56fb39134d988316554ca771 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 12 Apr 2017 01:47:04 +0200 Subject: tick interval --- main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index de27ce1..9de4825 100644 --- a/main.go +++ b/main.go @@ -15,19 +15,20 @@ type Opt struct { Short time.Duration Long time.Duration Day time.Duration + Tick time.Duration Runs int run int } type stateFn func(context.Context) stateFn -func display(ctx context.Context, s string) { +func (o *Opt) display(ctx context.Context, s string) { dl, ok := ctx.Deadline() if !ok { return } total := time.Until(dl) - ticker := time.NewTicker(time.Second) + ticker := time.NewTicker(o.Tick) defer ticker.Stop() defer fmt.Println("") for range ticker.C { @@ -71,7 +72,7 @@ func (o *Opt) doWork(ctx context.Context) stateFn { default: ctx, cancel := context.WithTimeout(ctx, o.Work) defer cancel() - display(ctx, "do work") + o.display(ctx, "do work") } if o.run++; o.run%o.Runs == 0 { return o.longBreak @@ -86,7 +87,7 @@ func (o *Opt) shortBreak(ctx context.Context) stateFn { default: ctx, cancel := context.WithTimeout(ctx, o.Short) defer cancel() - display(ctx, "short break") + o.display(ctx, "short break") } return o.doWork } @@ -98,7 +99,7 @@ func (o *Opt) longBreak(ctx context.Context) stateFn { default: ctx, cancel := context.WithTimeout(ctx, o.Long) defer cancel() - display(ctx, "long break") + o.display(ctx, "long break") } return o.doWork } @@ -109,6 +110,7 @@ func main() { flag.DurationVar(&o.Short, "short", 5*time.Minute, "short break") flag.DurationVar(&o.Long, "long", 15*time.Minute, "long break") flag.DurationVar(&o.Day, "day", 12*time.Hour, "work day") + flag.DurationVar(&o.Tick, "tick", time.Second, "update interval") flag.IntVar(&o.Runs, "runs", 4, "work runs") flag.Parse() -- cgit v1.2.3