5 "github.com/mitchellh/go-homedir"
12 // The options that the user can set
15 // All the possible settings
16 var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax"}
18 // The Settings struct contains the settings for micro
19 type Settings struct {
20 Colorscheme string `json:"colorscheme"`
21 TabSize int `json:"tabsize"`
22 AutoIndent bool `json:"autoindent"`
23 Syntax bool `json:"syntax"`
26 // InitSettings initializes the options map and sets all options to their default values
28 home, err := homedir.Dir()
30 TermMessage("Error finding your home directory\nCan't load settings file")
34 filename := home + "/.micro/settings.json"
35 if _, e := os.Stat(filename); e == nil {
36 input, err := ioutil.ReadFile(filename)
38 TermMessage("Error reading settings.json file: " + err.Error())
42 json.Unmarshal(input, &settings)
44 settings = DefaultSettings()
45 err := WriteSettings(filename)
47 TermMessage("Error writing settings.json file: " + err.Error())
50 os.Mkdir(home+"/.micro", 755)
54 // WriteSettings writes the settings to the specified filename as JSON
55 func WriteSettings(filename string) error {
57 home, err := homedir.Dir()
61 if _, e := os.Stat(home + "/.micro"); e == nil {
62 txt, _ := json.MarshalIndent(settings, "", " ")
63 err = ioutil.WriteFile(filename, txt, 0644)
68 // DefaultSettings returns the default settings for micro
69 func DefaultSettings() Settings {
71 Colorscheme: "default",
78 // SetOption prompts the user to set an option and checks that the response is valid
79 func SetOption(view *View, args []string) {
80 home, err := homedir.Dir()
82 messenger.Error("Error finding your home directory\nCan't load settings file")
85 filename := home + "/.micro/settings.json"
87 option := strings.TrimSpace(args[0])
88 value := strings.TrimSpace(args[1])
90 if Contains(possibleSettings, option) {
91 if option == "tabsize" {
92 tsize, err := strconv.Atoi(value)
94 messenger.Error("Invalid value for " + option)
97 settings.TabSize = tsize
98 } else if option == "colorscheme" {
99 settings.Colorscheme = value
101 view.buf.UpdateRules()
102 } else if option == "syntax" {
104 settings.Syntax = true
105 } else if value == "off" {
106 settings.Syntax = false
108 messenger.Error("Invalid value for " + option)
112 view.buf.UpdateRules()
114 err := WriteSettings(filename)
116 messenger.Error("Error writing to settings.json: " + err.Error())
120 messenger.Error("Option " + option + " does not exist")
123 messenger.Error("Invalid option, please use option value")