fix(cli): restore colors after ascii theme

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 12:45:24 +00:00
parent 3862b7c032
commit 50d9158920
5 changed files with 61 additions and 13 deletions

View file

@ -18,8 +18,9 @@ const (
) )
var ( var (
colorEnabled = true colorEnabled = true
colorEnabledMu sync.RWMutex colorEnabledMu sync.RWMutex
asciiDisabledColors bool
) )
func init() { func init() {
@ -48,6 +49,18 @@ func ColorEnabled() bool {
func SetColorEnabled(enabled bool) { func SetColorEnabled(enabled bool) {
colorEnabledMu.Lock() colorEnabledMu.Lock()
colorEnabled = enabled colorEnabled = enabled
if enabled {
asciiDisabledColors = false
}
colorEnabledMu.Unlock()
}
func restoreColorIfASCII() {
colorEnabledMu.Lock()
if asciiDisabledColors {
colorEnabled = true
asciiDisabledColors = false
}
colorEnabledMu.Unlock() colorEnabledMu.Unlock()
} }

View file

@ -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) { func TestRender_NilStyle_Good(t *testing.T) {
var s *AnsiStyle var s *AnsiStyle
got := s.Render("test") got := s.Render("test")

View file

@ -20,15 +20,24 @@ const (
var currentTheme = ThemeUnicode var currentTheme = ThemeUnicode
// UseUnicode switches the glyph theme to Unicode. // UseUnicode switches the glyph theme to Unicode.
func UseUnicode() { currentTheme = ThemeUnicode } func UseUnicode() {
currentTheme = ThemeUnicode
restoreColorIfASCII()
}
// UseEmoji switches the glyph theme to Emoji. // 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. // UseASCII switches the glyph theme to ASCII and disables colors.
func UseASCII() { func UseASCII() {
currentTheme = ThemeASCII currentTheme = ThemeASCII
SetColorEnabled(false) SetColorEnabled(false)
colorEnabledMu.Lock()
asciiDisabledColors = true
colorEnabledMu.Unlock()
} }
func glyphMap() map[string]string { func glyphMap() map[string]string {

View file

@ -291,6 +291,14 @@ func TestChooseMulti_Good_EmptyWithoutDefaultReturnsNone(t *testing.T) {
assert.Empty(t, vals) 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) { func TestChoose_Good_Filter(t *testing.T) {
SetStdin(strings.NewReader("ap\n2\n")) SetStdin(strings.NewReader("ap\n2\n"))
defer SetStdin(nil) defer SetStdin(nil)
@ -368,12 +376,12 @@ func TestChooseMulti_Good_CommasAndRanges(t *testing.T) {
assert.Equal(t, []string{"a", "b", "d"}, vals) 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")) SetStdin(strings.NewReader("\n"))
defer SetStdin(nil) defer SetStdin(nil)
vals := ChooseMulti("Pick", []string{"a", "b", "c"}, WithDefaultIndex[string](1)) 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) { func TestSetStdin_Good_ResetNil(t *testing.T) {

View file

@ -479,13 +479,6 @@ func ChooseMulti[T any](prompt string, items []T, opts ...ChooseOption[T]) []T {
promptHint("Filter cleared.") promptHint("Filter cleared.")
continue 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 return nil
} }