master
Bel LaPointe 2018-05-29 08:11:29 -06:00
parent debfeaa0a6
commit ea0bcfa40d
1 changed files with 35 additions and 12 deletions

47
main.go
View File

@ -67,16 +67,23 @@ func main() {
logger.Log("Quit with 'q', Pause with 'p', Reset with 'r'")
keych := keyChannel()
go func() {
last := time.Now()
printTime(&cur, base, &delim, invert, repeat, eta)
for {
select {
case <-monitor.C:
difference := time.Duration(time.Now().UnixNano() - last.UnixNano())
if difference > time.Second*10 {
keych <- 'p'
continue
}
if invert {
cur += time.Duration(time.Now().UnixNano() - last.UnixNano())
cur += difference
} else {
cur -= time.Duration(time.Now().UnixNano() - last.UnixNano())
cur -= difference
}
last = time.Now()
case state := <-pause:
@ -87,7 +94,7 @@ func main() {
monitor.Stop()
}
case <-stop:
logger.Log("")
logger.Logf()
confirm <- true
return
}
@ -95,15 +102,8 @@ func main() {
}
}()
if err := keyboard.Open(); err != nil {
panic(err)
}
defer keyboard.Close()
for {
b, _, err := keyboard.GetKey()
if err != nil {
panic(err)
}
b := <-keych
switch b {
case 'q':
stop <- true
@ -123,7 +123,7 @@ func main() {
notified = false
printTime(&cur, base, &delim, invert, repeat, eta)
case 'z':
logger.Log("\n")
logger.Logf()
printTime(&cur, base, &delim, invert, repeat, eta)
}
}
@ -199,3 +199,26 @@ func alertTime(pRemains *time.Duration, target time.Duration, reverse, repeat bo
}
}
}
func keyChannel() chan rune {
ch := make(chan rune, 20)
go func() {
if err := keyboard.Open(); err != nil {
panic(err)
}
for {
b, _, err := keyboard.GetKey()
if err != nil {
panic(err)
}
by := rune(b)
if by == 'q' {
keyboard.Close()
ch <- by
return
}
ch <- by
}
}()
return ch
}