8c7328f3a(remote quotas: a Remote set to Select none still tells its cards) was committed from a stale tree. Beside its own three files (docs/subsystems/providers-accounts.md, remote_quotas_scope.go, remote_quotas_test.go) it wrote 21 files back to their state before35aaaa4b9, which took out: -35aaaa4b9, the Codex client setting (codex_request.go, codex_tier.go, settings.go, settingsapi.go, api.go, app.js, i18n.js, index.html, codex-client.test.cjs and the rest); -2fa79aef3, a save of the bytes a file already holds changes nothing (#6289: logins.go, logins_unchanged_test.go); -797a23cad, the tray's words (traywords.go, traytip_words_test.go). The 21 files are checked out from 8c7328f3a^. After this commit `git diff 8c7328f3a^ --stat` names only the three remote-quota files, so main is the three lost commits plus 8c7328f3a's own change. Run (mica, at this tree): gofmt -l clean; go test -count=1 -tags nogui ./internal/provider ./internal/settings ./internal/settingsapi ok. ./internal/cli and . are red on main already, not from this: the codex-client and codex-open routes have no notYet entry, and catalog.go's mmddSnapshot is compiled at launch (1ca2c62c4). Both are fixed in the next two commits.
115 lines
3.4 KiB
Go
115 lines
3.4 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/lazyre"
|
|
)
|
|
|
|
// pageWords are the page's own translations (assets/i18n.js's I18N) in
|
|
// one language, read when first asked for it: the words a usage card's
|
|
// window is named and counted in ("Credits", "credits", "5 hours") come
|
|
// from the vendor or a plugin in English, and the Usage page says them
|
|
// with t(). The tray's tooltip says them with the same table (pageWord),
|
|
// so a window reads in the tray as on its card, in every language the
|
|
// page has (yi_time on Discord: "Credits 0 / 500 credits · 已用 0%").
|
|
// Only the language last asked for is kept: all four blocks held ~4.5 MB
|
|
// of an idle tray magpie's heap, for the few words one language needs.
|
|
func pageWords(lang string) map[string]string {
|
|
pageWordsMu.Lock()
|
|
defer pageWordsMu.Unlock()
|
|
if pageWordsLang == lang && pageWordsHeld != nil {
|
|
return pageWordsHeld
|
|
}
|
|
b, err := assets.ReadFile("assets/i18n.js")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
pageWordsLang, pageWordsHeld = lang, parsePageWordsIn(string(b), lang)[lang]
|
|
if pageWordsHeld == nil {
|
|
pageWordsHeld = map[string]string{}
|
|
}
|
|
return pageWordsHeld
|
|
}
|
|
|
|
var (
|
|
pageWordsMu sync.Mutex
|
|
pageWordsLang string
|
|
pageWordsHeld map[string]string
|
|
)
|
|
|
|
var (
|
|
pageLangHead = lazyre.New(`^ "?([\w-]+)"?: \{`)
|
|
pagePair = lazyre.New(`"((?:[^"\\]|\\.)*)"\s*:\s*"((?:[^"\\]|\\.)*)"`)
|
|
)
|
|
|
|
// parsePageWords reads I18N's language blocks from i18n.js's source: each
|
|
// ` zh: {` or ` "zh-TW": {` line starts one, and its entries are
|
|
// ` "English": "translation",` lines, one or more to a line. A string
|
|
// JSON can't read (a JS-only escape) is left out: that word reads in
|
|
// English, as the page's own fallback does.
|
|
func parsePageWords(src string) map[string]map[string]string {
|
|
return parsePageWordsIn(src, "")
|
|
}
|
|
|
|
// parsePageWordsIn is parsePageWords with only lang's block read ("" for
|
|
// every block).
|
|
func parsePageWordsIn(src, lang string) map[string]map[string]string {
|
|
if i := strings.Index(src, "\n};"); i >= 0 {
|
|
src = src[:i]
|
|
}
|
|
out := map[string]map[string]string{}
|
|
var cur map[string]string
|
|
for _, line := range strings.Split(src, "\n") {
|
|
if m := pageLangHead.FindStringSubmatch(line); m != nil {
|
|
cur = nil
|
|
if lang == "" || m[1] == lang {
|
|
cur = map[string]string{}
|
|
out[m[1]] = cur
|
|
}
|
|
continue
|
|
}
|
|
if cur == nil || !strings.HasPrefix(line, " \"") {
|
|
continue
|
|
}
|
|
for _, m := range pagePair.FindAllStringSubmatch(line, -1) {
|
|
var k, v string
|
|
if json.Unmarshal([]byte(`"`+m[1]+`"`), &k) != nil || json.Unmarshal([]byte(`"`+m[2]+`"`), &v) != nil {
|
|
continue
|
|
}
|
|
cur[k] = v
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// pageWord is s as the page says it in lang (its t()): s itself in
|
|
// English, or where the page has no word for it.
|
|
func pageWord(lang, s string) string {
|
|
if v, ok := pageWords(lang)[s]; ok && v != "" {
|
|
return v
|
|
}
|
|
return s
|
|
}
|
|
|
|
// trayWindowName is a window's name as its card says it in lang: the
|
|
// name whole when the page has it, else each " · " part the page has
|
|
// ("Gemini · 5 hours", a pool's window named with its pool by trayShared,
|
|
// a model's with its family by trayModels), as the card writes
|
|
// pool + " · " + t(name).
|
|
func trayWindowName(lang, name string) string {
|
|
if lang == "en" || name == "" {
|
|
return name
|
|
}
|
|
if w := pageWord(lang, name); w != name {
|
|
return w
|
|
}
|
|
parts := strings.Split(name, " · ")
|
|
for i, p := range parts {
|
|
parts[i] = pageWord(lang, p)
|
|
}
|
|
return strings.Join(parts, " · ")
|
|
}
|