diff --git a/main.go b/main.go index f3e659b..7fe9280 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,12 @@ func main() { namedStopwatch(args[0]) } else if len(args) == 2 && (args[0] == "clear" || args[0] == "-c" || args[0] == "--clear") { clearStopwatch(args[1]) + } else if len(args) == 2 && (args[0] == "timer" || args[0] == "-t" || args[0] == "--timer") { + d, err := time.ParseDuration(args[1]) + if err != nil { + fmt.Println("Error parsing duration:", err) + } + timerUI(time.Now().Add(d)) } } @@ -125,9 +131,35 @@ func stopwatchUI(startTime time.Time) { } } +func timerUI(startTime time.Time) { + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) + + go func() { + <-stop + os.Exit(0) + }() + + for { + elapsed := time.Until(startTime) + if elapsed.Milliseconds() == 0 { + break + } + fmt.Printf("\r%02dd %02dh %02dm %02ds %03dms", + elapsed/(24*time.Hour), + (elapsed%(24*time.Hour))/time.Hour, + (elapsed%time.Hour)/time.Minute, + (elapsed%time.Minute)/time.Second, + (elapsed%time.Second)/time.Millisecond) + time.Sleep(1 * time.Millisecond) + } +} + func printHelp() { fmt.Println("Usage:") fmt.Println(" stopwatch - Start a quick stopwatch") fmt.Println(" stopwatch [name] - Start or view a named stopwatch") - fmt.Println(" stopwatch clear [name] - Remove a named stopwatch") + fmt.Println(" stopwatch -t [time] - Start a quick timer") + fmt.Println(" stopwatch -c [name] - Remove a named stopwatch") + fmt.Println(" stopwatch -h - View help menu") } diff --git a/stopwatch b/stopwatch new file mode 100755 index 0000000..975f755 Binary files /dev/null and b/stopwatch differ