Handle color scheme changes in choose-files kitten

This commit is contained in:
Kovid Goyal 2025-07-20 13:43:59 +05:30
parent 9068bbaba9
commit 2bdbbd909c
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
4 changed files with 37 additions and 0 deletions

View file

@ -713,6 +713,7 @@ func (h *Handler) set_state_from_config(conf *Config, opts *Options) (err error)
}
var default_cwd string
var use_light_colors bool
func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
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) {
return 1, err
}
lp.MouseTrackingMode(loop.FULL_MOUSE_TRACKING)
lp.ColorSchemeChangeNotifications()
handler := Handler{lp: lp, err_chan: make(chan error, 8), msg_printer: message.NewPrinter(utils.LanguageTag()), spinner: tui.NewSpinner("dots")}
handler.rl = readline.New(lp, readline.RlInit{
Prompt: "> ", ContinuationPrompt: ". ", Completer: handler.complete_save_prompt,
@ -796,12 +798,22 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
if opts.Title != "" {
lp.SetWindowTitle(opts.Title)
}
lp.RequestCurrentColorScheme()
return handler.OnInitialize()
}
lp.OnResize = func(old, new_size loop.ScreenSize) (err error) {
handler.init_sizes(new_size)
return handler.draw_screen()
}
lp.OnColorSchemeChange = func(p loop.ColorPreference) (err error) {
new_val := p == loop.LIGHT_COLOR_PREFERENCE
if new_val != use_light_colors {
use_light_colors = new_val
handler.preview_manager.invalidate_color_scheme_based_cached_items()
return handler.draw_screen()
}
return
}
lp.OnKeyEvent = handler.OnKeyEvent
lp.OnText = handler.OnText
lp.OnMouseEvent = handler.OnMouseEvent

View file

@ -3,6 +3,7 @@ package choose_files
import (
"fmt"
"io/fs"
"maps"
"os"
"path/filepath"
"slices"
@ -20,6 +21,7 @@ var _ = fmt.Print
type Preview interface {
Render(h *Handler, x, y, width, height int)
IsValidForColorScheme(light bool) bool
}
type PreviewManager struct {
@ -71,6 +73,8 @@ type MessagePreview struct {
trailers []string
}
func (p MessagePreview) IsValidForColorScheme(bool) bool { return true }
func (p MessagePreview) Render(h *Handler, x, y, width, height int) {
offset := 0
if p.title != "" {
@ -163,6 +167,12 @@ func NewFileMetadataPreview(abspath string, metadata fs.FileInfo) Preview {
return &MessagePreview{title: title, msg: h, trailers: t}
}
func (pm *PreviewManager) invalidate_color_scheme_based_cached_items() {
pm.lock.Lock()
defer pm.lock.Unlock()
maps.DeleteFunc(pm.cache, func(key string, p Preview) bool { return !p.IsValidForColorScheme(use_light_colors) })
}
func (pm *PreviewManager) preview_for(abspath string, ftype fs.FileMode) (ans Preview) {
if ans = pm.cached_preview(abspath); ans != nil {
return ans

View file

@ -218,6 +218,10 @@ func NoFocusTracking(self *Loop) {
self.terminal_options.focus_tracking = false
}
func (self *Loop) RequestCurrentColorScheme() {
self.QueueWriteString("\x1b[?996n")
}
func (self *Loop) ColorSchemeChangeNotifications() *Loop {
self.terminal_options.color_scheme_change_notification = true
return self

View file

@ -14,6 +14,17 @@ const (
LIGHT_COLOR_PREFERENCE
)
func (c ColorPreference) String() string {
switch c {
case DARK_COLOR_PREFERENCE:
return "dark"
case LIGHT_COLOR_PREFERENCE:
return "light"
default:
return "no-preference"
}
}
type TerminalCapabilities struct {
KeyboardProtocol bool
KeyboardProtocolResponseReceived bool