1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package main
- import (
- "fmt"
- "os"
- "strings"
- )
- type Tile struct {
- Symbol string
- Color string
- }
- func ParseTile(line string) *Tile {
- line = strings.TrimSpace(line)
- if len(line) < 2 {
- return nil
- }
- return &Tile{
- Symbol: string(line[0]),
- Color: string(line[1:]),
- }
- }
- type TileIndex struct {
- Tiles []*Tile
- }
- func ParseTileIndex(filename string) (*TileIndex, error) {
- payload, err := os.ReadFile(filename)
- if err != nil {
- return nil, err
- }
- var (
- line string = ""
- index *TileIndex = &TileIndex{
- Tiles: []*Tile{
- nil,
- },
- }
- )
- for linenum, b := range payload {
- if b == '\n' {
- t := ParseTile(line)
- if t != nil {
- index.Tiles = append(index.Tiles, t)
- } else {
- fmt.Printf("%d]: Failed parsing tile, '%s'\r\n", linenum, line)
- }
- line = ""
- } else {
- line += string(b)
- }
- }
- return index, nil
- }
|