-
Notifications
You must be signed in to change notification settings - Fork 18
/
settings.go
188 lines (157 loc) · 4.07 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"os/user"
"strings"
"sync"
)
func getConfigFolder() (string, error) {
u, err := user.Current()
if err != nil {
return "", err
}
folder := u.HomeDir + "/.config/"
err = os.MkdirAll(folder, 0755)
return folder, err
}
type SettingsBase struct {
Info string
JSONFormatting bool
Timestamp string
LastWebsocketURL string
LastActions []string
PingSeconds int
Pipe struct {
In []string
Out []string
}
}
func (s *SettingsBase) Clone() SettingsBase {
ret := *s
fnCopy := func(dst *[]string, src []string) {
if src == nil {
return
}
*dst = make([]string, len(src))
copy(*dst, src)
}
fnCopy(&ret.LastActions, s.LastActions)
fnCopy(&ret.Pipe.In, s.Pipe.In)
fnCopy(&ret.Pipe.Out, s.Pipe.Out)
return ret
}
// persistent information about the usage of claws
type Settings struct {
SettingsBase
sync.RWMutex `json:"-"`
}
// for goroutine-safe read access to settings
func (s *Settings) Clone() SettingsBase {
s.RLock()
defer s.RUnlock()
return s.SettingsBase.Clone()
}
// loads settings from ~/.config/claws.json
func LoadSettings() (oSet Settings, err error) {
folder, err := getConfigFolder()
if err != nil {
return
}
f, err := os.Open(folder + "claws.json")
if err != nil {
// silently ignore NotExist
if os.IsNotExist(err) {
err = nil
return
}
return
}
defer f.Close()
err = json.NewDecoder(f).Decode(&oSet.SettingsBase)
return
}
// saves settings to ~/.config/claws.json
func (s *Settings) Save() error {
folder, err := getConfigFolder()
if err != nil {
return err
}
f, err := os.Create(folder + "claws.json")
if err != nil {
return err
}
defer f.Close()
s.RLock()
defer s.RUnlock()
s.Info = "Claws configuration file; more information can be found at https://howl.moe/claws"
e := json.NewEncoder(f)
e.SetIndent("", "\t")
return e.Encode(s.SettingsBase)
}
// applies ONLY specified fields of current settings to claws.json
func (s *Settings) Update(fields ...string) error {
// TODO: rewrite settings.go to make everything 100% atomic and better
// structured.
s.RLock()
defer s.RUnlock()
return s.Save()
}
// adds an action to LastActions
func (s *Settings) PushAction(act string) error {
s.Lock()
s.LastActions = append([]string{act}, s.LastActions...)
if len(s.LastActions) > 100 {
s.LastActions = s.LastActions[:100]
}
s.Unlock()
return s.Update("LastActions")
}
// displays CLI `--help` information
// writes specified flags/opts into settings
func (pSet *Settings) ParseFlags() error {
// Help message
flag.Usage = func() {
fmt.Fprint(os.Stderr, cliHelpPrefix)
flag.PrintDefaults()
fmt.Fprint(os.Stderr, cliHelpSuffix)
}
flag.BoolVar(&pSet.JSONFormatting, "j", pSet.JSONFormatting, "Start with JSON formatting enabled.")
flag.StringVar(&pSet.Timestamp, "t", pSet.Timestamp, "Golang date format for timestamps.\nDisabled when blank.")
flag.IntVar(&pSet.PingSeconds, "p", pSet.PingSeconds, "PING interval.\nDisabled when <= 0.")
flag.Parse()
// Use WebSocket URL if given.
sArgs := flag.Args()
for _, wsurl := range sArgs {
wsurl := strings.TrimSpace(wsurl)
if len(wsurl) > 0 {
pSet.LastWebsocketURL = wsurl
return pSet.Update("LastWebsocketURL")
}
}
return nil
}
const cliHelpPrefix = `COMMAND
claws [OPTION...] [WEBSOCKET_URL]
OPTIONS
`
const cliHelpSuffix = `
USAGE
Key Action
--- ---------------------------------------------------------------
Esc Enter command mode. (<Ctrl-[> also works)
c Create a new connection. Prompts for WebSocket URL.
If nothing is passed, previous URL will be used.
h View help/welcome screen with quick commands.
i Go to insert mode. (<Ins> key also works)
j Toggle auto-detection of JSON in server messages and
automatic tab indentation.
p Set ping interval in seconds. Will prompt for an interval.
If nothing is passed, pings will be disabled.
q Close current connection.
R Go into replace/overtype mode.
(can also be done by pressing <Ins> a couple of times)
t Toggle timestamps before messages in console.
`