fix text encoding

This commit is contained in:
cjdenio committed 2026-08-31 22:17:39 -06:00
1 parent 3312caf3df
commit 47003d1fe7
4 files changed
+46 -27

No files matched your search

+2
View File
@@ -1,3 +1,5 @@
module github.com/cjdenio/crossword
go 1.25.0
require golang.org/x/text v0.41.0
+2
View File
@@ -0,0 +1,2 @@
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
+18 -19
View File
@@ -6,6 +6,7 @@ import (
"os"
"slices"
"strings"
"time"
"github.com/cjdenio/crossword/puz"
)
@@ -100,24 +101,22 @@ func main() {
fmt.Printf("TITLE: %s\n", puzzle.Title)
fmt.Printf("AUTHOR: %s\n", puzzle.Author)
fmt.Println(RenderPuzzle(puzzle.State, puzzle.Width, puzzle.Height, puzzle.Clues[0].Cells, 1))
// j, _ := json.Marshal(puzzle)
// fmt.Println(string(j))
// for _, clue := range puzzle.Clues {
// if clue.Direction == puz.DirectionAcross {
// fmt.Printf(". %d-across: %s: %s\n", clue.Number, clue.Clue, clue.Solution)
// } else {
// fmt.Printf(". %d-down: %s: %s\n", clue.Number, clue.Clue, clue.Solution)
// }
// }
fmt.Println(RenderPuzzle(puzzle.Solution, puzzle.Width, puzzle.Height, puzzle.Clues[0].Cells, 1))
fmt.Printf("%s\n", puzzle.Copyright)
// for {
// n, _ := os.Stdin.Read(make([]byte, 1))
// if n > 0 {
// return
// }
// }
selectedCell := 1
for {
time.Sleep(500 * time.Millisecond)
fmt.Printf("\x1b[%dA", puzzle.Height+6)
fmt.Print("\x1b[J")
fmt.Printf("TITLE: %s\n", puzzle.Title)
fmt.Printf("AUTHOR: %s\n", puzzle.Author)
fmt.Println(RenderPuzzle(puzzle.Solution, puzzle.Width, puzzle.Height, puzzle.Clues[selectedCell].Cells, 1))
fmt.Printf("%s\n", puzzle.Copyright)
selectedCell++
}
}
+24 -8
View File
@@ -7,6 +7,8 @@ import (
"errors"
"math"
"strings"
"golang.org/x/text/encoding/charmap"
)
type Direction int
@@ -65,6 +67,20 @@ func startOfAcrossClue(puzzle string, width, index int) bool {
return false
}
func readText(b *bufio.Reader) (string, error) {
text, err := b.ReadString(0x00)
if err != nil {
return "", err
}
decoded, err := charmap.ISO8859_1.NewDecoder().String(text[:len(text)-1])
if err != nil {
return "", err
}
return decoded, nil
}
func ParsePuz(file []byte) (*Puzzle, error) {
// read magic bytes
magic := string(file[2:14])
@@ -92,32 +108,32 @@ func ParsePuz(file []byte) (*Puzzle, error) {
// read text
buf := bufio.NewReader(bytes.NewReader(file[stateEnd:]))
title, err := buf.ReadString(0x00)
title, err := readText(buf)
if err != nil {
return nil, err
}
puzzle.Title = title[:len(title)-1]
puzzle.Title = title
author, err := buf.ReadString(0x00)
author, err := readText(buf)
if err != nil {
return nil, err
}
puzzle.Author = author[:len(author)-1]
puzzle.Author = author
copyright, err := buf.ReadString(0x00)
copyright, err := readText(buf)
if err != nil {
return nil, err
}
puzzle.Copyright = copyright[:len(copyright)-1]
puzzle.Copyright = copyright
// read clues
clues := make([]string, 0, puzzle.ClueCount)
for range puzzle.ClueCount {
clue, err := buf.ReadString(0x00)
clue, err := readText(buf)
if err != nil {
return nil, err
}
clues = append(clues, clue[:len(clue)-1])
clues = append(clues, clue)
}
// assign clue numbers