]> git.lizzy.rs Git - micro.git/blob - src/settings.go
Only use settings.json if ~/.micro exists
[micro.git] / src / settings.go
1 package main
2
3 import (
4         "encoding/json"
5         "github.com/mitchellh/go-homedir"
6         "io/ioutil"
7         "os"
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 var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax"}
17
18 // The Settings struct contains the settings for micro
19 type Settings struct {
20         Colorscheme string `json:"colorscheme"`
21         TabSize     int    `json:"tabsize"`
22         AutoIndent  bool   `json:"autoindent"`
23         Syntax      bool   `json:"syntax"`
24 }
25
26 // InitSettings initializes the options map and sets all options to their default values
27 func InitSettings() {
28         home, err := homedir.Dir()
29         if err != nil {
30                 TermMessage("Error finding your home directory\nCan't load settings file")
31                 return
32         }
33
34         filename := home + "/.micro/settings.json"
35         if _, e := os.Stat(filename); e == nil {
36                 input, err := ioutil.ReadFile(filename)
37                 if err != nil {
38                         TermMessage("Error reading settings.json file: " + err.Error())
39                         return
40                 }
41
42                 json.Unmarshal(input, &settings)
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         home, err := homedir.Dir()
56         if err != nil {
57                 return err
58         }
59         if _, e := os.Stat(home + "/.micro"); e == nil {
60                 txt, _ := json.MarshalIndent(settings, "", "    ")
61                 err = ioutil.WriteFile(filename, txt, 0644)
62         }
63         return err
64 }
65
66 // DefaultSettings returns the default settings for micro
67 func DefaultSettings() Settings {
68         return Settings{
69                 Colorscheme: "default",
70                 TabSize:     4,
71                 AutoIndent:  true,
72                 Syntax:      true,
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         home, err := homedir.Dir()
79         if err != nil {
80                 messenger.Error("Error finding your home directory\nCan't load settings file")
81         }
82
83         filename := home + "/.micro/settings.json"
84         if len(args) == 2 {
85                 option := strings.TrimSpace(args[0])
86                 value := strings.TrimSpace(args[1])
87
88                 if Contains(possibleSettings, option) {
89                         if option == "tabsize" {
90                                 tsize, err := strconv.Atoi(value)
91                                 if err != nil {
92                                         messenger.Error("Invalid value for " + option)
93                                         return
94                                 }
95                                 settings.TabSize = tsize
96                         } else if option == "colorscheme" {
97                                 settings.Colorscheme = value
98                                 LoadSyntaxFiles()
99                                 view.buf.UpdateRules()
100                         } else if option == "syntax" {
101                                 if value == "on" {
102                                         settings.Syntax = true
103                                 } else if value == "off" {
104                                         settings.Syntax = false
105                                 } else {
106                                         messenger.Error("Invalid value for " + option)
107                                         return
108                                 }
109                                 LoadSyntaxFiles()
110                                 view.buf.UpdateRules()
111                         }
112                         err := WriteSettings(filename)
113                         if err != nil {
114                                 messenger.Error("Error writing to settings.json: " + err.Error())
115                                 return
116                         }
117                 } else {
118                         messenger.Error("Option " + option + " does not exist")
119                 }
120         } else {
121                 messenger.Error("Invalid option, please use option value")
122         }
123 }