]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/settings.go
Small optimization
[micro.git] / cmd / micro / settings.go
index 0e026f5d1d5768c9e2ad7a8fe373b4de5d7a03dc..5eade696fb1615548344b70883bb2e97bfb21b3c 100644 (file)
@@ -2,36 +2,22 @@ package main
 
 import (
        "encoding/json"
-       "github.com/mitchellh/go-homedir"
        "io/ioutil"
        "os"
+       "reflect"
        "strconv"
        "strings"
 )
 
 // The options that the user can set
-var settings Settings
-
-// All the possible settings
-var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax"}
-
-// The Settings struct contains the settings for micro
-type Settings struct {
-       Colorscheme string `json:"colorscheme"`
-       TabSize     int    `json:"tabsize"`
-       AutoIndent  bool   `json:"autoindent"`
-       Syntax      bool   `json:"syntax"`
-}
+var settings map[string]interface{}
 
 // InitSettings initializes the options map and sets all options to their default values
 func InitSettings() {
-       home, err := homedir.Dir()
-       if err != nil {
-               TermMessage("Error finding your home directory\nCan't load settings file")
-               return
-       }
+       defaults := DefaultSettings()
+       var parsed map[string]interface{}
 
-       filename := home + "/.micro/settings.json"
+       filename := configDir + "/settings.json"
        if _, e := os.Stat(filename); e == nil {
                input, err := ioutil.ReadFile(filename)
                if err != nil {
@@ -39,24 +25,30 @@ func InitSettings() {
                        return
                }
 
-               json.Unmarshal(input, &settings)
-       } else {
-               settings = DefaultSettings()
-               err := WriteSettings(filename)
+               err = json.Unmarshal(input, &parsed)
                if err != nil {
-                       TermMessage("Error writing settings.json file: " + err.Error())
+                       TermMessage("Error reading settings.json:", err.Error())
                }
        }
+
+       settings = make(map[string]interface{})
+       for k, v := range defaults {
+               settings[k] = v
+       }
+       for k, v := range parsed {
+               settings[k] = v
+       }
+
+       err := WriteSettings(filename)
+       if err != nil {
+               TermMessage("Error writing settings.json file: " + err.Error())
+       }
 }
 
 // WriteSettings writes the settings to the specified filename as JSON
 func WriteSettings(filename string) error {
        var err error
-       home, err := homedir.Dir()
-       if err != nil {
-               return err
-       }
-       if _, e := os.Stat(home + "/.micro"); e == nil {
+       if _, e := os.Stat(configDir); e == nil {
                txt, _ := json.MarshalIndent(settings, "", "    ")
                err = ioutil.WriteFile(filename, txt, 0644)
        }
@@ -64,60 +56,61 @@ func WriteSettings(filename string) error {
 }
 
 // DefaultSettings returns the default settings for micro
-func DefaultSettings() Settings {
-       return Settings{
-               Colorscheme: "default",
-               TabSize:     4,
-               AutoIndent:  true,
-               Syntax:      true,
+func DefaultSettings() map[string]interface{} {
+       return map[string]interface{}{
+               "colorscheme":  "default",
+               "tabsize":      float64(4),
+               "autoindent":   true,
+               "syntax":       true,
+               "tabsToSpaces": false,
+               "ruler":        true,
+               "gofmt":        false,
+               "goimports":    false,
        }
 }
 
 // SetOption prompts the user to set an option and checks that the response is valid
 func SetOption(view *View, args []string) {
-       home, err := homedir.Dir()
-       if err != nil {
-               messenger.Error("Error finding your home directory\nCan't load settings file")
-       }
-
-       filename := home + "/.micro/settings.json"
+       filename := configDir + "/settings.json"
        if len(args) == 2 {
                option := strings.TrimSpace(args[0])
                value := strings.TrimSpace(args[1])
 
-               if Contains(possibleSettings, option) {
-                       if option == "tabsize" {
-                               tsize, err := strconv.Atoi(value)
-                               if err != nil {
-                                       messenger.Error("Invalid value for " + option)
-                                       return
-                               }
-                               settings.TabSize = tsize
-                       } else if option == "colorscheme" {
-                               settings.Colorscheme = value
-                               LoadSyntaxFiles()
-                               view.buf.UpdateRules()
-                       } else if option == "syntax" {
-                               if value == "on" {
-                                       settings.Syntax = true
-                               } else if value == "off" {
-                                       settings.Syntax = false
-                               } else {
-                                       messenger.Error("Invalid value for " + option)
-                                       return
-                               }
-                               LoadSyntaxFiles()
-                               view.buf.UpdateRules()
+               if _, ok := settings[option]; !ok {
+                       messenger.Error(option + " is not a valid option")
+                       return
+               }
+
+               kind := reflect.TypeOf(settings[option]).Kind()
+               if kind == reflect.Bool {
+                       b, err := ParseBool(value)
+                       if err != nil {
+                               messenger.Error("Invalid value for " + option)
+                               return
                        }
-                       err := WriteSettings(filename)
+                       settings[option] = b
+               } else if kind == reflect.String {
+                       settings[option] = value
+               } else if kind == reflect.Float64 {
+                       i, err := strconv.Atoi(value)
                        if err != nil {
-                               messenger.Error("Error writing to settings.json: " + err.Error())
+                               messenger.Error("Invalid value for " + option)
                                return
                        }
-               } else {
-                       messenger.Error("Option " + option + " does not exist")
+                       settings[option] = float64(i)
+               }
+
+               if option == "colorscheme" {
+                       LoadSyntaxFiles()
+                       view.buf.UpdateRules()
+               }
+
+               err := WriteSettings(filename)
+               if err != nil {
+                       messenger.Error("Error writing to settings.json: " + err.Error())
+                       return
                }
        } else {
-               messenger.Error("Invalid option, please use option value")
+               messenger.Error("No value given")
        }
 }