tile.go 857 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. )
  7. type Tile struct {
  8. Symbol string
  9. Color string
  10. }
  11. func ParseTile(line string) *Tile {
  12. line = strings.TrimSpace(line)
  13. if len(line) < 2 {
  14. return nil
  15. }
  16. return &Tile{
  17. Symbol: string(line[0]),
  18. Color: string(line[1:]),
  19. }
  20. }
  21. type TileIndex struct {
  22. Tiles []*Tile
  23. }
  24. func ParseTileIndex(filename string) (*TileIndex, error) {
  25. payload, err := os.ReadFile(filename)
  26. if err != nil {
  27. return nil, err
  28. }
  29. var (
  30. line string = ""
  31. index *TileIndex = &TileIndex{
  32. Tiles: []*Tile{
  33. nil,
  34. },
  35. }
  36. )
  37. for linenum, b := range payload {
  38. if b == '\n' {
  39. t := ParseTile(line)
  40. if t != nil {
  41. index.Tiles = append(index.Tiles, t)
  42. } else {
  43. fmt.Printf("%d]: Failed parsing tile, '%s'\r\n", linenum, line)
  44. }
  45. line = ""
  46. } else {
  47. line += string(b)
  48. }
  49. }
  50. return index, nil
  51. }