mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-19 23:14:55 +02:00
committed by
Kovid Goyal
parent
e9f3844f64
commit
69c608c70f
@@ -81,24 +81,44 @@ func get_unique_directories(paths []string) []string {
|
||||
return result
|
||||
}
|
||||
|
||||
// resolve_path resolves symlinks in the directory component of path so the result
|
||||
// matches what the OS-level file-system watcher (FSEvents on macOS, inotify on Linux)
|
||||
// reports. This is important on macOS where /tmp is a symlink to /private/tmp.
|
||||
// If the directory cannot be resolved (e.g. it doesn't exist yet) the path is
|
||||
// returned cleaned but unresolved.
|
||||
func resolve_path(path string) string {
|
||||
dir := filepath.Dir(path)
|
||||
if resolved, err := filepath.EvalSymlinks(dir); err == nil {
|
||||
return filepath.Join(resolved, filepath.Base(path))
|
||||
}
|
||||
return filepath.Clean(path)
|
||||
}
|
||||
|
||||
func get_set_of_config_files(config_paths []string) *utils.Set[string] {
|
||||
cp := config.ConfigParser{
|
||||
AllIncludedFiles: utils.NewSet[string](), LineHandler: func(k, v string) error { return nil }}
|
||||
cp.ParseFiles(config_paths...)
|
||||
// Resolve symlinks in all paths collected by the parser (important on macOS
|
||||
// where /tmp -> /private/tmp causes mismatches with FSEvents-reported paths).
|
||||
result := utils.NewSet[string](cp.AllIncludedFiles.Len() + len(config_paths)*4)
|
||||
for _, p := range cp.AllIncludedFiles.AsSlice() {
|
||||
result.Add(resolve_path(p))
|
||||
}
|
||||
for _, path := range config_paths {
|
||||
path = filepath.Clean(path)
|
||||
cp.AllIncludedFiles.Add(path)
|
||||
path = resolve_path(path)
|
||||
result.Add(path)
|
||||
dir := filepath.Dir(path)
|
||||
for _, q := range []string{"dark-theme.auto.conf", "light-theme.auto.conf", "no-preference-theme.auto.conf"} {
|
||||
q = filepath.Join(filepath.Dir(path), q)
|
||||
cp.AllIncludedFiles.Add(filepath.Clean(q))
|
||||
result.Add(resolve_path(filepath.Join(dir, q)))
|
||||
}
|
||||
}
|
||||
return cp.AllIncludedFiles
|
||||
return result
|
||||
}
|
||||
|
||||
func watch_for_kitty_config_changes(action func() error, debounce_time time.Duration, config_paths []string) error {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
// watch_for_config_changes watches the directories derived from config_paths and calls action
|
||||
// whenever a watched config file (including includes and auto color scheme files) changes.
|
||||
// It runs until ctx is cancelled.
|
||||
func watch_for_config_changes(ctx context.Context, action func() error, debounce_time time.Duration, config_paths []string) error {
|
||||
event_chan := make(chan fswatcher.WatchEvent)
|
||||
all_paths := get_set_of_config_files(config_paths)
|
||||
dirs_to_watch := get_unique_directories(all_paths.AsSlice())
|
||||
@@ -108,7 +128,7 @@ func watch_for_kitty_config_changes(action func() error, debounce_time time.Dura
|
||||
|
||||
filtered_action := func(ev fswatcher.WatchEvent) error {
|
||||
all_paths := get_set_of_config_files(config_paths)
|
||||
if all_paths.Has(filepath.Clean(ev.Path)) {
|
||||
if all_paths.Has(resolve_path(ev.Path)) {
|
||||
return action()
|
||||
}
|
||||
return nil
|
||||
@@ -117,25 +137,30 @@ func watch_for_kitty_config_changes(action func() error, debounce_time time.Dura
|
||||
if err := watch_dirs(ctx, dirs_to_watch, debounce_time, event_chan); err != nil {
|
||||
return err
|
||||
}
|
||||
stdinClosed := make(chan struct{})
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
}
|
||||
close(stdinClosed)
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case event := <-event_chan:
|
||||
if err := filtered_action(event); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to signal kitty in event: %s with error: %s\n", event, err)
|
||||
}
|
||||
case <-stdinClosed:
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func watch_for_kitty_config_changes(action func() error, debounce_time time.Duration, config_paths []string) error {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
}
|
||||
cancel()
|
||||
}()
|
||||
return watch_for_config_changes(ctx, action, debounce_time, config_paths)
|
||||
}
|
||||
|
||||
func signal_kitty_to_reload_config(kitty_pid int) error {
|
||||
return unix.Kill(kitty_pid, unix.SIGUSR1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user