123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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
- }
|