| 1 | // Package pause words paused work (spec 2026-11-24-paused-work-design.md): |
| 2 | // when it resumes, in the person's zone and relative to now, and a lead |
| 3 | // line split where its bold part ends. It holds no copy: every word comes |
| 4 | // from the instance's copy (internal/equipo/copy.go) through Copy. |
| 5 | package pause |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Copy is the instance's copy, by key. |
| 14 | type Copy func(key string) string |
| 15 | |
| 16 | // Evening is the hour from which a time later today is "tonight". |
| 17 | const Evening = 18 |
| 18 | |
| 19 | // When is a resume time as a lead says it, in loc: "today at 15:00", |
| 20 | // "21:00 tonight", "midnight" (the coming one), "tomorrow at 09:00", else |
| 21 | // the day and the time, "Thu 03:00". |
| 22 | func When(c Copy, now, at time.Time, loc *time.Location) string { |
| 23 | n, a := now.In(loc), at.In(loc) |
| 24 | clock := a.Format("15:04") |
| 25 | switch days := dayDiff(n, a); { |
| 26 | case days == 1 && a.Hour() == 0 && a.Minute() == 0: |
| 27 | return c("time.midnight") |
| 28 | case days == 0 && a.Hour() >= Evening: |
| 29 | return fmt.Sprintf(c("time.tonight"), clock) |
| 30 | case days == 0: |
| 31 | return fmt.Sprintf(c("time.today_at"), clock) |
| 32 | case days == 1: |
| 33 | return fmt.Sprintf(c("time.tomorrow_at"), clock) |
| 34 | } |
| 35 | return a.Format("Mon 15:04") |
| 36 | } |
| 37 | |
| 38 | // Short is a resume time as Base's pill says it, in loc: the time today |
| 39 | // or tomorrow, "midnight" for the coming one, else the day's name. |
| 40 | func Short(c Copy, now, at time.Time, loc *time.Location) string { |
| 41 | n, a := now.In(loc), at.In(loc) |
| 42 | switch days := dayDiff(n, a); { |
| 43 | case days == 1 && a.Hour() == 0 && a.Minute() == 0: |
| 44 | return c("time.midnight") |
| 45 | case days <= 1: |
| 46 | return a.Format("15:04") |
| 47 | } |
| 48 | return a.Format("Monday") |
| 49 | } |
| 50 | |
| 51 | // At is a resume time as Base's sentence says it, in loc: the clock |
| 52 | // today or tomorrow, "midnight" for the coming one, else the day and the |
| 53 | // clock. |
| 54 | func At(c Copy, now, at time.Time, loc *time.Location) string { |
| 55 | n, a := now.In(loc), at.In(loc) |
| 56 | switch days := dayDiff(n, a); { |
| 57 | case days == 1 && a.Hour() == 0 && a.Minute() == 0: |
| 58 | return c("time.midnight") |
| 59 | case days <= 1: |
| 60 | return a.Format("15:04") |
| 61 | } |
| 62 | return a.Format("Mon 15:04") |
| 63 | } |
| 64 | |
| 65 | // dayDiff is how many calendar days b is after a, both in one zone. |
| 66 | func dayDiff(a, b time.Time) int { |
| 67 | da := time.Date(a.Year(), a.Month(), a.Day(), 12, 0, 0, 0, time.UTC) |
| 68 | db := time.Date(b.Year(), b.Month(), b.Day(), 12, 0, 0, 0, time.UTC) |
| 69 | return int(db.Sub(da).Round(24*time.Hour) / (24 * time.Hour)) |
| 70 | } |
| 71 | |
| 72 | // In is how long until then, roughly: minutes under an hour (at least |
| 73 | // one), hours under a day, then days. |
| 74 | func In(c Copy, d time.Duration) string { |
| 75 | minutes := int((d + time.Minute - 1) / time.Minute) |
| 76 | switch { |
| 77 | case minutes <= 1: |
| 78 | return c("time.in_minutes.one") |
| 79 | case minutes < 60: |
| 80 | return fmt.Sprintf(c("time.in_minutes"), minutes) |
| 81 | case d < 24*time.Hour: |
| 82 | if h := int(d.Round(time.Hour) / time.Hour); h > 1 { |
| 83 | return fmt.Sprintf(c("time.in_hours"), h) |
| 84 | } |
| 85 | return c("time.in_hours.one") |
| 86 | } |
| 87 | if days := int(d.Round(24*time.Hour) / (24 * time.Hour)); days > 1 { |
| 88 | return fmt.Sprintf(c("time.in_days"), days) |
| 89 | } |
| 90 | return c("time.in_days.one") |
| 91 | } |
| 92 | |
| 93 | // WhenIn is a time with how long until it: "Thu 03:00 (in 2 days)". |
| 94 | func WhenIn(c Copy, now, at time.Time, loc *time.Location) string { |
| 95 | return fmt.Sprintf(c("time.when_in"), When(c, now, at, loc), In(c, at.Sub(now))) |
| 96 | } |
| 97 | |
| 98 | // Lead words the copy at key with args, split where its bold part ends: |
| 99 | // at its first colon, which stays bold, or its first " (", which does |
| 100 | // not. A template with neither is all rest. |
| 101 | func Lead(c Copy, key string, args ...any) (strong, rest string) { |
| 102 | tmpl := c(key) |
| 103 | cut, keep := len(tmpl), 0 |
| 104 | if i := strings.Index(tmpl, ":"); i >= 0 { |
| 105 | cut, keep = i, 1 |
| 106 | } |
| 107 | if i := strings.Index(tmpl, " ("); i >= 0 && i < cut { |
| 108 | cut, keep = i, 0 |
| 109 | } |
| 110 | if cut == len(tmpl) { |
| 111 | return "", fmt.Sprintf(tmpl, args...) |
| 112 | } |
| 113 | head, tail := tmpl[:cut+keep], tmpl[cut+keep:] |
| 114 | n := verbs(head) |
| 115 | if n > len(args) { |
| 116 | return "", fmt.Sprintf(tmpl, args...) |
| 117 | } |
| 118 | return fmt.Sprintf(head, args[:n]...), fmt.Sprintf(tail, args[n:]...) |
| 119 | } |
| 120 | |
| 121 | // verbs counts the formatting verbs in s (%% is not one). |
| 122 | func verbs(s string) int { |
| 123 | n := 0 |
| 124 | for i := 0; i < len(s)-1; i++ { |
| 125 | if s[i] == '%' { |
| 126 | if s[i+1] != '%' { |
| 127 | n++ |
| 128 | } |
| 129 | i++ |
| 130 | } |
| 131 | } |
| 132 | return n |
| 133 | } |
| 134 | |