Files
8bitMyGoUI/datetime/relativetime.go
T
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:31:08 -04:00

57 lines
1.3 KiB
Go

package datetime
import (
"fmt"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type RelativeTimeOptions struct {
// Slot inks the text; zero means Faint.
Slot pixel.Slot
}
// RelativeTime says how long since or until t, each minute.
func RelativeTime(c *ui.Context, t time.Time, o RelativeTimeOptions) ui.Element {
k := pixel.K(c)
ink := o.Slot
if ink == 0 {
ink = pixel.Faint
}
// Minutes since t; ahead of it, rounded up and negative.
d, m := k.Now().Sub(t), 0
if d >= 0 {
m = int(d / time.Minute)
} else {
m = -int((-d + time.Minute - 1) / time.Minute)
}
k.At(c, t.Add(time.Duration(m+1)*time.Minute))
return pixel.Text(c, words(k, m), pixel.TextOptions{Slot: ink})
}
// words says m minutes: ago when positive, ahead when negative.
func words(k pixel.Kit, m int) string {
n := max(m, -m)
h, days := n/60, n/(24*60)
pick := func(past, future string) string {
if m > 0 {
return past
}
return future
}
switch {
case n == 0:
return k.Msg("just_now")
case h == 0:
return fmt.Sprintf(k.Msg(pick("min_ago", "in_min")), n)
case days == 0:
return fmt.Sprintf(k.Msg(pick("hours_ago", "in_hours")), h)
case days == 1:
return k.Msg(pick("yesterday", "tomorrow"))
}
return fmt.Sprintf(k.Msg(pick("days_ago", "in_days")), days)
}