]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
Fix draw ordering
[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         defaults := DefaultSettings()
18         var parsed map[string]interface{}
19
20         filename := configDir + "/settings.json"
21         if _, e := os.Stat(filename); e == nil {
22                 input, err := ioutil.ReadFile(filename)
23                 if err != nil {
24                         TermMessage("Error reading settings.json file: " + err.Error())
25                         return
26                 }
27
28                 err = json.Unmarshal(input, &parsed)
29                 if err != nil {
30                         TermMessage("Error reading settings.json:", err.Error())
31                 }
32         }
33
34         settings = make(map[string]interface{})
35         for k, v := range defaults {
36                 settings[k] = v
37         }
38         for k, v := range parsed {
39                 settings[k] = v
40         }
41
42         err := WriteSettings(filename)
43         if err != nil {
44                 TermMessage("Error writing settings.json file: " + err.Error())
45         }
46 }
47
48 // WriteSettings writes the settings to the specified filename as JSON
49 func WriteSettings(filename string) error {
50         var err error
51         if _, e := os.Stat(configDir); e == nil {
52                 txt, _ := json.MarshalIndent(settings, "", "    ")
53                 err = ioutil.WriteFile(filename, txt, 0644)
54         }
55         return err
56 }
57
58 // AddOption creates a new option. This is meant to be called by plugins to add options.
59 func AddOption(name string, value interface{}) {
60         settings[name] = value
61         err := WriteSettings(configDir + "/settings.json")
62         if err != nil {
63                 TermMessage("Error writing settings.json file: " + err.Error())
64         }
65 }
66
67 // GetOption returns the specified option. This is meant to be called by plugins to add options.
68 func GetOption(name string) interface{} {
69         return settings[name]
70 }
71
72 // DefaultSettings returns the default settings for micro
73 func DefaultSettings() map[string]interface{} {
74         return map[string]interface{}{
75                 "autoindent":   true,
76                 "colorscheme":  "monokai",
77                 "cursorline":   false,
78                 "ignorecase":   false,
79                 "indentchar":   " ",
80                 "ruler":        true,
81                 "savecursor":   false,
82                 "saveundo":     false,
83                 "scrollspeed":  float64(2),
84                 "scrollmargin": float64(3),
85                 "statusline":   true,
86                 "syntax":       true,
87                 "tabsize":      float64(4),
88                 "tabstospaces": false,
89         }
90 }
91
92 // SetOption prompts the user to set an option and checks that the response is valid
93 func SetOption(view *View, args []string) {
94         filename := configDir + "/settings.json"
95         if len(args) == 2 {
96                 option := strings.TrimSpace(args[0])
97                 value := strings.TrimSpace(args[1])
98
99                 if _, ok := settings[option]; !ok {
100                         messenger.Error(option + " is not a valid option")
101                         return
102                 }
103
104                 kind := reflect.TypeOf(settings[option]).Kind()
105                 if kind == reflect.Bool {
106                         b, err := ParseBool(value)
107                         if err != nil {
108                                 messenger.Error("Invalid value for " + option)
109                                 return
110                         }
111                         settings[option] = b
112                 } else if kind == reflect.String {
113                         settings[option] = value
114                 } else if kind == reflect.Float64 {
115                         i, err := strconv.Atoi(value)
116                         if err != nil {
117                                 messenger.Error("Invalid value for " + option)
118                                 return
119                         }
120                         settings[option] = float64(i)
121                 }
122
123                 if option == "colorscheme" {
124                         LoadSyntaxFiles()
125                         view.Buf.UpdateRules()
126                 }
127
128                 if option == "statusline" {
129                         view.Resize(screen.Size())
130                 }
131
132                 err := WriteSettings(filename)
133                 if err != nil {
134                         messenger.Error("Error writing to settings.json: " + err.Error())
135                         return
136                 }
137         } else {
138                 messenger.Error("No value given")
139         }
140 }