~chrisppy/beagles

ref: v0.2.0 beagles/config/config.go -rw-r--r-- 10.9 KiB
c14a6fceChris Palmer Updates for 0.2.0 release 1 year, 1 month ago
                                                                                
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// This file is part of beagles.
//
// Copyright © 2020 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package config

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"time"

	wlog "github.com/DataDrake/waterlog"
	"github.com/gdamore/tcell/v2"
	toml "github.com/pelletier/go-toml"
)

// Config contains all relevant configuration data for the application
type Config struct {
	AutoUpdateInterval time.Duration
	Browser            *Browser
	Theme              Theme
	Podcast            Podcast
	CMDMode            KeyCMD
	Enter              KeyCMD
	Escape             KeyCMD
	NavLeftArrow       KeyCMD
	NavUpArrow         KeyCMD
	NavDownArrow       KeyCMD
	NavRightArrow      KeyCMD
	NavLeft            KeyCMD
	NavUp              KeyCMD
	NavDown            KeyCMD
	NavRight           KeyCMD
	NavPageUp          KeyCMD
	NavPageDown        KeyCMD
	Play               KeyCMD
	Download           KeyCMD
	OpenURL            KeyCMD
	MarkRead           KeyCMD
	MarkUnread         KeyCMD
	MarkFavorite       KeyCMD
	MarkUnfavorite     KeyCMD
	List               KeyCMD
	Subscription       KeyCMD
	Favorite           KeyCMD
	Help               KeyCMD
}

// Theme config
type Theme struct {
	ErrColor       tcell.Color
	SeparatorColor tcell.Color
	ReadColor      tcell.Color
	CMDBGColor     tcell.Color
	CMDFGColor     tcell.Color
	StatusBGColor  tcell.Color
	StatusFGColor  tcell.Color
	TitleBGColor   tcell.Color
	TitleFGColor   tcell.Color
	ListBGColor    tcell.Color
	ListFGColor    tcell.Color
	TreeBGColor    tcell.Color
	TreeFGColor    tcell.Color
	ContentBGColor tcell.Color
	ContentFGColor tcell.Color
}

// KeyCMD contains everything needs to set the KeyBinding
type KeyCMD struct {
	Text  string
	Rune  rune
	IsKey bool
	Key   tcell.Key
}

type theme struct {
	BackgroundColor string            `toml:"background_color"`
	ForegroundColor string            `toml:"foreground_color"`
	ErrorColor      string            `toml:"error_color"`
	Separator       string            `toml:"separator_color"`
	ReadColor       string            `toml:"read_color"`
	CommandLine     *commandLineTheme `toml:"command_line, omitempty"`
	StatusLine      *statusLineTheme  `toml:"status_line, omitempty"`
	TitleLine       *titleLineTheme   `toml:"title_line, omitempty"`
	List            *listTheme        `toml:"list, omitempty"`
	Content         *contentTheme     `toml:"content, omitempty"`
	Tree            *treeTheme        `toml:"tree, omitempty"`
}

type config struct {
	Theme              theme       `toml:"theme"`
	KeyCommands        keyCommands `toml:"key_commands"`
	Podcast            Podcast     `toml:"podcast"`
	Browser            *Browser    `toml:"browser, omitempty"`
	AutoUpdateInterval string      `toml:"auto_update_interval, omitempty"`
}

// KeyCommands config
type keyCommands struct {
	MarkRead       string `toml:"mark_read"`
	MarkUnread     string `toml:"mark_unread"`
	MarkFavorite   string `toml:"mark_favorite"`
	MarkUnfavorite string `toml:"mark_unfavorite"`
	OpenURL        string `toml:"open_url"`
	Download       string `toml:"download"`
	Play           string `toml:"play"`
	Left           string `toml:"left"`
	Up             string `toml:"up"`
	Down           string `toml:"down"`
	Right          string `toml:"right"`
	PageUp         string `toml:"page_up"`
	PageDown       string `toml:"page_down"`
	List           string `toml:"list"`
	Subscription   string `toml:"subscription"`
	Favorite       string `toml:"favorite"`
}

// Podcast config
type Podcast struct {
	AutoDownload   bool
	ExternalPlayer *ExternalPlayer `toml:"external_player, omitempty"`
}

