aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go130
1 files changed, 56 insertions, 74 deletions
diff --git a/main.go b/main.go
index 592f770..6436f08 100644
--- a/main.go
+++ b/main.go
@@ -24,14 +24,53 @@ type Opt struct {
type stateFn func(context.Context) stateFn
-func (o *Opt) display(ctx context.Context, s string) {
- go notify(s)
+func (o *Opt) doWork(ctx context.Context) stateFn {
+ select {
+ case <-ctx.Done():
+ return nil
+ default:
+ ctx, cancel := context.WithTimeout(ctx, o.Work)
+ defer cancel()
+ display(ctx, o.Tick, "do work")
+ }
+ if o.run++; o.run%o.Runs == 0 {
+ return o.longBreak
+ }
+ return o.shortBreak
+}
+
+func (o *Opt) shortBreak(ctx context.Context) stateFn {
+ select {
+ case <-ctx.Done():
+ return nil
+ default:
+ ctx, cancel := context.WithTimeout(ctx, o.Short)
+ defer cancel()
+ display(ctx, o.Tick, "short break")
+ }
+ return o.doWork
+}
+
+func (o *Opt) longBreak(ctx context.Context) stateFn {
+ select {
+ case <-ctx.Done():
+ return nil
+ default:
+ ctx, cancel := context.WithTimeout(ctx, o.Long)
+ defer cancel()
+ display(ctx, o.Tick, "long break")
+ }
+ return o.doWork
+}
+
+func display(ctx context.Context, tick time.Duration, s string) {
+ go notify("Pomodoro timer", s)
dl, ok := ctx.Deadline()
if !ok {
return
}
total := time.Until(dl)
- ticker := time.NewTicker(o.Tick)
+ ticker := time.NewTicker(tick)
defer ticker.Stop()
defer fmt.Println("")
for range ticker.C {
@@ -44,14 +83,6 @@ func (o *Opt) display(ctx context.Context, s string) {
}
}
-func total(t time.Time) {
- fmt.Printf("total %v\n", round(time.Since(t)))
-}
-
-func round(d time.Duration) time.Duration {
- return d - d%time.Second
-}
-
func progress(total, left time.Duration) string {
width := 40
if left < 0 {
@@ -68,44 +99,20 @@ func progress(total, left time.Duration) string {
return s
}
-func (o *Opt) doWork(ctx context.Context) stateFn {
- select {
- case <-ctx.Done():
- return nil
- default:
- ctx, cancel := context.WithTimeout(ctx, o.Work)
- defer cancel()
- o.display(ctx, "do work")
- }
- if o.run++; o.run%o.Runs == 0 {
- return o.longBreak
- }
- return o.shortBreak
-}
-
-func (o *Opt) shortBreak(ctx context.Context) stateFn {
- select {
- case <-ctx.Done():
- return nil
- default:
- ctx, cancel := context.WithTimeout(ctx, o.Short)
- defer cancel()
- o.display(ctx, "short break")
+func notify(title, s string) error {
+ switch runtime.GOOS {
+ case "darwin":
+ msg := fmt.Sprintf("display notification %q with title %q", s, title)
+ return exec.Command("osascript", "-e", msg).Run()
+ case "linux":
+ return exec.Command("notify-send", title, s).Run()
+ default: // *BSD
+ msg := fmt.Sprintf("%s\n\n%s", title, s)
+ return exec.Command("xmessage", "-center", "-timeout", "5", msg).Run()
}
- return o.doWork
}
-func (o *Opt) longBreak(ctx context.Context) stateFn {
- select {
- case <-ctx.Done():
- return nil
- default:
- ctx, cancel := context.WithTimeout(ctx, o.Long)
- defer cancel()
- o.display(ctx, "long break")
- }
- return o.doWork
-}
+func round(d time.Duration) time.Duration { return d - d%time.Second }
func main() {
var o Opt
@@ -126,34 +133,9 @@ func main() {
cancel()
}()
- defer total(time.Now())
+ defer func(t time.Time) {
+ fmt.Printf("total %v\n", round(time.Since(t)))
+ }(time.Now())
for s := o.doWork; s != nil; s = s(ctx) {
}
}
-
-const title = "Pomodoro timer"
-
-func notifyX11(s string) error {
- msg := fmt.Sprintf("%s\n\n%s", title, s)
- return exec.Command("xmessage", "-center", "-timeout", "5", msg).Run()
-}
-
-func notifyOSX(s string) error {
- msg := fmt.Sprintf("display notification %q with title %q", s, title)
- return exec.Command("osascript", "-e", msg).Run()
-}
-
-func notifyLnx(s string) error {
- return exec.Command("notify-send", title, s).Run()
-}
-
-func notify(s string) error {
- switch runtime.GOOS {
- case "darwin":
- return notifyOSX(s)
- case "linux":
- return notifyLnx(s)
- default: // *BSD
- return notifyX11(s)
- }
-}