From 9ebcff9b78ef6c529969d34ebac757af9a28ebb2 Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Sat, 7 Mar 2020 09:03:25 -0600 Subject: Add ability to look in system-wide directories for configuration files --- colorschemes/registry.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'colorschemes') diff --git a/colorschemes/registry.go b/colorschemes/registry.go index d79271d..8f86491 100644 --- a/colorschemes/registry.go +++ b/colorschemes/registry.go @@ -3,8 +3,10 @@ package colorschemes import ( "encoding/json" "fmt" - "io/ioutil" "path/filepath" + "strings" + + "github.com/shibukawa/configdir" ) var registry map[string]Colorscheme @@ -15,7 +17,7 @@ func init() { } } -func FromName(confDir string, c string) (Colorscheme, error) { +func FromName(confDir configdir.ConfigDir, c string) (Colorscheme, error) { cs, ok := registry[c] if !ok { cs, err := getCustomColorscheme(confDir, c) @@ -34,12 +36,20 @@ func register(name string, c Colorscheme) { } // getCustomColorscheme tries to read a custom json colorscheme from /.json -func getCustomColorscheme(confDir string, name string) (Colorscheme, error) { +func getCustomColorscheme(confDir configdir.ConfigDir, name string) (Colorscheme, error) { var cs Colorscheme - filePath := filepath.Join(confDir, name+".json") - dat, err := ioutil.ReadFile(filePath) + fn := name + ".json" + folder := confDir.QueryFolderContainsFile(fn) + if folder == nil { + paths := make([]string, 0) + for _, d := range confDir.QueryFolders(configdir.Existing) { + paths = append(paths, d.Path) + } + return cs, fmt.Errorf("failed to find colorscheme file %s in %s", fn, strings.Join(paths, ", ")) + } + dat, err := folder.ReadFile(fn) if err != nil { - return cs, fmt.Errorf("failed to read colorscheme file: %v", err) + return cs, fmt.Errorf("failed to read colorscheme file %s: %v", filepath.Join(folder.Path, fn), err) } err = json.Unmarshal(dat, &cs) if err != nil { -- cgit v1.2.3