package web
import (
"html/template"
"strconv"
"time"
)
func (s *Server) templateFuncs() template.FuncMap {
return template.FuncMap{
"asset": s.asset,
"num": num,
"dateLong": dateLong,
"dateShort": dateShort,
"weekday": weekday,
"add": func(a, b int) int { return a + b },
"pluralDays": pluralDays,
"tileTitle": tileTitle,
}
}
// num renders an optional number for a form field: blank stays blank, so an
// unlogged day does not come back pre-filled with a zero.
func num(p *int) string {
if p == nil {
return ""
}
return strconv.Itoa(*p)
}
// dateLong renders "Thursday 31 July" for the day being logged.
func dateLong(t time.Time) string {
return t.Format("Monday 2 January")
}
// dateShort renders "31 Jul" for the tiles.
func dateShort(t time.Time) string {
return t.Format("2 Jan")
}
func weekday(t time.Time) string {
return t.Format("Mon")
}
func pluralDays(n int) string {
if n == 1 {
return "1 day"
}
return strconv.Itoa(n) + " days"
}
// tileTitle is the hover and screen-reader text for a square on the card.
func tileTitle(v dayView) string {
label := "Day " + strconv.Itoa(v.Number) + ", " + dateShort(v.Time) + " — "
switch {
case v.IsFuture:
return label + "still ahead"
case v.Marks.Complete():
return label + "all five"
case v.Score == 0:
return label + "nothing logged"
default:
return label + strconv.Itoa(v.Score) + " of 5"
}
}