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