fix(cli): make styled helpers nil-safe
Some checks are pending
Security Scan / security (push) Waiting to run
Some checks are pending
Security Scan / security (push) Waiting to run
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
aa5c0f810a
commit
aa537c89ca
2 changed files with 20 additions and 0 deletions
|
|
@ -14,11 +14,17 @@ func Sprint(args ...any) string {
|
||||||
|
|
||||||
// Styled returns text with a style applied.
|
// Styled returns text with a style applied.
|
||||||
func Styled(style *AnsiStyle, text string) string {
|
func Styled(style *AnsiStyle, text string) string {
|
||||||
|
if style == nil {
|
||||||
|
return compileGlyphs(text)
|
||||||
|
}
|
||||||
return style.Render(compileGlyphs(text))
|
return style.Render(compileGlyphs(text))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Styledf returns formatted text with a style applied.
|
// Styledf returns formatted text with a style applied.
|
||||||
func Styledf(style *AnsiStyle, format string, args ...any) string {
|
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...)))
|
return style.Render(compileGlyphs(fmt.Sprintf(format, args...)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -222,3 +222,17 @@ func TestPad_Good(t *testing.T) {
|
||||||
assert.Equal(t, "hello", Pad("hello", 3))
|
assert.Equal(t, "hello", Pad("hello", 3))
|
||||||
assert.Equal(t, "東京 ", Pad("東京", 6))
|
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:"))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue