From 9ff396c69f7022e75e4c792fa3ee3e9139f30da5 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 4 Sep 2016 12:50:13 -0400 Subject: [PATCH] Don't allow setting invalid colorschemes Fixes #303 --- cmd/micro/colorscheme.go | 25 +++++++++++++++++++++++++ cmd/micro/settings.go | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/micro/colorscheme.go b/cmd/micro/colorscheme.go index 94527af8..0bedec5e 100644 --- a/cmd/micro/colorscheme.go +++ b/cmd/micro/colorscheme.go @@ -18,6 +18,24 @@ var colorscheme Colorscheme var preInstalledColors = []string{"default", "simple", "solarized", "solarized-tc", "atom-dark-tc", "monokai", "gruvbox", "zenburn", "bubblegum"} +// ColorschemeExists checks if a given colorscheme exists +func ColorschemeExists(colorschemeName string) bool { + files, _ := ioutil.ReadDir(configDir) + for _, f := range files { + if f.Name() == colorschemeName+".micro" { + return true + } + } + + for _, name := range preInstalledColors { + if name == colorschemeName { + return true + } + } + + return false +} + // InitColorscheme picks and initializes the colorscheme when micro starts func InitColorscheme() { LoadDefaultColorscheme() @@ -31,6 +49,7 @@ func LoadDefaultColorscheme() { // LoadColorscheme loads the given colorscheme from a directory func LoadColorscheme(colorschemeName, dir string) { files, _ := ioutil.ReadDir(dir) + found := false for _, f := range files { if f.Name() == colorschemeName+".micro" { text, err := ioutil.ReadFile(dir + "/" + f.Name()) @@ -39,6 +58,7 @@ func LoadColorscheme(colorschemeName, dir string) { continue } colorscheme = ParseColorscheme(string(text)) + found = true } } @@ -50,8 +70,13 @@ func LoadColorscheme(colorschemeName, dir string) { continue } colorscheme = ParseColorscheme(string(data)) + found = true } } + + if !found { + TermMessage(colorschemeName, "is not a valid colorscheme") + } } // ParseColorscheme parses the text definition for a colorscheme and returns the corresponding object diff --git a/cmd/micro/settings.go b/cmd/micro/settings.go index 9f1d7915..f46b1f81 100644 --- a/cmd/micro/settings.go +++ b/cmd/micro/settings.go @@ -208,6 +208,12 @@ func DefaultLocalSettings() map[string]interface{} { // is local only it will set the local version // Use setlocal to force an option to be set locally func SetOption(option, value string) error { + if option == "colorscheme" { + if !ColorschemeExists(value) { + return errors.New(value + " is not a valid colorscheme") + } + } + if _, ok := globalSettings[option]; !ok { if _, ok := CurView().Buf.Settings[option]; !ok { return errors.New("Invalid option")