github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/hud/format.go (about) 1 package hud 2 3 import ( 4 "fmt" 5 "math" 6 "time" 7 ) 8 9 // Duplicated in web/src/format.ts 10 func formatBuildDuration(d time.Duration) string { 11 hours := int(d.Hours()) 12 if hours > 0 { 13 return fmt.Sprintf("%dh", hours) 14 } 15 16 minutes := int(d.Minutes()) 17 if minutes > 0 { 18 return fmt.Sprintf("%dm", minutes) 19 } 20 21 seconds := d.Seconds() 22 if seconds >= 9.95 { 23 return fmt.Sprintf("%ds", int(math.Round(seconds))) 24 } 25 26 fractionalSeconds := float64(d) / float64(time.Second) 27 return fmt.Sprintf("%0.1fs", fractionalSeconds) 28 } 29 30 func formatDeployAge(d time.Duration) string { 31 switch { 32 case d.Seconds() < 5: 33 return "<5s" 34 case d.Seconds() < 15: 35 return "<15s" 36 case d.Seconds() < 30: 37 return "<30s" 38 case d.Seconds() < 45: 39 return "<45s" 40 case d.Minutes() < 1: 41 return "<1m" 42 case d.Hours() < 1: 43 return fmt.Sprintf("%dm", int(d.Minutes())) 44 default: 45 return fmt.Sprintf("%dh", int(d.Hours())) 46 } 47 }