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 {
54 txt, _ := json.MarshalIndent(settings, "", " ")
55 err := ioutil.WriteFile(filename, txt, 0644)
59 // DefaultSettings returns the default settings for micro
60 func DefaultSettings() Settings {
62 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 home, err := homedir.Dir()
73 messenger.Error("Error finding your home directory\nCan't load settings file")
76 filename := home + "/.micro/settings.json"
78 option := strings.TrimSpace(args[0])
79 value := strings.TrimSpace(args[1])
81 if Contains(possibleSettings, option) {
82 if option == "tabsize" {
83 tsize, err := strconv.Atoi(value)
85 messenger.Error("Invalid value for " + option)
88 settings.TabSize = tsize
89 } else if option == "colorscheme" {
90 settings.Colorscheme = value
92 view.buf.UpdateRules()
93 } else if option == "syntax" {
95 settings.Syntax = true
96 } else if value == "off" {
97 settings.Syntax = false
99 messenger.Error("Invalid value for " + option)
103 view.buf.UpdateRules()
105 err := WriteSettings(filename)
107 messenger.Error("Error writing to settings.json: " + err.Error())
111 messenger.Error("Option " + option + " does not exist")
114 messenger.Error("Invalid option, please use option value")