]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
d260f26b8353fdaff1725d48a42d3cc8556a08ef
[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 Settings
14
15 // All the possible settings
16 // This map maps the name of the setting in the Settings struct
17 // to the name that the user will actually use (the one in the json file)
18 var possibleSettings = map[string]string{
19         "colorscheme":  "Colorscheme",
20         "tabsize":      "TabSize",
21         "autoindent":   "AutoIndent",
22         "syntax":       "Syntax",
23         "tabsToSpaces": "TabsToSpaces",
24         "ruler":        "Ruler",
25         "gofmt":        "GoFmt",
26         "goimports":    "GoImports",
27 }
28
29 // The Settings struct contains the settings for micro
30 type Settings struct {
31         Colorscheme  string `json:"colorscheme"`
32         TabSize      int    `json:"tabsize"`
33         AutoIndent   bool   `json:"autoindent"`
34         Syntax       bool   `json:"syntax"`
35         TabsToSpaces bool   `json:"tabsToSpaces"`
36         Ruler        bool   `json:"ruler"`
37         GoFmt        bool   `json:"gofmt"`
38         GoImports    bool   `json:"goimports"`
39 }
40
41 // InitSettings initializes the options map and sets all options to their default values
42 func InitSettings() {
43         filename := configDir + "/settings.json"
44         if _, e := os.Stat(filename); e == nil {
45                 input, err := ioutil.ReadFile(filename)
46                 if err != nil {
47                         TermMessage("Error reading settings.json file: " + err.Error())
48                         return
49                 }
50
51                 err = json.Unmarshal(input, &settings)
52                 if err != nil {
53                         TermMessage("Error reading settings.json:", err.Error())
54                 }
55         } else {
56                 settings = DefaultSettings()
57                 err := WriteSettings(filename)
58                 if err != nil {
59                         TermMessage("Error writing settings.json file: " + err.Error())
60                 }
61         }
62 }
63
64 // WriteSettings writes the settings to the specified filename as JSON
65 func WriteSettings(filename string) error {
66         var err error
67         if _, e := os.Stat(configDir); e == nil {
68                 txt, _ := json.MarshalIndent(settings, "", "    ")
69                 err = ioutil.WriteFile(filename, txt, 0644)
70         }
71         return err
72 }
73
74 // DefaultSettings returns the default settings for micro
75 func DefaultSettings() Settings {
76         return Settings{
77                 Colorscheme:  "default",
78                 TabSize:      4,
79                 AutoIndent:   true,
80                 Syntax:       true,
81                 TabsToSpaces: false,
82                 Ruler:        true,
83                 GoFmt:        false,
84                 GoImports:    false,
85         }
86 }
87
88 // SetOption prompts the user to set an option and checks that the response is valid
89 func SetOption(view *View, args []string) {
90         filename := configDir + "/settings.json"
91         if len(args) == 2 {
92                 option := strings.TrimSpace(args[0])
93                 value := strings.TrimSpace(args[1])
94
95                 mutable := reflect.ValueOf(&settings).Elem()
96                 field := mutable.FieldByName(possibleSettings[option])
97                 if !field.IsValid() {
98                         messenger.Error(option + " is not a valid option")
99                         return
100                 }
101                 kind := field.Type().Kind()
102                 if kind == reflect.Bool {
103                         b, err := ParseBool(value)
104                         if err != nil {
105                                 messenger.Error("Invalid value for " + option)
106                                 return
107                         }
108                         field.SetBool(b)
109                 } else if kind == reflect.String {
110                         field.SetString(value)
111                 } else if kind == reflect.Int {
112                         i, err := strconv.Atoi(value)
113                         if err != nil {
114                                 messenger.Error("Invalid value for " + option)
115                                 return
116                         }
117                         field.SetInt(int64(i))
118                 }
119
120                 if option == "colorscheme" {
121                         LoadSyntaxFiles()
122                         view.buf.UpdateRules()
123                 }
124
125                 err := WriteSettings(filename)
126                 if err != nil {
127                         messenger.Error("Error writing to settings.json: " + err.Error())
128                         return
129                 }
130         } else {
131                 messenger.Error("No value given")
132         }
133 }