-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
347 lines (297 loc) · 7.48 KB
/
config.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2015 Lukas Weber. All rights reserved.
// Use of this source code is governed by the MIT-styled
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"unicode/utf8"
termbox "github.com/nsf/termbox-go"
"gopkg.in/gcfg.v1"
)
var specialKeys = map[string]termbox.Key{
"up": termbox.KeyArrowUp,
"down": termbox.KeyArrowDown,
"left": termbox.KeyArrowLeft,
"right": termbox.KeyArrowRight,
"pageup": termbox.KeyPgup,
"pagedown": termbox.KeyPgdn,
"enter": termbox.KeyEnter,
}
// KeyBinding represents a single keybinding.
type KeyBinding struct {
Ch rune
Key termbox.Key // for nonprintables
KeyName string
Command string
Args []string
}
// UnmarshalText implements the encoding.TextUnmarshaller interface.
func (k *KeyBinding) UnmarshalText(text []byte) error {
str := string(text)
fields := strings.Fields(str)
if len(fields) < 2 {
return fmt.Errorf("Expected syntax 'key command'")
}
if utf8.RuneCountInString(fields[0]) == 1 {
k.Ch, _ = utf8.DecodeRuneInString(fields[0])
} else {
ok := false
k.Key, ok = specialKeys[fields[0]]
if !ok {
return fmt.Errorf("Unsupported key '%s'", fields[0])
}
}
k.KeyName = fields[0]
k.Command = fields[1]
k.Args = fields[2:]
return nil
}
// KeyBindings represent a set of keybindings.
type KeyBindings struct {
Key []*KeyBinding
}
// Account represents a mail account set up to send mail.
type Account struct {
Addr string
Sendmail_Command string
Sent_Tag []string
Sent_Dir string
Draft_Dir string
}
// TagAlias represents an alias for tags.
type TagAlias struct {
tag string
alias string
color int
}
// UnmarshalText implements the encoding.TextUnmarshaller interface.
func (t *TagAlias) UnmarshalText(text []byte) error {
str := string(text)
fields := strings.Fields(str)
if len(fields) > 3 || len(fields) == 0 {
return errors.New("Tag aliases must be of form 'tag [alias [color]]'.")
}
t.tag = fields[0]
if len(fields) >= 2 {
t.alias = fields[1]
}
t.color = -1
if len(fields) == 3 {
var err error
t.color, err = strconv.Atoi(fields[2])
if err != nil {
return errors.New("Tag aliases must be of form 'tag [alias [color]]'. Color must be an integer.")
}
}
return nil
}
// Config holds all configuration values.
// Refer to gcfg documentation for the resulting config file syntax.
type Config struct {
General struct {
Database string
Initial_Command string
Synchronize_Flags bool
}
Bindings map[string]*KeyBindings
Theme struct {
BottomBar int
Date int
Subject int
From int
Tags int
Error int
HlBg int
HlFg int
Quote int
}
Commands struct {
Attachments string
Editor string
HtmlDump string
}
Account map[string]*Account
Tags struct {
Alias []*TagAlias
}
}
// PostConfig contains post processed config fields, e.g. values
// stored in maps for faster access
type PostConfig struct {
TagAliases map[string]string
TagColors map[string]int
}
const (
configPath = "$HOME/.config/barely/config"
)
var config Config
var pconfig PostConfig
// default configuration
const DefaultCfg = `# This is the default configuration file for barely.
# barely looks for it in '~/.config/barely/config'
#
# Omitted options will default to the settings they have here.
# For syntax, see http://git-scm.com/docs/git-config#_syntax
[general]
# Location of the notmuch database
database=~/mail
# First command to be executed on start. This should open a
# new buffer. If it doesn't, a search buffer for "" is opened.
initial-command=msearch tag:unread
# Whether barely should add matching maildir tags after changing
# message tags.
synchronize-flags=true
# For every address you want to send mail with, there has to be an
# account section like this one. the addr, sendmail-command and
# sent-dir are mandatory for sending.
# draft-dir is mandatory for saving drafts of course.
#
# [account "example"]
# addr = [email protected]
# sendmail-command = msmtp --account=example -t
# sent-dir = $HOME/mail/example/sent
# draft-dir = $HOME/mail/example/draft
# sent-tag = sent
# sent-tag = example
[commands]
# program used to open all tpyes of attachments
attachments=xdg-open
# editor program
editor=vim
# html to plaintext converter
htmldump=w3m -dump
# This section describes the color theme. Colors are numbers
# in the terminal 256 color cube.
[theme]
bottombar = 241
date = 103
subject = 110
from = 115
tags = 244
error = 88
hlbg = 240
hlfg = 147
quote = 80
# The bindings sections contain keybinding definitions of the
# form
# key = KEY COMMAND ARGS...
#
# Valid commands differ from buffer to buffer.
[bindings]
key = q quit
key = d close
key = / prompt search
key = : prompt
key = ? help
key = @ refresh
[bindings "search"]
key = up move up
key = down move down
key = pageup move pageup
key = pagedown move pagedown
key = enter show
key = s untag unread
key = & tag deleted
[bindings "mail"]
key = up move up
key = down move down
key = pageup move pageup
key = pagedown move pagedown
key = enter show
key = r reply
key = R groupreply
key = / prompt search
key = | prompt search
key = n search
key = N rsearch
[bindings "compose"]
key = up move up
key = down move down
key = pageup move pageup
key = pagedown move pagedown
key = enter edit
key = y send
key = a prompt attach
key = A deattach
# The tags section can be used to set display aliases for tags.
# This can be used to hide or abbreviate common tags and to color important
# tags to highlight unread mail for example.
#
# [tags]
# alias = replied >
# alias = attachment @
# alias = sent # empty alias means hiding tag
# alias = unread unread 87 # highlight the unread tag in color 87
`
func preparePostConfig(pcfg *PostConfig, cfg *Config) {
pcfg.TagAliases = make(map[string]string)
pcfg.TagColors = make(map[string]int)
for _, a := range cfg.Tags.Alias {
pcfg.TagAliases[a.tag] = a.alias
if a.color >= 0 {
pcfg.TagColors[a.tag] = a.color
}
}
}
// removeDoubleBindings removes double KeyBindings in the config giving the last defined binding
// priority.
func removeDoubleBindings(cfg *Config) {
for name, binds := range cfg.Bindings {
newKey := make([]*KeyBinding, 0, len(binds.Key))
written := make(map[string]int)
for _, k := range binds.Key {
if idx, ok := written[k.KeyName]; ok {
newKey[idx] = k
} else {
written[k.KeyName] = len(newKey)
newKey = append(newKey, k)
}
}
cfg.Bindings[name].Key = newKey
}
}
// LoadConfig loads the configuration from the standard configuration file path and sets the
// global config struct.
func LoadConfig() {
err := gcfg.ReadStringInto(&config, DefaultCfg)
if err != nil {
panic(err)
}
path := os.ExpandEnv(configPath)
err = gcfg.ReadFileInto(&config, path)
if err != nil {
fmt.Println(err)
}
removeDoubleBindings(&config)
preparePostConfig(&pconfig, &config)
}
// getBinding returns a key binding fitting a pressed key (Ch, Key) for a specific section.
// If no such binding exists, it returns nil.
//
// Global bindings are associated to the section "".
func getBinding(section string, Ch rune, Key termbox.Key) *KeyBinding {
sec := config.Bindings[section]
if sec == nil {
return nil
}
keys := sec.Key
for i := range keys {
if (Ch != 0 && Ch == keys[i].Ch) || (Ch == 0 && Key == keys[i].Key) {
return keys[i]
}
}
return nil
}
// getAccount fetches an account for a given mail address.
func getAccount(addr string) *Account {
for _, val := range config.Account {
if val.Addr == addr {
return val
}
}
return nil
}