This commit is contained in:
Tijl 2024-09-27 17:41:52 +02:00
parent 0013cb6d81
commit 0ceb6e092c
Signed by: tijl
GPG Key ID: DAE24BFCD722F053
2 changed files with 33 additions and 1 deletions

34
main.go
View File

@ -27,6 +27,12 @@ func main() {
namedStopwatch(args[0]) namedStopwatch(args[0])
} else if len(args) == 2 && (args[0] == "clear" || args[0] == "-c" || args[0] == "--clear") { } else if len(args) == 2 && (args[0] == "clear" || args[0] == "-c" || args[0] == "--clear") {
clearStopwatch(args[1]) 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() { func printHelp() {
fmt.Println("Usage:") fmt.Println("Usage:")
fmt.Println(" stopwatch - Start a quick stopwatch") fmt.Println(" stopwatch - Start a quick stopwatch")
fmt.Println(" stopwatch [name] - Start or view a named 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")
} }

BIN
stopwatch Executable file

Binary file not shown.