12 // The options that the user can set
15 // All the possible settings
16 // This map maps the name of the setting in the Settings struct
17 // to the name that the user will actually use (the one in the json file)
18 var possibleSettings = map[string]string{
19 "colorscheme": "Colorscheme",
21 "autoindent": "AutoIndent",
23 "tabsToSpaces": "TabsToSpaces",
26 "goimports": "GoImports",
29 // The Settings struct contains the settings for micro
30 type Settings struct {
31 Colorscheme string `json:"colorscheme"`
32 TabSize int `json:"tabsize"`
33 AutoIndent bool `json:"autoindent"`
34 Syntax bool `json:"syntax"`
35 TabsToSpaces bool `json:"tabsToSpaces"`
36 Ruler bool `json:"ruler"`
37 GoFmt bool `json:"gofmt"`
38 GoImports bool `json:"goimports"`
41 // InitSettings initializes the options map and sets all options to their default values
43 filename := configDir + "/settings.json"
44 if _, e := os.Stat(filename); e == nil {
45 input, err := ioutil.ReadFile(filename)
47 TermMessage("Error reading settings.json file: " + err.Error())
51 err = json.Unmarshal(input, &settings)
53 TermMessage("Error reading settings.json:", err.Error())
56 settings = DefaultSettings()
57 err := WriteSettings(filename)
59 TermMessage("Error writing settings.json file: " + err.Error())
64 // WriteSettings writes the settings to the specified filename as JSON
65 func WriteSettings(filename string) error {
67 if _, e := os.Stat(configDir); e == nil {
68 txt, _ := json.MarshalIndent(settings, "", " ")
69 err = ioutil.WriteFile(filename, txt, 0644)
74 // DefaultSettings returns the default settings for micro
75 func DefaultSettings() Settings {
77 Colorscheme: "default",
88 // SetOption prompts the user to set an option and checks that the response is valid
89 func SetOption(view *View, args []string) {
90 filename := configDir + "/settings.json"
92 option := strings.TrimSpace(args[0])
93 value := strings.TrimSpace(args[1])
95 mutable := reflect.ValueOf(&settings).Elem()
96 field := mutable.FieldByName(possibleSettings[option])
98 messenger.Error(option + " is not a valid option")
101 kind := field.Type().Kind()
102 if kind == reflect.Bool {
103 b, err := ParseBool(value)
105 messenger.Error("Invalid value for " + option)
109 } else if kind == reflect.String {
110 field.SetString(value)
111 } else if kind == reflect.Int {
112 i, err := strconv.Atoi(value)
114 messenger.Error("Invalid value for " + option)
117 field.SetInt(int64(i))
120 if option == "colorscheme" {
122 view.buf.UpdateRules()
125 err := WriteSettings(filename)
127 messenger.Error("Error writing to settings.json: " + err.Error())
131 messenger.Error("No value given")