]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
Use a map for settings instead of a struct
[micro.git] / cmd / micro / settings.go
1 package main
2
3 import (
4         "encoding/json"
5         "io/ioutil"
6         "os"
7         "reflect"
8         "strconv"
9         "strings"
10 )
11
12 // The options that the user can set
13 var settings map[string]interface{}
14
15 // InitSettings initializes the options map and sets all options to their default values
16 func InitSettings() {
17         settings = make(map[string]interface{})
18         filename := configDir + "/settings.json"
19         if _, e := os.Stat(filename); e == nil {
20                 input, err := ioutil.ReadFile(filename)
21                 if err != nil {
22                         TermMessage("Error reading settings.json file: " + err.Error())
23                         return
24                 }
25
26                 err = json.Unmarshal(input, &settings)
27                 if err != nil {
28                         TermMessage("Error reading settings.json:", err.Error())
29                 }
30         } else {
31                 settings = DefaultSettings()
32                 err := WriteSettings(filename)
33                 if err != nil {
34                         TermMessage("Error writing settings.json file: " + err.Error())
35                 }
36         }
37 }
38
39 // WriteSettings writes the settings to the specified filename as JSON
40 func WriteSettings(filename string) error {
41         var err error
42         if _, e := os.Stat(configDir); e == nil {
43                 txt, _ := json.MarshalIndent(settings, "", "    ")
44                 err = ioutil.WriteFile(filename, txt, 0644)
45         }
46         return err
47 }
48
49 // DefaultSettings returns the default settings for micro
50 func DefaultSettings() map[string]interface{} {
51         return map[string]interface{}{
52                 "colorscheme":  "default",
53                 "tabsize":      4,
54                 "autoindent":   true,
55                 "syntax":       true,
56                 "tabsToSpaces": false,
57                 "ruler":        true,
58                 "gofmt":        false,
59                 "goimports":    false,
60         }
61 }
62
63 // SetOption prompts the user to set an option and checks that the response is valid
64 func SetOption(view *View, args []string) {
65         filename := configDir + "/settings.json"
66         if len(args) == 2 {
67                 option := strings.TrimSpace(args[0])
68                 value := strings.TrimSpace(args[1])
69
70                 if _, ok := settings[option]; !ok {
71                         messenger.Error(option + " is not a valid option")
72                         return
73                 }
74
75                 kind := reflect.TypeOf(settings[option]).Kind()
76                 if kind == reflect.Bool {
77                         b, err := ParseBool(value)
78                         if err != nil {
79                                 messenger.Error("Invalid value for " + option)
80                                 return
81                         }
82                         settings[option] = b
83                 } else if kind == reflect.String {
84                         settings[option] = value
85                 } else if kind == reflect.Float64 {
86                         i, err := strconv.Atoi(value)
87                         if err != nil {
88                                 messenger.Error("Invalid value for " + option)
89                                 return
90                         }
91                         settings[option] = float64(i)
92                 }
93
94                 if option == "colorscheme" {
95                         LoadSyntaxFiles()
96                         view.buf.UpdateRules()
97                 }
98
99                 err := WriteSettings(filename)
100                 if err != nil {
101                         messenger.Error("Error writing to settings.json: " + err.Error())
102                         return
103                 }
104         } else {
105                 messenger.Error("No value given")
106         }
107 }