Skip to content

Commit

Permalink
restart
Browse files Browse the repository at this point in the history
  • Loading branch information
drgrib committed Apr 25, 2020
1 parent 175b4a8 commit fd81e7f
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package agent

import (
. "fmt"
"github.com/0xAX/notificator"
ui "github.com/gizak/termui"
"math"
"time"

"github.com/0xAX/notificator"
ui "github.com/gizak/termui"
)

//////////////////////////////////////////////
Expand Down Expand Up @@ -56,31 +57,6 @@ func (t *Timer) Start(d time.Duration) {
}
// strip monotonic time to account for system changes
t.end = time.Now().Add(t.duration).Round(0)
}

func (t *Timer) update() {
t.status = "Finished"
now := time.Now()
if !now.After(t.end) {
exactLeft := t.end.Sub(now)
floorSeconds := math.Floor(exactLeft.Seconds())
t.left = time.Duration(floorSeconds) * time.Second
t.status = Sprintf("%v", t.left)
if t.Debug {
t.status += "\n"
t.status += Sprintf("\nnow: %v", now)
t.status += Sprintf("\nexactLeft: %v", exactLeft)
t.status += Sprintf("\nt.end: %v", t.end)
t.status += Sprintf("\nt.end.Sub(now): %v", t.end.Sub(now))
}
}
}

func (t *Timer) CountDown() {
// init and close
err := ui.Init()
mustBeNil(err)
defer ui.Close()

// init notificator
notify := notificator.New(notificator.Options{
Expand All @@ -105,6 +81,31 @@ func (t *Timer) CountDown() {
notify.Push(
"", "Finished", "", notificator.UR_CRITICAL)
}()
}

func (t *Timer) update() {
t.status = "Finished\n\n[r]estart\n[q]uit"
now := time.Now()
if !now.After(t.end) {
exactLeft := t.end.Sub(now)
floorSeconds := math.Floor(exactLeft.Seconds())
t.left = time.Duration(floorSeconds) * time.Second
t.status = Sprintf("%v", t.left)
if t.Debug {
t.status += "\n"
t.status += Sprintf("\nnow: %v", now)
t.status += Sprintf("\nexactLeft: %v", exactLeft)
t.status += Sprintf("\nt.end: %v", t.end)
t.status += Sprintf("\nt.end.Sub(now): %v", t.end.Sub(now))
}
}
}

func (t *Timer) CountDown() {
// init and close
err := ui.Init()
mustBeNil(err)
defer ui.Close()

// init cell
cell := ui.NewPar("")
Expand Down Expand Up @@ -152,6 +153,13 @@ func (t *Timer) CountDown() {
ui.StopLoop()
})

// handle restart
ui.Handle("/sys/kbd/r", func(ui.Event) {
if time.Now().After(t.end) {
t.Start(t.duration)
}
})

// start loop
ui.Loop()
}

0 comments on commit fd81e7f

Please sign in to comment.