master
Bel LaPointe 2018-04-23 07:42:56 -06:00
parent d810f4e3f0
commit 13885f7ba6
1 changed files with 24 additions and 19 deletions

43
main.go
View File

@ -21,12 +21,14 @@ func main() {
var interval string var interval string
var repeat bool var repeat bool
var invert bool var invert bool
var eta bool
flag.BoolVar(&repeat, "repeat", true, "Whether to repeat the timer on complete") flag.BoolVar(&repeat, "repeat", true, "Whether to repeat the timer on complete")
flag.BoolVar(&invert, "stopwatch", false, "Use as a stopwatch") flag.BoolVar(&invert, "stopwatch", false, "Use as a stopwatch")
flag.StringVar(&duration, "duration", "30m", "How long the timer should be") flag.StringVar(&duration, "duration", "30m", "How long the timer should be")
flag.StringVar(&offset, "offset", "0m", "How much time the initial time should skip") flag.StringVar(&offset, "offset", "0m", "How much time the initial time should skip")
flag.StringVar(&interval, "interval", "500ms", "Interval duration") flag.StringVar(&interval, "interval", "500ms", "Interval duration")
flag.BoolVar(&eta, "eta", false, "Whether to display the ending time")
flag.Parse() flag.Parse()
tickerInterval, err := time.ParseDuration(interval) tickerInterval, err := time.ParseDuration(interval)
@ -66,7 +68,7 @@ func main() {
go func() { go func() {
last := time.Now() last := time.Now()
printTime(&cur, base, &delim, invert, repeat) printTime(&cur, base, &delim, invert, repeat, eta)
for { for {
select { select {
case <-monitor.C: case <-monitor.C:
@ -88,7 +90,7 @@ func main() {
confirm <- true confirm <- true
return return
} }
printTime(&cur, base, &delim, invert, repeat) printTime(&cur, base, &delim, invert, repeat, eta)
} }
}() }()
@ -112,25 +114,22 @@ func main() {
case 'r': case 'r':
skip = time.Duration(0) skip = time.Duration(0)
originalStart = time.Now() originalStart = time.Now()
cur = base if invert {
cur = time.Duration(0)
} else {
cur = base
}
notified = false notified = false
printTime(&cur, base, &delim, invert, repeat) printTime(&cur, base, &delim, invert, repeat, eta)
case 'z': case 'z':
fmt.Printf("\n") fmt.Printf("\n")
printTime(&cur, base, &delim, invert, repeat) printTime(&cur, base, &delim, invert, repeat, eta)
/*
fmt.Printf("\nnow:\t%v\nstart:\t%v\ndiff:\t%v\n", time.Now().UnixNano()/int64(time.Second), originalStart.UnixNano()/int64(time.Second), (time.Now().UnixNano()-originalStart.UnixNano())/int64(time.Second))
if invert {
cur = skip + time.Duration(time.Now().UnixNano()-originalStart.UnixNano())
} else {
cur = base - skip - time.Duration(time.Now().UnixNano()-originalStart.UnixNano())
}
*/
} }
} }
} }
func printTime(pRemains *time.Duration, target time.Duration, delim *rune, reverse bool, repeat bool) { func printTime(pRemains *time.Duration, target time.Duration, delim *rune, reverse bool, repeat bool, eta bool) {
var final string
remains := *pRemains remains := *pRemains
sec := remains.Seconds() sec := remains.Seconds()
min := int(sec / 60.0) min := int(sec / 60.0)
@ -156,16 +155,22 @@ func printTime(pRemains *time.Duration, target time.Duration, delim *rune, rever
tdelim = ':' tdelim = ':'
go alertTime(pRemains, target, reverse, repeat) go alertTime(pRemains, target, reverse, repeat)
} }
at := fmt.Sprintf("%2d%c%02d%c%02d", hrs, byte(*delim), min, byte(*delim), seconds) at := fmt.Sprintf("%2d%c%02d%c%02d ", hrs, byte(*delim), min, byte(*delim), seconds)
rem := fmt.Sprintf("%2d%c%02d%c%02d", rHrs, tdelim, rMin, tdelim, rSec) rem := fmt.Sprintf("%2d%c%02d%c%02d ", rHrs, tdelim, rMin, tdelim, rSec)
fmt.Printf("\rRemaining: %s \tAt: %s ", rem, at) rem = fmt.Sprintf("Remaining: %s \tAt: %s ", rem, at)
if eta {
rem = fmt.Sprintf("%s \tETA: %s", rem, time.Unix(0, (time.Now().UnixNano()+target.Nanoseconds()-pRemains.Nanoseconds())).Format("3:04"))
}
final = rem
} else { } else {
rem := fmt.Sprintf("%2d%c%02d%c%02d", hrs, byte(*delim), min, byte(*delim), seconds) rem := fmt.Sprintf("%2d%c%02d%c%02d ", hrs, byte(*delim), min, byte(*delim), seconds)
fmt.Printf("\rRemaining: %s ", rem) final = fmt.Sprintf("Remaining: %s ", rem)
final = fmt.Sprintf("%s \tETA: %s", final, time.Unix(0, time.Now().UnixNano()+pRemains.Nanoseconds()).Format("3:04"))
if remains < 0 { if remains < 0 {
go alertTime(pRemains, target, reverse, repeat) go alertTime(pRemains, target, reverse, repeat)
} }
} }
fmt.Printf("\r%s", final)
} }
func alertTime(pRemains *time.Duration, target time.Duration, reverse, repeat bool) { func alertTime(pRemains *time.Duration, target time.Duration, reverse, repeat bool) {