aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-04-09 11:44:34 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-04-09 11:44:34 +0200
commit195c098360cf67e4918b069b905c89d6d510fac4 (patch)
treecf04eb205cc04d8dc9725c2b86754829f6e9d787
parent0167d97b3e4f40a58f25eb39e4034d08dbca51c4 (diff)
wip
-rw-r--r--main.go37
-rw-r--r--progress.go40
2 files changed, 58 insertions, 19 deletions
diff --git a/main.go b/main.go
index 3cbe411..2cc50c4 100644
--- a/main.go
+++ b/main.go
@@ -3,7 +3,6 @@ package main
import (
"context"
"flag"
- "fmt"
"log"
"os"
"os/signal"
@@ -17,10 +16,10 @@ type Opt struct {
Day time.Duration
Runs int
run int
- disp chan display
+ do chan do
}
-type display struct {
+type do struct {
s string
d time.Duration
}
@@ -28,13 +27,13 @@ type display struct {
type stateFn func(context.Context) stateFn
func (o *Opt) doWork(ctx context.Context) stateFn {
- t := time.NewTimer(o.Work)
- defer t.Stop()
- o.disp <- display{"work", o.Work}
+ timer := time.NewTimer(o.Work)
+ defer timer.Stop()
+ o.do <- do{"work", o.Work}
select {
case <-ctx.Done():
return nil
- case <-t.C:
+ case <-timer.C:
return o.doBreak
}
}
@@ -47,31 +46,31 @@ func (o *Opt) doBreak(_ context.Context) stateFn {
}
func (o *Opt) shortBreak(ctx context.Context) stateFn {
- t := time.NewTimer(o.Short)
- defer t.Stop()
- o.disp <- display{"short", o.Short}
+ timer := time.NewTimer(o.Short)
+ defer timer.Stop()
+ o.do <- do{"short", o.Short}
select {
case <-ctx.Done():
return nil
- case <-t.C:
+ case <-timer.C:
return o.doWork
}
}
func (o *Opt) longBreak(ctx context.Context) stateFn {
- t := time.NewTimer(o.Long)
- defer t.Stop()
- o.disp <- display{"long", o.Long}
+ timer := time.NewTimer(o.Long)
+ defer timer.Stop()
+ o.do <- do{"long", o.Long}
select {
case <-ctx.Done():
return nil
- case <-t.C:
+ case <-timer.C:
return o.doWork
}
}
-func notifier(ctx context.Context) chan display {
- c := make(chan display)
+func notifier(ctx context.Context) chan do {
+ c := make(chan do)
go func() {
defer close(c)
for v := range c {
@@ -79,7 +78,7 @@ func notifier(ctx context.Context) chan display {
case <-ctx.Done():
return
default:
- fmt.Println("Do", v.s, v.d)
+ count(ctx, v.s, v.d)
}
}
}()
@@ -100,7 +99,7 @@ func (o *Opt) pomodoro() {
ctx, cancel := context.WithTimeout(context.Background(), o.Day)
defer cancel()
atInterrupt(cancel)
- o.disp = notifier(ctx)
+ o.do = notifier(ctx)
for s := o.doWork; s != nil; s = s(ctx) {
}
}
diff --git a/progress.go b/progress.go
new file mode 100644
index 0000000..51effbe
--- /dev/null
+++ b/progress.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "strings"
+ "time"
+)
+
+/*
+__________..........__________..........__________..........__________..........
+Directory does not contain SHA256.sig. Continue without verification? [no] yes
+Installing xshare58.tgz 100% |********************......| 4358 KB 00:12 ETA
+Work 100% |**********************....| 25m0s ETA
+*/
+
+func progress(current, max time.Duration) string {
+ width := time.Duration(40)
+ n := width * current / max
+ done := strings.Repeat("*", int(n))
+ left := strings.Repeat(".", int(width-n))
+ return fmt.Sprintf("|%v%v| %3d%%", done, left, 100*current/max)
+}
+
+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 {
+ select {
+ case <-ctx.Done():
+ fmt.Printf("\r%-20s %5s %v", s, d, progress(d, d))
+ return
+ default:
+ fmt.Printf("\r%-20s %5s %v", s, d, progress(t.Sub(start), d))
+ }
+ }
+}