mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 14:18:26 +02:00
Handle color scheme changes in choose-files kitten
This commit is contained in:
@@ -713,6 +713,7 @@ func (h *Handler) set_state_from_config(conf *Config, opts *Options) (err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
var default_cwd string
|
var default_cwd string
|
||||||
|
var use_light_colors bool
|
||||||
|
|
||||||
func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
|
func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
|
||||||
write_output := func(selections []string, interrupted bool, current_filter string) {
|
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
|
return 1, err
|
||||||
}
|
}
|
||||||
lp.MouseTrackingMode(loop.FULL_MOUSE_TRACKING)
|
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 := 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{
|
handler.rl = readline.New(lp, readline.RlInit{
|
||||||
Prompt: "> ", ContinuationPrompt: ". ", Completer: handler.complete_save_prompt,
|
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 != "" {
|
if opts.Title != "" {
|
||||||
lp.SetWindowTitle(opts.Title)
|
lp.SetWindowTitle(opts.Title)
|
||||||
}
|
}
|
||||||
|
lp.RequestCurrentColorScheme()
|
||||||
return handler.OnInitialize()
|
return handler.OnInitialize()
|
||||||
}
|
}
|
||||||
lp.OnResize = func(old, new_size loop.ScreenSize) (err error) {
|
lp.OnResize = func(old, new_size loop.ScreenSize) (err error) {
|
||||||
handler.init_sizes(new_size)
|
handler.init_sizes(new_size)
|
||||||
return handler.draw_screen()
|
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.OnKeyEvent = handler.OnKeyEvent
|
||||||
lp.OnText = handler.OnText
|
lp.OnText = handler.OnText
|
||||||
lp.OnMouseEvent = handler.OnMouseEvent
|
lp.OnMouseEvent = handler.OnMouseEvent
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package choose_files
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
@@ -20,6 +21,7 @@ var _ = fmt.Print
|
|||||||
|
|
||||||
type Preview interface {
|
type Preview interface {
|
||||||
Render(h *Handler, x, y, width, height int)
|
Render(h *Handler, x, y, width, height int)
|
||||||
|
IsValidForColorScheme(light bool) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type PreviewManager struct {
|
type PreviewManager struct {
|
||||||
@@ -71,6 +73,8 @@ type MessagePreview struct {
|
|||||||
trailers []string
|
trailers []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p MessagePreview) IsValidForColorScheme(bool) bool { return true }
|
||||||
|
|
||||||
func (p MessagePreview) Render(h *Handler, x, y, width, height int) {
|
func (p MessagePreview) Render(h *Handler, x, y, width, height int) {
|
||||||
offset := 0
|
offset := 0
|
||||||
if p.title != "" {
|
if p.title != "" {
|
||||||
@@ -163,6 +167,12 @@ func NewFileMetadataPreview(abspath string, metadata fs.FileInfo) Preview {
|
|||||||
return &MessagePreview{title: title, msg: h, trailers: t}
|
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) {
|
func (pm *PreviewManager) preview_for(abspath string, ftype fs.FileMode) (ans Preview) {
|
||||||
if ans = pm.cached_preview(abspath); ans != nil {
|
if ans = pm.cached_preview(abspath); ans != nil {
|
||||||
return ans
|
return ans
|
||||||
|
|||||||
@@ -218,6 +218,10 @@ func NoFocusTracking(self *Loop) {
|
|||||||
self.terminal_options.focus_tracking = false
|
self.terminal_options.focus_tracking = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *Loop) RequestCurrentColorScheme() {
|
||||||
|
self.QueueWriteString("\x1b[?996n")
|
||||||
|
}
|
||||||
|
|
||||||
func (self *Loop) ColorSchemeChangeNotifications() *Loop {
|
func (self *Loop) ColorSchemeChangeNotifications() *Loop {
|
||||||
self.terminal_options.color_scheme_change_notification = true
|
self.terminal_options.color_scheme_change_notification = true
|
||||||
return self
|
return self
|
||||||
|
|||||||
@@ -14,6 +14,17 @@ const (
|
|||||||
LIGHT_COLOR_PREFERENCE
|
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 {
|
type TerminalCapabilities struct {
|
||||||
KeyboardProtocol bool
|
KeyboardProtocol bool
|
||||||
KeyboardProtocolResponseReceived bool
|
KeyboardProtocolResponseReceived bool
|
||||||
|
|||||||
Reference in New Issue
Block a user