From 0fe67cfd5def4ac3e919fd611fe5acc55ca2d253 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Mon, 29 Jun 2026 04:21:37 -0400 Subject: better dialog color, edge cases on blocked map links fixed --- internal/color/color.go | 19 ++++++++++ internal/color/color_test.go | 90 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) (limited to 'internal/color') diff --git a/internal/color/color.go b/internal/color/color.go index 5a1aa54..92535d1 100644 --- a/internal/color/color.go +++ b/internal/color/color.go @@ -521,3 +521,22 @@ func ExpandTagsDefault(mode string, def ColorSpec, text string) string { return sb.String() } + +func ExpandDialogTags(mode string, dialogSpec ColorSpec, text string) string { + parts := strings.Split(text, `"`) + var sb strings.Builder + for i, part := range parts { + if i%2 == 0 { + if part != "" { + sb.WriteString(ExpandTagsDefault(mode, NoColor(), part)) + } + } else { + if i < len(parts)-1 { + sb.WriteString(ExpandTagsDefault(mode, dialogSpec, `"`+part+`"`)) + } else { + sb.WriteString(ExpandTagsDefault(mode, dialogSpec, `"`+part)) + } + } + } + return sb.String() +} diff --git a/internal/color/color_test.go b/internal/color/color_test.go index bffc0d7..f6b79f0 100644 --- a/internal/color/color_test.go +++ b/internal/color/color_test.go @@ -117,3 +117,93 @@ func TestAverage(t *testing.T) { t.Errorf("Average(none, none).Fg = %d, want -1", got.Fg) } } + +func TestExpandDialogTags_NoQuotes(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, "The Netrunner looks up.") + if strings.Contains(got, "\033") { + t.Errorf("text without quotes should not contain ANSI codes, got %q", got) + } +} + +func TestExpandDialogTags_AllQuoted(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, `"Hello there."`) + expectFg := FgCode("xterm256", 208) + if !strings.Contains(got, expectFg+`"Hello there."`+Reset) && !strings.Contains(got, `"`+expectFg+`Hello there."`+Reset) { + t.Errorf("fully quoted text should be dialog-colored, got %q", got) + } + if !strings.Contains(got, `"`) { + t.Error("quote characters should be present") + } +} + +func TestExpandDialogTags_Mixed(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, `He says, "Hello, world!"`) + expectFg := FgCode("xterm256", 208) + if !strings.Contains(got, `He says, `) { + t.Errorf("narrative text missing, got %q", got) + } + if !strings.Contains(got, expectFg+`"Hello, world!"`+Reset) { + t.Errorf("quoted text should be dialog-colored, got %q", got) + } +} + +func TestExpandDialogTags_InlineTagsInQuotes(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, `"Please {0B bold}look{/} carefully."`) + inlineFg := FgCode("xterm256", 11) + if !strings.Contains(got, inlineFg+`look`+Reset) { + t.Errorf("inline tag inside quotes should override dialog color, got %q", got) + } +} + +func TestExpandDialogTags_InlineTagsOutsideQuotes(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, `{0B bold}Notice:{/} "Something moved."`) + inlineFg := FgCode("xterm256", 11) + if !strings.Contains(got, inlineFg+`Notice:`+Reset) { + t.Errorf("inline tag outside quotes should work, got %q", got) + } +} + +func TestExpandDialogTags_MultipleQuotes(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, `"Hello," she said. "How are you?"`) + count := strings.Count(got, FgCode("xterm256", 208)) + if count < 2 { + t.Errorf("expected at least 2 dialog-colored segments, got %d in %q", count, got) + } +} + +func TestExpandDialogTags_UnmatchedQuote(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, `"Something started but never finished`) + expectFg := FgCode("xterm256", 208) + if !strings.Contains(got, expectFg) { + t.Errorf("unmatched quote should still be dialog-colored, got %q", got) + } + if !strings.Contains(got, `"Something started but never finished`) { + t.Errorf("unmatched quote text should be present, got %q", got) + } +} + +func TestExpandDialogTags_Empty(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("xterm256", dialogSpec, "") + if got != "" { + t.Errorf("empty input should yield empty output, got %q", got) + } +} + +func TestExpandDialogTags_NoColorMode(t *testing.T) { + dialogSpec := Parse("D0") + got := ExpandDialogTags("none", dialogSpec, `"Hello."`) + if strings.Contains(got, "\033") { + t.Errorf("no-color mode should suppress ANSI codes, got %q", got) + } + if got != `"Hello."` { + t.Errorf("no-color mode should preserve text, got %q", got) + } +} -- cgit v1.2.3