]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/settings.go
make undothresthold a setting (part 2)
[micro.git] / cmd / micro / settings.go
index 0e026f5d1d5768c9e2ad7a8fe373b4de5d7a03dc..8124a801b9f75a9dc8baeda15d3bc885b99720d4 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,85 +25,114 @@ 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)
        }
        return err
 }
 
+// AddOption creates a new option. This is meant to be called by plugins to add options.
+func AddOption(name string, value interface{}) {
+       settings[name] = value
+       err := WriteSettings(configDir + "/settings.json")
+       if err != nil {
+               TermMessage("Error writing settings.json file: " + err.Error())
+       }
+}
+
+// GetOption returns the specified option. This is meant to be called by plugins to add options.
+func GetOption(name string) interface{} {
+       return settings[name]
+}
+
 // 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{}{
+               "autoindent":    true,
+               "colorscheme":   "default",
+               "ignorecase":    false,
+               "indentchar":    " ",
+               "ruler":         true,
+               "scrollspeed":   float64(2),
+               "scrollmargin":  float64(3),
+               "statusline":    true,
+               "syntax":        true,
+               "tabsize":       float64(4),
+               "tabstospaces":  false,
+               "undothreshold": float64(500),
        }
 }
 
 // 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()
+               }
+
+               if option == "statusline" {
+                       view.Resize(screen.Size())
+               }
+
+               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")
        }
 }