// ExternalPlayer config
type ExternalPlayer struct {
	Bin  string   `toml:"bin"`
	Args []string `toml:"args"`
}

// Browser config
type Browser struct {
	HTTP *struct {
		Bin  string   `toml:"bin"`
		Args []string `toml:"args"`
	} `toml:"http, omitempty"`
}

type statusLineTheme struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

type titleLineTheme struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

type commandLineTheme struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

type treeTheme struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

type listTheme struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

type contentTheme struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

func createKeyCMD(key string, cmds map[string]bool) KeyCMD {
	if _, ok := cmds[key]; ok {
		wlog.Fatalf("%s is used multiple times in the config", key)
	}
	cmds[key] = false

	keyCMD := KeyCMD{
		Text: key,
		Key:  tcell.KeyRune,
	}

	if len(key) == 1 {
		keyCMD.IsKey = false
		keyCMD.Rune = []rune(key)[0]
	} else {
		keyCMD.IsKey = true
		for k, v := range tcell.KeyNames {
			if v == key {
				keyCMD.Key = k
				break
			}
		}
	}

	return keyCMD
}

// Load will load the config into the application, if it does not exist it will
// create a default one.  If an error occurs reading the config, it will use
// the default one.
func Load(configDir string) *Config {
	cfg := load(configDir)

	cmds := make(map[string]bool)

	theme := Theme{
		ErrColor:       tcell.GetColor(cfg.Theme.ErrorColor),
		SeparatorColor: tcell.GetColor(cfg.Theme.Separator),
		ReadColor:      tcell.GetColor(cfg.Theme.ReadColor),
	}

	c := &Config{
		Podcast:        cfg.Podcast,
		Browser:        cfg.Browser,
		Theme:          theme,
		CMDMode:        createKeyCMD(":", cmds),
		Enter:          createKeyCMD("Enter", cmds),
		Escape:         createKeyCMD("Esc", cmds),
		NavLeftArrow:   createKeyCMD("Left", cmds),
		NavUpArrow:     createKeyCMD("Up", cmds),
		NavDownArrow:   createKeyCMD("Down", cmds),
		NavRightArrow:  createKeyCMD("Right", cmds),
		Help:           createKeyCMD("?", cmds),
		List:           createKeyCMD(cfg.KeyCommands.List, cmds),
		Subscription:   createKeyCMD(cfg.KeyCommands.Subscription, cmds),
		Favorite:       createKeyCMD(cfg.KeyCommands.Favorite, cmds),
		NavLeft:        createKeyCMD(cfg.KeyCommands.Left, cmds),
		NavUp:          createKeyCMD(cfg.KeyCommands.Up, cmds),
		NavDown:        createKeyCMD(cfg.KeyCommands.Down, cmds),
		NavRight:       createKeyCMD(cfg.KeyCommands.Right, cmds),
		NavPageUp:      createKeyCMD(cfg.KeyCommands.PageUp, cmds),
		NavPageDown:    createKeyCMD(cfg.KeyCommands.PageDown, cmds),
		Play:           createKeyCMD(cfg.KeyCommands.Play, cmds),
		Download:       createKeyCMD(cfg.KeyCommands.Download, cmds),
		OpenURL:        createKeyCMD(cfg.KeyCommands.OpenURL, cmds),
		MarkRead:       createKeyCMD(cfg.KeyCommands.MarkRead, cmds),
		MarkUnread:     createKeyCMD(cfg.KeyCommands.MarkUnread, cmds),
		MarkFavorite:   createKeyCMD(cfg.KeyCommands.MarkFavorite, cmds),
		MarkUnfavorite: createKeyCMD(cfg.KeyCommands.MarkUnfavorite, cmds),
	}

	bg := tcell.GetColor(cfg.Theme.BackgroundColor)
	fg := tcell.GetColor(cfg.Theme.ForegroundColor)

	if cfg.Theme.CommandLine != nil {
		w := cfg.Theme.CommandLine
		c.Theme.CMDBGColor = tcell.GetColor(w.BackgroundColor)
		c.Theme.CMDFGColor = tcell.GetColor(w.ForegroundColor)
	} else {
		c.Theme.CMDBGColor = bg
		c.Theme.CMDFGColor = fg
	}

	if cfg.Theme.StatusLine != nil {
		w := cfg.Theme.StatusLine
		c.Theme.StatusBGColor = tcell.GetColor(w.BackgroundColor)
		c.Theme.StatusFGColor = tcell.GetColor(w.ForegroundColor)
	} else {
		c.Theme.StatusBGColor = bg
		c.Theme.StatusFGColor = fg
	}

	if cfg.Theme.TitleLine != nil {
		w := cfg.Theme.TitleLine
		c.Theme.TitleBGColor = tcell.GetColor(w.BackgroundColor)
		c.Theme.TitleFGColor = tcell.GetColor(w.ForegroundColor)
	} else {
		c.Theme.TitleBGColor = bg
		c.Theme.TitleFGColor = fg
	}

	if cfg.Theme.List != nil {
		w := cfg.Theme.List
		c.Theme.ListBGColor = tcell.GetColor(w.BackgroundColor)
		c.Theme.ListFGColor = tcell.GetColor(w.ForegroundColor)
	} else {
		c.Theme.ListBGColor = bg
		c.Theme.ListFGColor = fg
	}

	if cfg.Theme.Tree != nil {
		w := cfg.Theme.Tree
		c.Theme.TreeBGColor = tcell.GetColor(w.BackgroundColor)
		c.Theme.TreeFGColor = tcell.GetColor(w.ForegroundColor)
	} else {
		c.Theme.TreeBGColor = bg
		c.Theme.TreeFGColor = fg
	}

	if cfg.Theme.Content != nil {
		w := cfg.Theme.Content
		c.Theme.ContentBGColor = tcell.GetColor(w.BackgroundColor)
		c.Theme.ContentFGColor = tcell.GetColor(w.ForegroundColor)
	} else {
		c.Theme.ContentBGColor = bg
		c.Theme.ContentFGColor = fg
	}

	c.AutoUpdateInterval, _ = time.ParseDuration(cfg.AutoUpdateInterval)

	return c
}

