From 0614c6396652c2a1f27ee9dc4d3135cd6f14b4ca Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 22 Feb 2023 12:02:41 +0530 Subject: [PATCH] Handle XDG_CONFIG_DIRS in Go as well --- tools/utils/paths.go | 55 ++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/tools/utils/paths.go b/tools/utils/paths.go index 21be2a29c..d73264d89 100644 --- a/tools/utils/paths.go +++ b/tools/utils/paths.go @@ -71,33 +71,44 @@ var KittyExe = (&Once[string]{Run: func() string { }}).Get var ConfigDir = (&Once[string]{Run: func() (config_dir string) { - if os.Getenv("KITTY_CONFIG_DIRECTORY") != "" { - config_dir = Abspath(Expanduser(os.Getenv("KITTY_CONFIG_DIRECTORY"))) - } else { - var locations []string - if os.Getenv("XDG_CONFIG_HOME") != "" { - locations = append(locations, os.Getenv("XDG_CACHE_HOME")) + if kcd := os.Getenv("KITTY_CONFIG_DIRECTORY"); kcd != "" { + return Abspath(Expanduser(kcd)) + } + var locations []string + seen := Set[string]{} + add := func(x string) { + x = Abspath(Expanduser(x)) + if !seen.Has(x) { + seen.Add(x) + locations = append(locations, x) } - locations = append(locations, Expanduser("~/.config")) - if runtime.GOOS == "darwin" { - locations = append(locations, Expanduser("~/Library/Preferences")) + } + if xh := os.Getenv("XDG_CONFIG_HOME"); xh != "" { + add(xh) + } + if dirs := os.Getenv("XDG_CONFIG_DIRS"); dirs != "" { + for _, candidate := range strings.Split(dirs, ":") { + add(candidate) } - for _, loc := range locations { - if loc != "" { - q := filepath.Join(loc, "kitty") - if _, err := os.Stat(filepath.Join(q, "kitty.conf")); err == nil { - config_dir = q - break - } - } - } - for _, loc := range locations { - if loc != "" { - config_dir = filepath.Join(loc, "kitty") - break + } + add("~/.config") + if runtime.GOOS == "darwin" { + add("~/Library/Preferences") + } + for _, loc := range locations { + if loc != "" { + q := filepath.Join(loc, "kitty") + if _, err := os.Stat(filepath.Join(q, "kitty.conf")); err == nil { + config_dir = q + return } } } + config_dir = os.Getenv("XDG_CONFIG_HOME") + if config_dir == "" { + config_dir = "~/.config" + } + config_dir = filepath.Join(Expanduser(config_dir), "kitty") return }}).Get