diff --git a/main.go b/main.go index d6aa312..27ed99a 100644 --- a/main.go +++ b/main.go @@ -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 +}