v0.1.3516 made gateway's and redact's patterns lazy. The other 299 package-level regexp.MustCompile calls, in 148 files across agent, provider, gui, library, codexcat, sessions, usage, plugin and the rest, now go through lazyre.New too, so a launch that never reaches them (`magpie statusline`, run by Claude Code on every message) doesn't compile them. Fields, maps and params that held them are *lazyre.RE. Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build (t42) against this one (t43): - GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765 - `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M, peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30 changed packages ok; GOOS=linux go vet ok except ., internal/cli and internal/gui, which stop in wails v3's cgo files when cross-compiled (the same at main). New TestAllLazyPatternsCompile at the root compiles all 574 registered patterns (ok), so a bad pattern fails a test rather than a first use. Not run before push: the changed packages' own test suites; they run on mote after it.
89 lines
2.7 KiB
Go
89 lines
2.7 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), by
|
|
// language, read once: 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%").
|
|
var pageWords = sync.OnceValue(func() map[string]map[string]string {
|
|
b, err := assets.ReadFile("assets/i18n.js")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return parsePageWords(string(b))
|
|
})
|
|
|
|
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 {
|
|
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 = 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, " · ")
|
|
}
|