From 7109fe10f62236b0bbabbda6325143f4855833c4 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 7 Apr 2017 00:10:50 +0200 Subject: ... --- main.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 97f9839..24cfb3c 100644 --- a/main.go +++ b/main.go @@ -19,12 +19,17 @@ type Opt struct { run int } -type stateFn func(context.Context, chan string) stateFn +type display struct { + s string + d time.Duration +} + +type stateFn func(context.Context, chan display) stateFn -func (o *Opt) doWork(ctx context.Context, c chan string) stateFn { +func (o *Opt) doWork(ctx context.Context, c chan display) stateFn { t := time.NewTimer(o.Work) defer t.Stop() - c <- fmt.Sprintf("Work %v run #%v", o.Work, o.run+1) + c <- display{"Work", o.Work} select { case <-ctx.Done(): return nil @@ -33,17 +38,17 @@ func (o *Opt) doWork(ctx context.Context, c chan string) stateFn { } } -func (o *Opt) doBreak(_ context.Context, _ chan string) stateFn { +func (o *Opt) doBreak(_ context.Context, _ chan display) stateFn { if o.run++; o.run%o.Runs == 0 { return o.longBreak } return o.shortBreak } -func (o *Opt) shortBreak(ctx context.Context, c chan string) stateFn { +func (o *Opt) shortBreak(ctx context.Context, c chan display) stateFn { t := time.NewTimer(o.Short) defer t.Stop() - c <- fmt.Sprintf("Short %v", o.Short) + c <- display{"Short", o.Short} select { case <-ctx.Done(): return nil @@ -52,10 +57,10 @@ func (o *Opt) shortBreak(ctx context.Context, c chan string) stateFn { } } -func (o *Opt) longBreak(ctx context.Context, c chan string) stateFn { +func (o *Opt) longBreak(ctx context.Context, c chan display) stateFn { t := time.NewTimer(o.Long) defer t.Stop() - c <- fmt.Sprintf("Long %v", o.Long) + c <- display{"Long", o.Long} select { case <-ctx.Done(): return nil @@ -64,8 +69,8 @@ func (o *Opt) longBreak(ctx context.Context, c chan string) stateFn { } } -func notifier(ctx context.Context) chan string { - c := make(chan string) +func notifier(ctx context.Context) chan display { + c := make(chan display) go func() { defer close(c) for v := range c { @@ -73,7 +78,7 @@ func notifier(ctx context.Context) chan string { case <-ctx.Done(): return default: - log.Println(v) + fmt.Println(v.s, v.d) } } }() @@ -99,7 +104,7 @@ func (o *Opt) pomodoro() { } } -const timeBase = time.Minute +const timeBase = time.Second func main() { var o Opt -- cgit v1.2.3