aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 2ad95384f0ab75d5e13c3468cab1df0df723baaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"strings"
	"time"
)

type Opt struct {
	Work  time.Duration
	Short time.Duration
	Long  time.Duration
	Day   time.Duration
	Runs  int
	run   int
	do    chan do
}

type do struct {
	s string
	d time.Duration
}

type stateFn func(context.Context) stateFn

func (o *Opt) doWork(ctx context.Context) stateFn {
	timer := time.NewTimer(o.Work)
	defer timer.Stop()
	o.do <- do{"work", o.Work}
	select {
	case <-ctx.Done():
		return nil
	case <-timer.C:
		return o.doBreak
	}
}

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) stateFn {
	timer := time.NewTimer(o.Short)
	defer timer.Stop()
	o.do <- do{"short", o.Short}
	select {
	case <-ctx.Done():
		return nil
	case <-timer.C:
		return o.doWork
	}
}

func (o *Opt) longBreak(ctx context.Context) stateFn {
	timer := time.NewTimer(o.Long)
	defer timer.Stop()
	o.do <- do{"long", o.Long}
	select {
	case <-ctx.Done():
		return nil
	case <-timer.C:
		return o.doWork
	}
}

func notifier(ctx context.Context) chan do {
	c := make(chan do)
	go func() {
		defer close(c)
		for v := range c {
			select {
			case <-ctx.Done():
				return
			default:
				count(ctx, v.s, v.d)
			}
		}
	}()
	return c
}

func atInterrupt(cancel context.CancelFunc) {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		<-c
		cancel()
	}()
}

func (o *Opt) pomodoro() {
	defer func(t time.Time) { log.Printf("total %v", time.Since(t)) }(time.Now())
	ctx, cancel := context.WithTimeout(context.Background(), o.Day)
	defer cancel()
	atInterrupt(cancel)
	o.do = notifier(ctx)
	for s := o.doWork; s != nil; s = s(ctx) {
	}
}

const timeBase = time.Second

func main() {
	var o Opt
	flag.DurationVar(&o.Work, "work", 25*timeBase, "work time")
	flag.DurationVar(&o.Short, "short", 5*timeBase, "short break")
	flag.DurationVar(&o.Long, "long", 15*timeBase, "long break")
	flag.DurationVar(&o.Day, "day", 600*timeBase, "work day")
	flag.IntVar(&o.Runs, "runs", 4, "work runs")
	flag.Parse()
	o.pomodoro()
}

func progress(current, max time.Duration) string {
	if current > max {
		current = max
	}
	width := time.Duration(40)
	n := width * current / max
	done := strings.Repeat("*", int(n))
	left := strings.Repeat(".", int(width-n))
	return fmt.Sprintf("%3d%% |%v%v| %6s/%-6s",
		100*current/max, done, left,
		current-current%time.Second, max-max%time.Second)
}

func count(ctx context.Context, s string, d time.Duration) {
	ctx, _ = context.WithTimeout(ctx, d)
	start := time.Now()
	defer fmt.Print("\n")
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	for t := range ticker.C {
		fmt.Printf("\r%-16s %v", s, progress(t.Sub(start), d))
		select {
		case <-ctx.Done():
			return
		default:
		}
	}
}