12 // The options that the user can set
13 var settings map[string]interface{}
15 // InitSettings initializes the options map and sets all options to their default values
17 defaults := DefaultSettings()
18 var parsed map[string]interface{}
20 filename := configDir + "/settings.json"
21 if _, e := os.Stat(filename); e == nil {
22 input, err := ioutil.ReadFile(filename)
24 TermMessage("Error reading settings.json file: " + err.Error())
28 err = json.Unmarshal(input, &parsed)
30 TermMessage("Error reading settings.json:", err.Error())
34 settings = make(map[string]interface{})
35 for k, v := range defaults {
38 for k, v := range parsed {
42 err := WriteSettings(filename)
44 TermMessage("Error writing settings.json file: " + err.Error())
48 // WriteSettings writes the settings to the specified filename as JSON
49 func WriteSettings(filename string) error {
51 if _, e := os.Stat(configDir); e == nil {
52 txt, _ := json.MarshalIndent(settings, "", " ")
53 err = ioutil.WriteFile(filename, txt, 0644)
58 // DefaultSettings returns the default settings for micro
59 func DefaultSettings() map[string]interface{} {
60 return map[string]interface{}{
61 "colorscheme": "default",
62 "tabsize": float64(4),
65 "tabsToSpaces": false,
72 // SetOption prompts the user to set an option and checks that the response is valid
73 func SetOption(view *View, args []string) {
74 filename := configDir + "/settings.json"
76 option := strings.TrimSpace(args[0])
77 value := strings.TrimSpace(args[1])
79 if _, ok := settings[option]; !ok {
80 messenger.Error(option + " is not a valid option")
84 kind := reflect.TypeOf(settings[option]).Kind()
85 if kind == reflect.Bool {
86 b, err := ParseBool(value)
88 messenger.Error("Invalid value for " + option)
92 } else if kind == reflect.String {
93 settings[option] = value
94 } else if kind == reflect.Float64 {
95 i, err := strconv.Atoi(value)
97 messenger.Error("Invalid value for " + option)
100 settings[option] = float64(i)
103 if option == "colorscheme" {
105 view.Buf.UpdateRules()
108 err := WriteSettings(filename)
110 messenger.Error("Error writing to settings.json: " + err.Error())
114 messenger.Error("No value given")