Files
magpie/internal/provider/window_pace.go
T
yetone 3a7d0314a9 usage: a window's Pace as a number on its card, and in the menu bar when asked (fflake33)
fflake33 on Discord asked for the pace as a figure, "+12%" or "−8%"
against an even pace through the window, on the Usage cards and in the
menu bar.

- provider: QuotaWindow.Pace(readAt, now) is the share used (0-100) less
  the share of the window gone by, from Bounds (reset less span; a
  month's window a calendar month); none for an unlimited window, one
  whose span or reset isn't known, or outside it. WithWindowStarts sets
  startsAt on copies of the cards for the GUI (the caches' cards are
  left alone); /api/usage/quotas and its refresh send it.
- Usage page: "Pace +20%" on the reset line after the wait (quotaPace,
  paceLine), its sign turned with the meter in Left mode, amber while
  more is used than an even pace would have, what it means in its
  tooltip. Past cycles and windows with no known start show none.
- Menu bar: Settings > Menu bar pace (settings.TrayPace, off by default;
  hidden on Windows, whose tray shows no text) puts " +12%" beside each
  figure ("42% −14%", at most 10 runes, trayRowMax) and "pace −14%" in
  the tooltip. KeepOwn keeps it device-local like the other tray
  settings; the Settings save keeps it (prefsKeep).
- Strings in en, zh, zh-TW, ja, de. gui-shell.md says it.

Run:
- go test -tags nogui ./internal/provider ./internal/gui ./internal/settings
  ./internal/usage: ok; ./internal/davsync: ok (now covers TrayPace;
  red with TrayPace taken out of KeepOwn). go test ./internal/gui -run
  Tray (darwin, with the tray image tests): ok.
- New TestWindowPace, TestWithWindowStarts (input not mutated),
  TestTrayUsagePace (off unchanged, used and left, rows, tooltip).
- quota-pace.test.cjs, chromium and webkit: 4 pass; cards in en, zh,
  zh-TW, ja, de at 1100 and 420px, used and left, amber, tooltips, no
  sideways scroll, a click moves nothing; the Settings row in all five
  at both widths, On posts trayPace, a currency save keeps it, no
  scroll; hidden with no card and on Windows, shown on Linux. Red with
  paceLine not called, with trayPace out of prefsKeep, and with the
  Left-mode sign not turned.
- gui-de, gui-ja, gui-zh-tw, i18n-keys: 12 pass; tray-bird 6, quota-holds
  2, quota-read-at 4 pass.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows clean.
Not tried: the real menu bar on a Mac or Linux tray.
2026-10-14 13:02:48 +08:00

50 lines
1.5 KiB
Go

package provider
import "time"
// Pace is how far w's use, read at readAt, is from an even pace through
// the window at now, in points of the window (fflake33 on Discord): what
// is used less the share of the window gone by. +12 is twelve points more
// used than an even pace would have used by now, so at this rate the
// window runs out before it resets; -8 is eight points to spare. ok is
// false for a window with no limit, or whose start or reset isn't known,
// or when now is outside it.
func (w QuotaWindow) Pace(readAt, now time.Time) (pace float64, ok bool) {
if w.Unlimited {
return 0, false
}
start, end, ok := w.Bounds(readAt)
if !ok || !end.After(start) || now.Before(start) || !now.Before(end) {
return 0, false
}
even := 100 * float64(now.Sub(start)) / float64(end.Sub(start))
return max(0, min(100, w.Used)) - even, true
}
// WithWindowStarts is qs with each window's StartsAt set where its bounds
// are known, for the GUI's Pace number; qs and its windows, which the
// caches share, are left as they were.
func WithWindowStarts(qs []SubscriptionQuota, now time.Time) []SubscriptionQuota {
out := make([]SubscriptionQuota, len(qs))
for i, q := range qs {
readAt := now
if q.ReadAt != nil && !q.ReadAt.After(now) {
readAt = *q.ReadAt
}
if len(q.Windows) > 0 {
ws := make([]QuotaWindow, len(q.Windows))
for j, w := range q.Windows {
if w.StartsAt == nil && !w.Unlimited {
if start, _, ok := w.Bounds(readAt); ok {
w.StartsAt = &start
}
}
ws[j] = w
}
q.Windows = ws
}
out[i] = q
}
return out
}