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 settings = make(map[string]interface{})
18 filename := configDir + "/settings.json"
19 if _, e := os.Stat(filename); e == nil {
20 input, err := ioutil.ReadFile(filename)
22 TermMessage("Error reading settings.json file: " + err.Error())
26 err = json.Unmarshal(input, &settings)
28 TermMessage("Error reading settings.json:", err.Error())
31 settings = DefaultSettings()
32 err := WriteSettings(filename)
34 TermMessage("Error writing settings.json file: " + err.Error())
39 // WriteSettings writes the settings to the specified filename as JSON
40 func WriteSettings(filename string) error {
42 if _, e := os.Stat(configDir); e == nil {
43 txt, _ := json.MarshalIndent(settings, "", " ")
44 err = ioutil.WriteFile(filename, txt, 0644)
49 // DefaultSettings returns the default settings for micro
50 func DefaultSettings() map[string]interface{} {
51 return map[string]interface{}{
52 "colorscheme": "default",
56 "tabsToSpaces": false,
63 // SetOption prompts the user to set an option and checks that the response is valid
64 func SetOption(view *View, args []string) {
65 filename := configDir + "/settings.json"
67 option := strings.TrimSpace(args[0])
68 value := strings.TrimSpace(args[1])
70 if _, ok := settings[option]; !ok {
71 messenger.Error(option + " is not a valid option")
75 kind := reflect.TypeOf(settings[option]).Kind()
76 if kind == reflect.Bool {
77 b, err := ParseBool(value)
79 messenger.Error("Invalid value for " + option)
83 } else if kind == reflect.String {
84 settings[option] = value
85 } else if kind == reflect.Float64 {
86 i, err := strconv.Atoi(value)
88 messenger.Error("Invalid value for " + option)
91 settings[option] = float64(i)
94 if option == "colorscheme" {
96 view.buf.UpdateRules()
99 err := WriteSettings(filename)
101 messenger.Error("Error writing to settings.json: " + err.Error())
105 messenger.Error("No value given")