11 // The options that the user can set
14 // All the possible settings
15 var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax", "tabsToSpaces", "ruler"}
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"`
23 TabsToSpaces bool `json:"tabsToSpaces"`
24 Ruler bool `json:"ruler"`
27 // InitSettings initializes the options map and sets all options to their default values
29 filename := configDir + "/settings.json"
30 if _, e := os.Stat(filename); e == nil {
31 input, err := ioutil.ReadFile(filename)
33 TermMessage("Error reading settings.json file: " + err.Error())
37 json.Unmarshal(input, &settings)
39 settings = DefaultSettings()
40 err := WriteSettings(filename)
42 TermMessage("Error writing settings.json file: " + err.Error())
47 // WriteSettings writes the settings to the specified filename as JSON
48 func WriteSettings(filename string) error {
50 if _, e := os.Stat(configDir); e == nil {
51 txt, _ := json.MarshalIndent(settings, "", " ")
52 err = ioutil.WriteFile(filename, txt, 0644)
57 // DefaultSettings returns the default settings for micro
58 func DefaultSettings() Settings {
60 Colorscheme: "default",
69 // SetOption prompts the user to set an option and checks that the response is valid
70 func SetOption(view *View, args []string) {
71 filename := configDir + "/settings.json"
73 option := strings.TrimSpace(args[0])
74 value := strings.TrimSpace(args[1])
76 if Contains(possibleSettings, option) {
77 if option == "tabsize" {
78 tsize, err := strconv.Atoi(value)
80 messenger.Error("Invalid value for " + option)
83 settings.TabSize = tsize
84 } else if option == "colorscheme" {
85 settings.Colorscheme = value
87 view.buf.UpdateRules()
88 } else if option == "syntax" {
90 settings.Syntax = true
91 } else if value == "off" {
92 settings.Syntax = false
94 messenger.Error("Invalid value for " + option)
98 view.buf.UpdateRules()
99 } else if option == "tabsToSpaces" {
101 settings.TabsToSpaces = true
102 } else if value == "off" {
103 settings.TabsToSpaces = false
105 messenger.Error("Invalid value for " + option)
108 } else if option == "autoindent" {
110 settings.AutoIndent = true
111 } else if value == "off" {
112 settings.AutoIndent = false
114 messenger.Error("Invalid value for " + option)
117 } else if option == "ruler" {
119 settings.Ruler = true
120 } else if value == "off" {
121 settings.Ruler = false
123 messenger.Error("Invalid value for " + option)
128 err := WriteSettings(filename)
130 messenger.Error("Error writing to settings.json: " + err.Error())
134 messenger.Error("Option " + option + " does not exist")
137 messenger.Error("Invalid option, please use option value")