1package main
2
3import (
4 "fmt"
5 "strings"
6 "time"
7)
8
9func formatDuration(d time.Duration) string {
10 d = d.Round(time.Second)
11 h := d / time.Hour
12 d -= h * time.Hour
13 m := d / time.Minute
14 d -= m * time.Minute
15 s := d / time.Second
16
17 parts := []string{}
18
19 if h > 0 {
20 parts = append(parts, fmt.Sprintf("%d hour", h))
21 }
22 if m > 0 || h > 0 {
23 parts = append(parts, fmt.Sprintf("%d minute", m))
24 }
25 parts = append(parts, fmt.Sprintf("%d second", s))
26
27 return strings.Join(parts, " ")
28}
29
30func main() {
31 start := time.Now()
32 ticker := time.NewTicker(time.Second)
33 defer ticker.Stop()
34
35 for range ticker.C {
36 elapsed := time.Since(start)
37
38 // Clear the current line
39 fmt.Print("\r\033[K")
40 // Print the new status
41 fmt.Print(formatDuration(elapsed))
42 }
43}
44