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())
52 // WriteSettings writes the settings to the specified filename as JSON
53 func WriteSettings(filename string) error {
55 home, err := homedir.Dir()
59 if _, e := os.Stat(home + "/.micro"); e == nil {
60 txt, _ := json.MarshalIndent(settings, "", " ")
61 err = ioutil.WriteFile(filename, txt, 0644)
66 // DefaultSettings returns the default settings for micro
67 func DefaultSettings() Settings {
69 Colorscheme: "default",
76 // SetOption prompts the user to set an option and checks that the response is valid
77 func SetOption(view *View, args []string) {
78 home, err := homedir.Dir()
80 messenger.Error("Error finding your home directory\nCan't load settings file")
83 filename := home + "/.micro/settings.json"
85 option := strings.TrimSpace(args[0])
86 value := strings.TrimSpace(args[1])
88 if Contains(possibleSettings, option) {
89 if option == "tabsize" {
90 tsize, err := strconv.Atoi(value)
92 messenger.Error("Invalid value for " + option)
95 settings.TabSize = tsize
96 } else if option == "colorscheme" {
97 settings.Colorscheme = value
99 view.buf.UpdateRules()
100 } else if option == "syntax" {
102 settings.Syntax = true
103 } else if value == "off" {
104 settings.Syntax = false
106 messenger.Error("Invalid value for " + option)
110 view.buf.UpdateRules()
112 err := WriteSettings(filename)
114 messenger.Error("Error writing to settings.json: " + err.Error())
118 messenger.Error("Option " + option + " does not exist")
121 messenger.Error("Invalid option, please use option value")