~chrisppy/beagles

ref: f43459449f608be374edacf02e49cfc9420871d1 beagles/config/config.go -rw-r--r-- 4.1 KiB
f4345944Chris Palmer Fix badge path 1 year, 11 months 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
// 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 (
	"bytes"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/BurntSushi/toml"
)

// Config contains all relevant configuration data for the application
type Config struct {
	BackgroundColor string       `toml:"background_color"`
	ForegroundColor string       `toml:"foreground_color"`
	ErrorColor      string       `toml:"error_color"`
	Separator       string       `toml:"separator_color"`
	KeyCommands     KeyCommands  `toml:"key_commands"`
	CommandLine     *CommandLine `toml:"command_line, omitempty"`
	StatusLine      *StatusLine  `toml:"status_line, omitempty"`
	TitleLine       *TitleLine   `toml:"title_line, omitempty"`
	List            *List        `toml:"list, omitempty"`
	Content         *Content     `toml:"content, omitempty"`
	Help            *Help        `toml:"help, omitempty"`
}

// KeyCommands config
type KeyCommands struct {
	MarkRead rune `toml:"mark_read"`
	OpenURL  rune `toml:"open_url"`
	Left     rune `toml:"left"`
	Up       rune `toml:"up"`
	Down     rune `toml:"down"`
	Right    rune `toml:"right"`
}

// StatusLine config
type StatusLine struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

// TitleLine config
type TitleLine struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

// CommandLine config
type CommandLine struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

// List config
type List struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

// Content config
type Content struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

// Help config
type Help struct {
	BackgroundColor string `toml:"background_color"`
	ForegroundColor string `toml:"foreground_color"`
}

// 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 {
	dtheme := defaultConfig()
	path := filepath.Clean(filepath.Join(configDir, "config.toml"))
	if _, err := os.Stat(path); os.IsNotExist(err) {
		buf := &bytes.Buffer{}
		if err := toml.NewEncoder(buf).Encode(&dtheme); err != nil {
			return dtheme
		}

		if err := ioutil.WriteFile(filepath.Clean(path), buf.Bytes(), 0600); err != nil {
			return dtheme
		}
		return dtheme
	}

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

	theme := &Config{}
	if err := toml.Unmarshal(cfg, theme); err != nil {
		return dtheme
	}

	return theme
}

func defaultConfig() *Config {
	cltheme := &CommandLine{
		BackgroundColor: "transparent",
		ForegroundColor: "#d8dee9",
	}

	cnttheme := &Content{
		BackgroundColor: "transparent",
		ForegroundColor: "#e5e9f0",
	}

	htheme := &Help{
		BackgroundColor: "transparent",
		ForegroundColor: "#e5e9f0",
	}

	ltheme := &List{
		BackgroundColor: "transparent",
		ForegroundColor: "#e5e9f0",
	}

	keys := KeyCommands{
		MarkRead: 'm',
		OpenURL:  'n',
		Left:     'h',
		Down:     'j',
		Up:       'k',
		Right:    'l',
	}

	return &Config{
		BackgroundColor: "#88c0d0",
		ForegroundColor: "#2e3440",
		ErrorColor:      "#bf616a",
		Separator:       "#88c0d0",
		CommandLine:     cltheme,
		Content:         cnttheme,
		List:            ltheme,
		Help:            htheme,
		KeyCommands:     keys,
	}
}