]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
Added StartOfLine and EndOfLine actions
[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", "gofmt", "goimports"}
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         GoFmt        bool   `json:"gofmt"`
26         GoImports    bool   `json:"goimports"`
27 }
28
29 // InitSettings initializes the options map and sets all options to their default values
30 func InitSettings() {
31         filename := configDir + "/settings.json"
32         if _, e := os.Stat(filename); e == nil {
33                 input, err := ioutil.ReadFile(filename)
34                 if err != nil {
35                         TermMessage("Error reading settings.json file: " + err.Error())
36                         return
37                 }
38
39                 err = json.Unmarshal(input, &settings)
40                 if err != nil {
41                         TermMessage("Error reading settings.json:", err.Error())
42                 }
43         } else {
44                 settings = DefaultSettings()
45                 err := WriteSettings(filename)
46                 if err != nil {
47                         TermMessage("Error writing settings.json file: " + err.Error())
48                 }
49         }
50 }
51
52 // WriteSettings writes the settings to the specified filename as JSON
53 func WriteSettings(filename string) error {
54         var err error
55         if _, e := os.Stat(configDir); e == nil {
56                 txt, _ := json.MarshalIndent(settings, "", "    ")
57                 err = ioutil.WriteFile(filename, txt, 0644)
58         }
59         return err
60 }
61
62 // DefaultSettings returns the default settings for micro
63 func DefaultSettings() Settings {
64         return Settings{
65                 Colorscheme:  "default",
66                 TabSize:      4,
67                 AutoIndent:   true,
68                 Syntax:       true,
69                 TabsToSpaces: false,
70                 Ruler:        true,
71                 GoFmt:        false,
72                 GoImports:    false,
73         }
74 }
75
76 // SetOption prompts the user to set an option and checks that the response is valid
77 func SetOption(view *View, args []string) {
78         filename := configDir + "/settings.json"
79         if len(args) == 2 {
80                 option := strings.TrimSpace(args[0])
81                 value := strings.TrimSpace(args[1])
82
83                 if Contains(possibleSettings, option) {
84                         if option == "tabsize" {
85                                 tsize, err := strconv.Atoi(value)
86                                 if err != nil {
87                                         messenger.Error("Invalid value for " + option)
88                                         return
89                                 }
90                                 settings.TabSize = tsize
91                         } else if option == "colorscheme" {
92                                 settings.Colorscheme = value
93                                 LoadSyntaxFiles()
94                                 view.buf.UpdateRules()
95                         } else if option == "syntax" {
96                                 if value == "on" {
97                                         settings.Syntax = true
98                                 } else if value == "off" {
99                                         settings.Syntax = false
100                                 } else {
101                                         messenger.Error("Invalid value for " + option)
102                                         return
103                                 }
104                                 LoadSyntaxFiles()
105                                 view.buf.UpdateRules()
106                         } else if option == "tabsToSpaces" {
107                                 if value == "on" {
108                                         settings.TabsToSpaces = true
109                                 } else if value == "off" {
110                                         settings.TabsToSpaces = false
111                                 } else {
112                                         messenger.Error("Invalid value for " + option)
113                                         return
114                                 }
115                         } else if option == "autoindent" {
116                                 if value == "on" {
117                                         settings.AutoIndent = true
118                                 } else if value == "off" {
119                                         settings.AutoIndent = false
120                                 } else {
121                                         messenger.Error("Invalid value for " + option)
122                                         return
123                                 }
124                         } else if option == "ruler" {
125                                 if value == "on" {
126                                         settings.Ruler = true
127                                 } else if value == "off" {
128                                         settings.Ruler = false
129                                 } else {
130                                         messenger.Error("Invalid value for " + option)
131                                         return
132                                 }
133                         } else if option == "gofmt" {
134                                 if value == "on" {
135                                         settings.GoFmt = true
136                                 } else if value == "off" {
137                                         settings.GoFmt = false
138                                 } else {
139                                         messenger.Error("Invalid value for " + option)
140                                         return
141                                 }
142                         } else if option == "goimports" {
143                                 if value == "on" {
144                                         settings.GoFmt = false // goimports does gofmt
145                                         settings.GoImports = true
146                                 } else if value == "off" {
147                                         settings.GoFmt = false
148                                 } else {
149                                         messenger.Error("Invalid value for " + option)
150                                         return
151                                 }
152                         }
153
154                         err := WriteSettings(filename)
155                         if err != nil {
156                                 messenger.Error("Error writing to settings.json: " + err.Error())
157                                 return
158                         }
159                 } else {
160                         messenger.Error("Option " + option + " does not exist")
161                 }
162         } else {
163                 messenger.Error("Invalid option, please use option value")
164         }
165 }