From aa537c89ca345d2c9a447104cbdb94e735a7a37a Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 06:30:14 +0000 Subject: [PATCH] fix(cli): make styled helpers nil-safe Co-Authored-By: Virgil --- pkg/cli/strings.go | 6 ++++++ pkg/cli/styles_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/cli/strings.go b/pkg/cli/strings.go index 4c82712..8252c46 100644 --- a/pkg/cli/strings.go +++ b/pkg/cli/strings.go @@ -14,11 +14,17 @@ func Sprint(args ...any) string { // Styled returns text with a style applied. func Styled(style *AnsiStyle, text string) string { + if style == nil { + return compileGlyphs(text) + } return style.Render(compileGlyphs(text)) } // Styledf returns formatted text with a style applied. func Styledf(style *AnsiStyle, format string, args ...any) string { + if style == nil { + return compileGlyphs(fmt.Sprintf(format, args...)) + } return style.Render(compileGlyphs(fmt.Sprintf(format, args...))) } diff --git a/pkg/cli/styles_test.go b/pkg/cli/styles_test.go index 949c138..b127a59 100644 --- a/pkg/cli/styles_test.go +++ b/pkg/cli/styles_test.go @@ -222,3 +222,17 @@ func TestPad_Good(t *testing.T) { assert.Equal(t, "hello", Pad("hello", 3)) assert.Equal(t, "東京 ", Pad("東京", 6)) } + +func TestStyled_Good_NilStyle(t *testing.T) { + restoreThemeAndColors(t) + UseASCII() + + assert.Equal(t, "hello [OK]", Styled(nil, "hello :check:")) +} + +func TestStyledf_Good_NilStyle(t *testing.T) { + restoreThemeAndColors(t) + UseASCII() + + assert.Equal(t, "value: [WARN]", Styledf(nil, "value: %s", ":warn:")) +}