]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
Add tabsToSpaces option
[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"}
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 }
25
26 // InitSettings initializes the options map and sets all options to their default values
27 func InitSettings() {
28         filename := configDir + "/settings.json"
29         if _, e := os.Stat(filename); e == nil {
30                 input, err := ioutil.ReadFile(filename)
31                 if err != nil {
32                         TermMessage("Error reading settings.json file: " + err.Error())
33                         return
34                 }
35
36                 json.Unmarshal(input, &settings)
37         } else {
38                 settings = DefaultSettings()
39                 err := WriteSettings(filename)
40                 if err != nil {
41                         TermMessage("Error writing settings.json file: " + err.Error())
42                 }
43         }
44 }
45
46 // WriteSettings writes the settings to the specified filename as JSON
47 func WriteSettings(filename string) error {
48         var err error
49         if _, e := os.Stat(configDir); e == nil {
50                 txt, _ := json.MarshalIndent(settings, "", "    ")
51                 err = ioutil.WriteFile(filename, txt, 0644)
52         }
53         return err
54 }
55
56 // DefaultSettings returns the default settings for micro
57 func DefaultSettings() Settings {
58         return Settings{
59                 Colorscheme:  "default",
60                 TabSize:      4,
61                 AutoIndent:   true,
62                 Syntax:       true,
63                 TabsToSpaces: false,
64         }
65 }
66
67 // SetOption prompts the user to set an option and checks that the response is valid
68 func SetOption(view *View, args []string) {
69         filename := configDir + "/settings.json"
70         if len(args) == 2 {
71                 option := strings.TrimSpace(args[0])
72                 value := strings.TrimSpace(args[1])
73
74                 if Contains(possibleSettings, option) {
75                         if option == "tabsize" {
76                                 tsize, err := strconv.Atoi(value)
77                                 if err != nil {
78                                         messenger.Error("Invalid value for " + option)
79                                         return
80                                 }
81                                 settings.TabSize = tsize
82                         } else if option == "colorscheme" {
83                                 settings.Colorscheme = value
84                                 LoadSyntaxFiles()
85                                 view.buf.UpdateRules()
86                         } else if option == "syntax" {
87                                 if value == "on" {
88                                         settings.Syntax = true
89                                 } else if value == "off" {
90                                         settings.Syntax = false
91                                 } else {
92                                         messenger.Error("Invalid value for " + option)
93                                         return
94                                 }
95                                 LoadSyntaxFiles()
96                                 view.buf.UpdateRules()
97                         } else if option == "tabsToSpaces" {
98                                 if value == "on" {
99                                         settings.TabsToSpaces = true
100                                 } else if value == "off" {
101                                         settings.TabsToSpaces = false
102                                 } else {
103                                         messenger.Error("Invalid value for " + option)
104                                         return
105                                 }
106                         }
107                         err := WriteSettings(filename)
108                         if err != nil {
109                                 messenger.Error("Error writing to settings.json: " + err.Error())
110                                 return
111                         }
112                 } else {
113                         messenger.Error("Option " + option + " does not exist")
114                 }
115         } else {
116                 messenger.Error("Invalid option, please use option value")
117         }
118 }