diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/comic/comic_test.go | 36 | ||||
| -rw-r--r-- | internal/comic/types.go | 36 |
2 files changed, 56 insertions, 16 deletions
diff --git a/internal/comic/comic_test.go b/internal/comic/comic_test.go index 5502bc6..be4ae6c 100644 --- a/internal/comic/comic_test.go +++ b/internal/comic/comic_test.go @@ -67,16 +67,16 @@ func TestParseGenerateResultUsesConfiguredDimensions(t *testing.T) { storyPanelSeparator, "P1-A: first", "P1-B: second", - "P7-A: last", - "P7-B: end", - "P7-C: ignored", + "P10-A: last", + "P10-B: end", + "P10-C: ignored", } - got := parseGenerateResultWithDimensions(strings.Join(lines, "\n"), 7, 2) + got := parseGenerateResultWithDimensions(strings.Join(lines, "\n"), 10, 2) if got.StoryText != "story text" || got.Bible != "bible text" || got.Title != "My Comic" { t.Fatalf("parseGenerateResultWithDimensions() = %#v", got) } - if got.PanelScript == nil || len(got.PanelScript) != 7 { - t.Fatalf("panel script pages = %d, want 7", len(got.PanelScript)) + if got.PanelScript == nil || len(got.PanelScript) != 10 { + t.Fatalf("panel script pages = %d, want 10", len(got.PanelScript)) } for i, page := range got.PanelScript { if len(page) != 2 { @@ -86,8 +86,28 @@ func TestParseGenerateResultUsesConfiguredDimensions(t *testing.T) { if got.PanelScript[0][0] != "first" || got.PanelScript[0][1] != "second" { t.Fatalf("first page panel script = %#v", got.PanelScript[0]) } - if got.PanelScript[6][0] != "last" || got.PanelScript[6][1] != "end" { - t.Fatalf("last page panel script = %#v", got.PanelScript[6]) + if got.PanelScript[9][0] != "last" || got.PanelScript[9][1] != "end" { + t.Fatalf("last page panel script = %#v", got.PanelScript[9]) + } +} + +func TestPanelLabelSupportsMultiLetterSequence(t *testing.T) { + t.Parallel() + + if got, want := panelLabel(0), "A"; got != want { + t.Fatalf("panelLabel(0) = %q, want %q", got, want) + } + if got, want := panelLabel(25), "Z"; got != want { + t.Fatalf("panelLabel(25) = %q, want %q", got, want) + } + if got, want := panelLabel(26), "AA"; got != want { + t.Fatalf("panelLabel(26) = %q, want %q", got, want) + } + if got, want := panelLabel(27), "AB"; got != want { + t.Fatalf("panelLabel(27) = %q, want %q", got, want) + } + if got := panelLabelsText(28); !strings.Contains(got, "AA") || !strings.Contains(got, "AB") { + t.Fatalf("panelLabelsText(28) = %q, want multi-letter labels", got) } } diff --git a/internal/comic/types.go b/internal/comic/types.go index f1b609c..e5072fb 100644 --- a/internal/comic/types.go +++ b/internal/comic/types.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/rand/v2" + "strconv" "strings" "time" @@ -209,21 +210,33 @@ func parsePanelScript(text string, storyPages, panelsPerPage int) [][]string { script[i] = make([]string, panelsPerPage) } - panelIndex := make(map[byte]int, panelsPerPage) + panelIndexByLabel := make(map[string]int, panelsPerPage) for i := 0; i < panelsPerPage; i++ { - panelIndex[byte('A'+i)] = i + panelIndexByLabel[panelLabel(i)] = i } for _, line := range strings.Split(text, "\n") { line = strings.TrimSpace(line) - if len(line) < 7 || line[0] != 'P' || line[2] != '-' || line[4] != ':' { + if !strings.HasPrefix(line, "P") { continue } - page := int(line[1] - '1') - panel, ok := panelIndex[line[3]] - if !ok || page < 0 || page >= storyPages { + pagePart, rest, ok := strings.Cut(line[1:], "-") + if !ok { continue } - script[page][panel] = strings.TrimSpace(line[5:]) + panelPart, textPart, ok := strings.Cut(rest, ":") + if !ok { + continue + } + page, err := strconv.Atoi(strings.TrimSpace(pagePart)) + if err != nil { + continue + } + panelPart = strings.ToUpper(strings.TrimSpace(panelPart)) + panel, ok := panelIndexByLabel[panelPart] + if !ok || page <= 0 || page > storyPages { + continue + } + script[page-1][panel] = strings.TrimSpace(textPart) } return script } @@ -269,7 +282,14 @@ func panelLabel(idx int) string { if idx < 0 { return "" } - return string(rune('A' + idx)) + idx++ + var out []byte + for idx > 0 { + idx-- + out = append([]byte{byte('A' + idx%26)}, out...) + idx /= 26 + } + return string(out) } func panelLabelsText(count int) string { |
