]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
Merge
[micro.git] / cmd / micro / settings.go
1 package main
2
3 import (
4         "encoding/json"
5         "io/ioutil"
6         "os"
7         "strconv"
8         "strings"
9 )
10
11 // The options that the user can set
12 var settings Settings
13
14 // All the possible settings
15 var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax", "tabsToSpaces", "ruler"}
16
17 // The Settings struct contains the settings for micro
18 type Settings struct {
19         Colorscheme  string `json:"colorscheme"`
20         TabSize      int    `json:"tabsize"`
21         AutoIndent   bool   `json:"autoindent"`
22         Syntax       bool   `json:"syntax"`
23         TabsToSpaces bool   `json:"tabsToSpaces"`
24         Ruler        bool   `json:"ruler"`
25 }
26
27 // InitSettings initializes the options map and sets all options to their default values
28 func InitSettings() {
29         filename := configDir + "/settings.json"
30         if _, e := os.Stat(filename); e == nil {
31                 input, err := ioutil.ReadFile(filename)
32                 if err != nil {
33                         TermMessage("Error reading settings.json file: " + err.Error())
34                         return
35                 }
36
37                 json.Unmarshal(input, &settings)
38         } else {
39                 settings = DefaultSettings()
40                 err := WriteSettings(filename)
41                 if err != nil {
42                         TermMessage("Error writing settings.json file: " + err.Error())
43                 }
44         }
45 }
46
47 // WriteSettings writes the settings to the specified filename as JSON
48 func WriteSettings(filename string) error {
49         var err error
50         if _, e := os.Stat(configDir); e == nil {
51                 txt, _ := json.MarshalIndent(settings, "", "    ")
52                 err = ioutil.WriteFile(filename, txt, 0644)
53         }
54         return err
55 }
56
57 // DefaultSettings returns the default settings for micro
58 func DefaultSettings() Settings {
59         return Settings{
60                 Colorscheme:  "default",
61                 TabSize:      4,
62                 AutoIndent:   true,
63                 Syntax:       true,
64                 TabsToSpaces: false,
65                 Ruler:        true,
66         }
67 }
68
69 // SetOption prompts the user to set an option and checks that the response is valid
70 func SetOption(view *View, args []string) {
71         filename := configDir + "/settings.json"
72         if len(args) == 2 {
73                 option := strings.TrimSpace(args[0])
74                 value := strings.TrimSpace(args[1])
75
76                 if Contains(possibleSettings, option) {
77                         if option == "tabsize" {
78                                 tsize, err := strconv.Atoi(value)
79                                 if err != nil {
80                                         messenger.Error("Invalid value for " + option)
81                                         return
82                                 }
83                                 settings.TabSize = tsize
84                         } else if option == "colorscheme" {
85                                 settings.Colorscheme = value
86                                 LoadSyntaxFiles()
87                                 view.buf.UpdateRules()
88                         } else if option == "syntax" {
89                                 if value == "on" {
90                                         settings.Syntax = true
91                                 } else if value == "off" {
92                                         settings.Syntax = false
93                                 } else {
94                                         messenger.Error("Invalid value for " + option)
95                                         return
96                                 }
97                                 LoadSyntaxFiles()
98                                 view.buf.UpdateRules()
99                         } else if option == "tabsToSpaces" {
100                                 if value == "on" {
101                                         settings.TabsToSpaces = true
102                                 } else if value == "off" {
103                                         settings.TabsToSpaces = false
104                                 } else {
105                                         messenger.Error("Invalid value for " + option)
106                                         return
107                                 }
108                         } else if option == "autoindent" {
109                                 if value == "on" {
110                                         settings.AutoIndent = true
111                                 } else if value == "off" {
112                                         settings.AutoIndent = false
113                                 } else {
114                                         messenger.Error("Invalid value for " + option)
115                                         return
116                                 }
117                         } else if option == "ruler" {
118                                 if value == "on" {
119                                         settings.Ruler = true
120                                 } else if value == "off" {
121                                         settings.Ruler = false
122                                 } else {
123                                         messenger.Error("Invalid value for " + option)
124                                         return
125                                 }
126                         }
127
128                         err := WriteSettings(filename)
129                         if err != nil {
130                                 messenger.Error("Error writing to settings.json: " + err.Error())
131                                 return
132                         }
133                 } else {
134                         messenger.Error("Option " + option + " does not exist")
135                 }
136         } else {
137                 messenger.Error("Invalid option, please use option value")
138         }
139 }