From 50d9158920cfcba3e6106be2b12daf2f32646746 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 12:45:24 +0000 Subject: [PATCH] fix(cli): restore colors after ascii theme Co-Authored-By: Virgil --- pkg/cli/ansi.go | 17 +++++++++++++++-- pkg/cli/ansi_test.go | 25 +++++++++++++++++++++++++ pkg/cli/glyph.go | 13 +++++++++++-- pkg/cli/prompt_test.go | 12 ++++++++++-- pkg/cli/utils.go | 7 ------- 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/pkg/cli/ansi.go b/pkg/cli/ansi.go index e4df66e..7ce87db 100644 --- a/pkg/cli/ansi.go +++ b/pkg/cli/ansi.go @@ -18,8 +18,9 @@ const ( ) var ( - colorEnabled = true - colorEnabledMu sync.RWMutex + colorEnabled = true + colorEnabledMu sync.RWMutex + asciiDisabledColors bool ) func init() { @@ -48,6 +49,18 @@ func ColorEnabled() bool { func SetColorEnabled(enabled bool) { colorEnabledMu.Lock() colorEnabled = enabled + if enabled { + asciiDisabledColors = false + } + colorEnabledMu.Unlock() +} + +func restoreColorIfASCII() { + colorEnabledMu.Lock() + if asciiDisabledColors { + colorEnabled = true + asciiDisabledColors = false + } colorEnabledMu.Unlock() } diff --git a/pkg/cli/ansi_test.go b/pkg/cli/ansi_test.go index d73efc2..7912aa4 100644 --- a/pkg/cli/ansi_test.go +++ b/pkg/cli/ansi_test.go @@ -86,6 +86,31 @@ func TestUseASCII_Good(t *testing.T) { } } +func TestUseUnicodeAndEmojiRestoreColorsAfterASCII(t *testing.T) { + restoreThemeAndColors(t) + + SetColorEnabled(true) + UseASCII() + if ColorEnabled() { + t.Fatal("UseASCII should disable colors") + } + + UseUnicode() + if !ColorEnabled() { + t.Fatal("UseUnicode should restore colors after ASCII mode") + } + + UseASCII() + if ColorEnabled() { + t.Fatal("UseASCII should disable colors again") + } + + UseEmoji() + if !ColorEnabled() { + t.Fatal("UseEmoji should restore colors after ASCII mode") + } +} + func TestRender_NilStyle_Good(t *testing.T) { var s *AnsiStyle got := s.Render("test") diff --git a/pkg/cli/glyph.go b/pkg/cli/glyph.go index 26023e5..1cc29f2 100644 --- a/pkg/cli/glyph.go +++ b/pkg/cli/glyph.go @@ -20,15 +20,24 @@ const ( var currentTheme = ThemeUnicode // UseUnicode switches the glyph theme to Unicode. -func UseUnicode() { currentTheme = ThemeUnicode } +func UseUnicode() { + currentTheme = ThemeUnicode + restoreColorIfASCII() +} // UseEmoji switches the glyph theme to Emoji. -func UseEmoji() { currentTheme = ThemeEmoji } +func UseEmoji() { + currentTheme = ThemeEmoji + restoreColorIfASCII() +} // UseASCII switches the glyph theme to ASCII and disables colors. func UseASCII() { currentTheme = ThemeASCII SetColorEnabled(false) + colorEnabledMu.Lock() + asciiDisabledColors = true + colorEnabledMu.Unlock() } func glyphMap() map[string]string { diff --git a/pkg/cli/prompt_test.go b/pkg/cli/prompt_test.go index 162864e..b873b56 100644 --- a/pkg/cli/prompt_test.go +++ b/pkg/cli/prompt_test.go @@ -291,6 +291,14 @@ func TestChooseMulti_Good_EmptyWithoutDefaultReturnsNone(t *testing.T) { assert.Empty(t, vals) } +func TestChooseMulti_Good_EmptyIgnoresDefaultIndex(t *testing.T) { + SetStdin(strings.NewReader("\n")) + defer SetStdin(nil) + + vals := ChooseMulti("Pick", []string{"a", "b", "c"}, WithDefaultIndex[string](1)) + assert.Empty(t, vals) +} + func TestChoose_Good_Filter(t *testing.T) { SetStdin(strings.NewReader("ap\n2\n")) defer SetStdin(nil) @@ -368,12 +376,12 @@ func TestChooseMulti_Good_CommasAndRanges(t *testing.T) { assert.Equal(t, []string{"a", "b", "d"}, vals) } -func TestChooseMulti_Good_DefaultIndex(t *testing.T) { +func TestChooseMulti_Good_DefaultIndexIgnored(t *testing.T) { SetStdin(strings.NewReader("\n")) defer SetStdin(nil) vals := ChooseMulti("Pick", []string{"a", "b", "c"}, WithDefaultIndex[string](1)) - assert.Equal(t, []string{"b"}, vals) + assert.Empty(t, vals) } func TestSetStdin_Good_ResetNil(t *testing.T) { diff --git a/pkg/cli/utils.go b/pkg/cli/utils.go index 1c76ec7..ecab93f 100644 --- a/pkg/cli/utils.go +++ b/pkg/cli/utils.go @@ -479,13 +479,6 @@ func ChooseMulti[T any](prompt string, items []T, opts ...ChooseOption[T]) []T { promptHint("Filter cleared.") continue } - if idx, ok := defaultVisibleIndex(visible, cfg.defaultN); ok { - return []T{items[idx]} - } - if cfg.defaultN >= 0 { - promptHint("Default selection is not available in the current list. Narrow the list or choose another number.") - continue - } return nil } -- 2.45.3