From 0167d97b3e4f40a58f25eb39e4034d08dbca51c4 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 7 Apr 2017 09:27:40 +0200 Subject: ... --- main.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 24cfb3c..3cbe411 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ type Opt struct { Day time.Duration Runs int run int + disp chan display } type display struct { @@ -24,12 +25,12 @@ type display struct { d time.Duration } -type stateFn func(context.Context, chan display) stateFn +type stateFn func(context.Context) stateFn -func (o *Opt) doWork(ctx context.Context, c chan display) stateFn { +func (o *Opt) doWork(ctx context.Context) stateFn { t := time.NewTimer(o.Work) defer t.Stop() - c <- display{"Work", o.Work} + o.disp <- display{"work", o.Work} select { case <-ctx.Done(): return nil @@ -38,17 +39,17 @@ func (o *Opt) doWork(ctx context.Context, c chan display) stateFn { } } -func (o *Opt) doBreak(_ context.Context, _ chan display) stateFn { +func (o *Opt) doBreak(_ context.Context) stateFn { if o.run++; o.run%o.Runs == 0 { return o.longBreak } return o.shortBreak } -func (o *Opt) shortBreak(ctx context.Context, c chan display) stateFn { +func (o *Opt) shortBreak(ctx context.Context) stateFn { t := time.NewTimer(o.Short) defer t.Stop() - c <- display{"Short", o.Short} + o.disp <- display{"short", o.Short} select { case <-ctx.Done(): return nil @@ -57,10 +58,10 @@ func (o *Opt) shortBreak(ctx context.Context, c chan display) stateFn { } } -func (o *Opt) longBreak(ctx context.Context, c chan display) stateFn { +func (o *Opt) longBreak(ctx context.Context) stateFn { t := time.NewTimer(o.Long) defer t.Stop() - c <- display{"Long", o.Long} + o.disp <- display{"long", o.Long} select { case <-ctx.Done(): return nil @@ -78,7 +79,7 @@ func notifier(ctx context.Context) chan display { case <-ctx.Done(): return default: - fmt.Println(v.s, v.d) + fmt.Println("Do", v.s, v.d) } } }() @@ -99,8 +100,8 @@ func (o *Opt) pomodoro() { ctx, cancel := context.WithTimeout(context.Background(), o.Day) defer cancel() atInterrupt(cancel) - c := notifier(ctx) - for s := o.doWork; s != nil; s = s(ctx, c) { + o.disp = notifier(ctx) + for s := o.doWork; s != nil; s = s(ctx) { } } -- cgit v1.2.3