func load(configDir string) *config {
	dcfg := defaultConfig()
	path := filepath.Clean(filepath.Join(configDir, "config.toml"))
	if _, err := os.Stat(path); os.IsNotExist(err) {
		b, err := toml.Marshal(dcfg)
		if err != nil {
			return dcfg
		}
		if err := ioutil.WriteFile(filepath.Clean(path), b, 0600); err != nil {
			return dcfg
		}
		return dcfg
	}

	c, err := ioutil.ReadFile(filepath.Clean(path))
	if err != nil {
		return dcfg
	}

	cfg := &config{}
	if err := toml.Unmarshal(c, cfg); err != nil {
		return dcfg
	}

	return cfg
}

func defaultConfig() *config {
	cltheme := &commandLineTheme{
		BackgroundColor: "#2e3440",
		ForegroundColor: "#d8dee9",
	}

	cnttheme := &contentTheme{
		BackgroundColor: "#2e3440",
		ForegroundColor: "#e5e9f0",
	}

	ltheme := &listTheme{
		BackgroundColor: "#2e3440",
		ForegroundColor: "#e5e9f0",
	}

	ttheme := &treeTheme{
		BackgroundColor: "#2e3440",
		ForegroundColor: "#e5e9f0",
	}

	extPlay := &ExternalPlayer{
		Bin:  "ffplay",
		Args: []string{"[FILE]"},
	}

	keys := keyCommands{
		MarkRead:       "m",
		MarkUnread:     "u",
		MarkFavorite:   "f",
		MarkUnfavorite: "c",
		OpenURL:        "n",
		Download:       "d",
		Play:           "p",
		Left:           "h",
		Down:           "j",
		Up:             "k",
		Right:          "l",
		PageUp:         "PgUp",
		PageDown:       "PgDn",
		List:           "L",
		Subscription:   "S",
		Favorite:       "F",
	}

	pod := Podcast{
		AutoDownload:   true,
		ExternalPlayer: extPlay,
	}

	theme := theme{
		BackgroundColor: "#88c0d0",
		ForegroundColor: "#2e3440",
		ErrorColor:      "#bf616a",
		Separator:       "#88c0d0",
		ReadColor:       "#b48ead",
		CommandLine:     cltheme,
		Content:         cnttheme,
		List:            ltheme,
		Tree:            ttheme,
	}

	return &config{
		Theme:       theme,
		KeyCommands: keys,
		Podcast:     pod,
	}
}