package main import ( "encoding/json" "os" "path/filepath" "strings" ) type Catalog struct { Tiles []Tile } func (c *Catalog) LoadFile(filename string) error { pay, err := os.ReadFile(filename) if err != nil { return err } file := []map[string]any{} err = json.Unmarshal(pay, &file) if err != nil { return err } for _, m := range file { t := TileFromMap(m) if t == nil { continue } c.Tiles = append(c.Tiles, t) } return nil } func (c *Catalog) LoadTiles(dirname string) error { ls, err := os.ReadDir(dirname) if err != nil { return err } for _, ent := range ls { // Ignore: hidden, dirs, non json, template if strings.HasPrefix(ent.Name(), ".") || ent.IsDir() || !strings.HasSuffix(ent.Name(), ".json") || ent.Name() == "0_template.json" { continue } err = c.LoadFile(filepath.Join(dirname, ent.Name())) if err != nil { return err } } return nil } func (c *Catalog) FindByName(name string) (int, bool) { for idx, t := range c.Tiles { if t.Name() == name { return idx, true } } return 0, false } func (c *Catalog) Symbol(idx int) string { return c.Tiles[idx].Symbol() } func (c *Catalog) Color(idx int) string { return c.Tiles[idx].Color() } func (c *Catalog) Next(idx int) { c.Tiles[idx].Next() } func (c *Catalog) MakeTemplate(filename string) error { pay, err := json.MarshalIndent([]map[string]any{ { "name": "template", "symbol": "0", "color": "BRI BRO ON BLA", }, { "name": "animated-template", "symbols": []string{ "1", "2", "3", }, "colors": []string{ "BRI RED ON BLA", "BRI GRE ON BLA", "BRI CYA ON BLA", }, "update": []string{ "time 150ms", }, }, }, "", " ") if err != nil { return err } err = os.WriteFile(filename, pay, 0666) return err }