Skip to content

Commit 2bdbbd9

Browse files
committed
Handle color scheme changes in choose-files kitten
1 parent 9068bba commit 2bdbbd9

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

kittens/choose_files/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ func (h *Handler) set_state_from_config(conf *Config, opts *Options) (err error)
713713
}
714714

715715
var default_cwd string
716+
var use_light_colors bool
716717

717718
func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
718719
write_output := func(selections []string, interrupted bool, current_filter string) {
@@ -763,6 +764,7 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
763764
return 1, err
764765
}
765766
lp.MouseTrackingMode(loop.FULL_MOUSE_TRACKING)
767+
lp.ColorSchemeChangeNotifications()
766768
handler := Handler{lp: lp, err_chan: make(chan error, 8), msg_printer: message.NewPrinter(utils.LanguageTag()), spinner: tui.NewSpinner("dots")}
767769
handler.rl = readline.New(lp, readline.RlInit{
768770
Prompt: "> ", ContinuationPrompt: ". ", Completer: handler.complete_save_prompt,
@@ -796,12 +798,22 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
796798
if opts.Title != "" {
797799
lp.SetWindowTitle(opts.Title)
798800
}
801+
lp.RequestCurrentColorScheme()
799802
return handler.OnInitialize()
800803
}
801804
lp.OnResize = func(old, new_size loop.ScreenSize) (err error) {
802805
handler.init_sizes(new_size)
803806
return handler.draw_screen()
804807
}
808+
lp.OnColorSchemeChange = func(p loop.ColorPreference) (err error) {
809+
new_val := p == loop.LIGHT_COLOR_PREFERENCE
810+
if new_val != use_light_colors {
811+
use_light_colors = new_val
812+
handler.preview_manager.invalidate_color_scheme_based_cached_items()
813+
return handler.draw_screen()
814+
}
815+
return
816+
}
805817
lp.OnKeyEvent = handler.OnKeyEvent
806818
lp.OnText = handler.OnText
807819
lp.OnMouseEvent = handler.OnMouseEvent

kittens/choose_files/preview.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package choose_files
33
import (
44
"fmt"
55
"io/fs"
6+
"maps"
67
"os"
78
"path/filepath"
89
"slices"
@@ -20,6 +21,7 @@ var _ = fmt.Print
2021

2122
type Preview interface {
2223
Render(h *Handler, x, y, width, height int)
24+
IsValidForColorScheme(light bool) bool
2325
}
2426

2527
type PreviewManager struct {
@@ -71,6 +73,8 @@ type MessagePreview struct {
7173
trailers []string
7274
}
7375

76+
func (p MessagePreview) IsValidForColorScheme(bool) bool { return true }
77+
7478
func (p MessagePreview) Render(h *Handler, x, y, width, height int) {
7579
offset := 0
7680
if p.title != "" {
@@ -163,6 +167,12 @@ func NewFileMetadataPreview(abspath string, metadata fs.FileInfo) Preview {
163167
return &MessagePreview{title: title, msg: h, trailers: t}
164168
}
165169

170+
func (pm *PreviewManager) invalidate_color_scheme_based_cached_items() {
171+
pm.lock.Lock()
172+
defer pm.lock.Unlock()
173+
maps.DeleteFunc(pm.cache, func(key string, p Preview) bool { return !p.IsValidForColorScheme(use_light_colors) })
174+
}
175+
166176
func (pm *PreviewManager) preview_for(abspath string, ftype fs.FileMode) (ans Preview) {
167177
if ans = pm.cached_preview(abspath); ans != nil {
168178
return ans

tools/tui/loop/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ func NoFocusTracking(self *Loop) {
218218
self.terminal_options.focus_tracking = false
219219
}
220220

221+
func (self *Loop) RequestCurrentColorScheme() {
222+
self.QueueWriteString("\x1b[?996n")
223+
}
224+
221225
func (self *Loop) ColorSchemeChangeNotifications() *Loop {
222226
self.terminal_options.color_scheme_change_notification = true
223227
return self

tools/tui/loop/capabilities.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ const (
1414
LIGHT_COLOR_PREFERENCE
1515
)
1616

17+
func (c ColorPreference) String() string {
18+
switch c {
19+
case DARK_COLOR_PREFERENCE:
20+
return "dark"
21+
case LIGHT_COLOR_PREFERENCE:
22+
return "light"
23+
default:
24+
return "no-preference"
25+
}
26+
}
27+
1728
type TerminalCapabilities struct {
1829
KeyboardProtocol bool
1930
KeyboardProtocolResponseReceived bool

0 commit comments

Comments
 (0)