Files
cyclingbot/bot/format.go
Blake Ridgway ba1770b493 first commit
2026-04-11 14:06:59 -05:00

82 lines
1.8 KiB
Go

package bot
import (
"fmt"
"strings"
)
const miToKM = 1.60934
// formatDist formats a KM value in the user's preferred unit.
func formatDist(km float64, unit string) string {
if unit == "miles" {
return fmt.Sprintf("%.1f mi", km/miToKM)
}
return fmt.Sprintf("%.1f km", km)
}
// distanceComparison returns a fun contextual comparison string.
func distanceComparison(km float64) string {
switch {
case km >= 384400:
return "You've cycled **to the Moon!** 🌕"
case km >= 40075:
return fmt.Sprintf("That's **%.1f laps around the Earth!** 🌏", km/40075)
case km >= 1892:
return fmt.Sprintf("That's like riding **London → Rome** (%.0f%%)! 🗺️", km/1892*100)
case km >= 500:
return fmt.Sprintf("That's like riding **Amsterdam → Paris** and back (%.0f%%)! 🇫🇷", km/830*100)
default:
return "Keep pedalling — the KMs are adding up! 💪"
}
}
// progressBar renders a simple ASCII progress bar.
func progressBar(current, max float64, width int) string {
if max <= 0 {
return ""
}
pct := current / max
if pct > 1 {
pct = 1
}
filled := int(pct * float64(width))
bar := ""
for i := 0; i < width; i++ {
if i < filled {
bar += "█"
} else {
bar += "░"
}
}
return fmt.Sprintf("[%s] %.0f%%", bar, pct*100)
}
// fmtKM formats a KM value with comma separators (e.g. 5243.3 → "5,243.3").
// decimals controls how many decimal places to show.
func fmtKM(km float64, decimals int) string {
s := fmt.Sprintf(fmt.Sprintf("%%.%df", decimals), km)
parts := strings.SplitN(s, ".", 2)
intPart := parts[0]
n := len(intPart)
var out []byte
for i := 0; i < n; i++ {
if i > 0 && (n-i)%3 == 0 {
out = append(out, ',')
}
out = append(out, intPart[i])
}
if len(parts) > 1 {
return string(out) + "." + parts[1]
}
return string(out)
}
func plural(n int) string {
if n == 1 {
return ""
}
return "s"
}