]> git.lizzy.rs Git - micro.git/blob - cmd/micro/settings.go
Create ~/.micro if it does not exist
[micro.git] / cmd / micro / 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                 os.Mkdir(home+"/.micro", 755)
51         }
52 }
53
54 // WriteSettings writes the settings to the specified filename as JSON
55 func WriteSettings(filename string) error {
56         var err error
57         home, err := homedir.Dir()
58         if err != nil {
59                 return err
60         }
61         if _, e := os.Stat(home + "/.micro"); e == nil {
62                 txt, _ := json.MarshalIndent(settings, "", "    ")
63                 err = ioutil.WriteFile(filename, txt, 0644)
64         }
65         return err
66 }
67
68 // DefaultSettings returns the default settings for micro
69 func DefaultSettings() Settings {
70         return Settings{
71                 Colorscheme: "default",
72                 TabSize:     4,
73                 AutoIndent:  true,
74                 Syntax:      true,
75         }
76 }
77
78 // SetOption prompts the user to set an option and checks that the response is valid
79 func SetOption(view *View, args []string) {
80         home, err := homedir.Dir()
81         if err != nil {
82                 messenger.Error("Error finding your home directory\nCan't load settings file")
83         }
84
85         filename := home + "/.micro/settings.json"
86         if len(args) == 2 {
87                 option := strings.TrimSpace(args[0])
88                 value := strings.TrimSpace(args[1])
89
90                 if Contains(possibleSettings, option) {
91                         if option == "tabsize" {
92                                 tsize, err := strconv.Atoi(value)
93                                 if err != nil {
94                                         messenger.Error("Invalid value for " + option)
95                                         return
96                                 }
97                                 settings.TabSize = tsize
98                         } else if option == "colorscheme" {
99                                 settings.Colorscheme = value
100                                 LoadSyntaxFiles()
101                                 view.buf.UpdateRules()
102                         } else if option == "syntax" {
103                                 if value == "on" {
104                                         settings.Syntax = true
105                                 } else if value == "off" {
106                                         settings.Syntax = false
107                                 } else {
108                                         messenger.Error("Invalid value for " + option)
109                                         return
110                                 }
111                                 LoadSyntaxFiles()
112                                 view.buf.UpdateRules()
113                         }
114                         err := WriteSettings(filename)
115                         if err != nil {
116                                 messenger.Error("Error writing to settings.json: " + err.Error())
117                                 return
118                         }
119                 } else {
120                         messenger.Error("Option " + option + " does not exist")
121                 }
122         } else {
123                 messenger.Error("Invalid option, please use option value")
124         }
125 }