aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <ds@doozer.de>2017-04-06 13:06:53 +0200
committerDimitri Sokolyuk <ds@doozer.de>2017-04-06 13:06:53 +0200
commit87ea657307db0fe1282f380e7f3f34360b9b4d82 (patch)
tree737bbd709f10676665bd153e5b03ccae9d4d4cb6
parentffbc5ac02e8e934ccfbcc4825c12ea618673688f (diff)
Add interrupt
-rw-r--r--main.go14
1 files changed, 13 insertions, 1 deletions
diff --git a/main.go b/main.go
index b85198a..0b91791 100644
--- a/main.go
+++ b/main.go
@@ -5,6 +5,8 @@ import (
"flag"
"fmt"
"log"
+ "os"
+ "os/signal"
"time"
)
@@ -78,16 +80,26 @@ func notifier(ctx context.Context) chan string {
return c
}
+func atInterrupt(cancel context.CancelFunc) {
+ c := make(chan os.Signal)
+ 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)
c := notifier(ctx)
for s := o.doWork; s != nil; s = s(ctx, c) {
}
}
-const timeBase = time.Second
+const timeBase = time.Minute
func main() {
var o Opt