]> git.lizzy.rs Git - micro.git/blob - src/settings.go
106fe8c40db539e492851802c4b8f2cba92791ba
[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         txt, _ := json.MarshalIndent(settings, "", "    ")
55         err := ioutil.WriteFile(filename, txt, 0644)
56         return err
57 }
58
59 // DefaultSettings returns the default settings for micro
60 func DefaultSettings() Settings {
61         return Settings{
62                 Colorscheme: "default",
63                 TabSize:     4,
64                 AutoIndent:  true,
65                 Syntax:      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         home, err := homedir.Dir()
72         if err != nil {
73                 messenger.Error("Error finding your home directory\nCan't load settings file")
74         }
75
76         filename := home + "/.micro/settings.json"
77         if len(args) == 2 {
78                 option := strings.TrimSpace(args[0])
79                 value := strings.TrimSpace(args[1])
80
81                 if Contains(possibleSettings, option) {
82                         if option == "tabsize" {
83                                 tsize, err := strconv.Atoi(value)
84                                 if err != nil {
85                                         messenger.Error("Invalid value for " + option)
86                                         return
87                                 }
88                                 settings.TabSize = tsize
89                         } else if option == "colorscheme" {
90                                 settings.Colorscheme = value
91                                 LoadSyntaxFiles()
92                                 view.buf.UpdateRules()
93                         } else if option == "syntax" {
94                                 if value == "on" {
95                                         settings.Syntax = true
96                                 } else if value == "off" {
97                                         settings.Syntax = false
98                                 } else {
99                                         messenger.Error("Invalid value for " + option)
100                                         return
101                                 }
102                                 LoadSyntaxFiles()
103                                 view.buf.UpdateRules()
104                         }
105                         err := WriteSettings(filename)
106                         if err != nil {
107                                 messenger.Error("Error writing to settings.json: " + err.Error())
108                                 return
109                         }
110                 } else {
111                         messenger.Error("Option " + option + " does not exist")
112                 }
113         } else {
114                 messenger.Error("Invalid option, please use option value")
115         }
116 }