11 // The options that the user can set
14 // All the possible settings
15 var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax", "tabsToSpaces"}
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"`
26 // InitSettings initializes the options map and sets all options to their default values
28 filename := configDir + "/settings.json"
29 if _, e := os.Stat(filename); e == nil {
30 input, err := ioutil.ReadFile(filename)
32 TermMessage("Error reading settings.json file: " + err.Error())
36 json.Unmarshal(input, &settings)
38 settings = DefaultSettings()
39 err := WriteSettings(filename)
41 TermMessage("Error writing settings.json file: " + err.Error())
46 // WriteSettings writes the settings to the specified filename as JSON
47 func WriteSettings(filename string) error {
49 if _, e := os.Stat(configDir); e == nil {
50 txt, _ := json.MarshalIndent(settings, "", " ")
51 err = ioutil.WriteFile(filename, txt, 0644)
56 // DefaultSettings returns the default settings for micro
57 func DefaultSettings() Settings {
59 Colorscheme: "default",
67 // SetOption prompts the user to set an option and checks that the response is valid
68 func SetOption(view *View, args []string) {
69 filename := configDir + "/settings.json"
71 option := strings.TrimSpace(args[0])
72 value := strings.TrimSpace(args[1])
74 if Contains(possibleSettings, option) {
75 if option == "tabsize" {
76 tsize, err := strconv.Atoi(value)
78 messenger.Error("Invalid value for " + option)
81 settings.TabSize = tsize
82 } else if option == "colorscheme" {
83 settings.Colorscheme = value
85 view.buf.UpdateRules()
86 } else if option == "syntax" {
88 settings.Syntax = true
89 } else if value == "off" {
90 settings.Syntax = false
92 messenger.Error("Invalid value for " + option)
96 view.buf.UpdateRules()
97 } else if option == "tabsToSpaces" {
99 settings.TabsToSpaces = true
100 } else if value == "off" {
101 settings.TabsToSpaces = false
103 messenger.Error("Invalid value for " + option)
107 err := WriteSettings(filename)
109 messenger.Error("Error writing to settings.json: " + err.Error())
113 messenger.Error("Option " + option + " does not exist")
116 messenger.Error("Invalid option, please use option value")