diff options
| author | historia <[not public]> | 2026-06-29 04:21:37 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-29 04:21:37 -0400 |
| commit | 0fe67cfd5def4ac3e919fd611fe5acc55ca2d253 (patch) | |
| tree | fbcdcdeb355bd6c9d3afab006f052a8b465f7feb /internal/color | |
| parent | 72dbe734015916f98020d6a617981956f0509e26 (diff) | |
| download | thehouseoficarus-0fe67cfd5def4ac3e919fd611fe5acc55ca2d253.tar.gz | |
better dialog color, edge cases on blocked map links fixed
Diffstat (limited to 'internal/color')
| -rw-r--r-- | internal/color/color.go | 19 | ||||
| -rw-r--r-- | internal/color/color_test.go | 90 |
2 files changed, 109 insertions, 0 deletions
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) + } +} |
