11 // The options that the user can set
14 // All the possible settings
15 var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax"}
17 // The Settings struct contains the settings for micro
18 type Settings struct {
19 Colorscheme string `json:"colorscheme"`
20 TabSize int `json:"tabsize"`
21 AutoIndent bool `json:"autoindent"`
22 Syntax bool `json:"syntax"`
25 // InitSettings initializes the options map and sets all options to their default values
27 filename := configDir + "/settings.json"
28 if _, e := os.Stat(filename); e == nil {
29 input, err := ioutil.ReadFile(filename)
31 TermMessage("Error reading settings.json file: " + err.Error())
35 json.Unmarshal(input, &settings)
37 settings = DefaultSettings()
38 err := WriteSettings(filename)
40 TermMessage("Error writing settings.json file: " + err.Error())
45 // WriteSettings writes the settings to the specified filename as JSON
46 func WriteSettings(filename string) error {
48 if _, e := os.Stat(configDir); e == nil {
49 txt, _ := json.MarshalIndent(settings, "", " ")
50 err = ioutil.WriteFile(filename, txt, 0644)
55 // DefaultSettings returns the default settings for micro
56 func DefaultSettings() Settings {
58 Colorscheme: "default",
65 // SetOption prompts the user to set an option and checks that the response is valid
66 func SetOption(view *View, args []string) {
67 filename := configDir + "/settings.json"
69 option := strings.TrimSpace(args[0])
70 value := strings.TrimSpace(args[1])
72 if Contains(possibleSettings, option) {
73 if option == "tabsize" {
74 tsize, err := strconv.Atoi(value)
76 messenger.Error("Invalid value for " + option)
79 settings.TabSize = tsize
80 } else if option == "colorscheme" {
81 settings.Colorscheme = value
83 view.buf.UpdateRules()
84 } else if option == "syntax" {
86 settings.Syntax = true
87 } else if value == "off" {
88 settings.Syntax = false
90 messenger.Error("Invalid value for " + option)
94 view.buf.UpdateRules()
96 err := WriteSettings(filename)
98 messenger.Error("Error writing to settings.json: " + err.Error())
102 messenger.Error("Option " + option + " does not exist")
105 messenger.Error("Invalid option, please use option value")