forked from peco/peco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
259 lines (221 loc) · 6.41 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
package peco
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/nsf/termbox-go"
)
var homedirFunc = homedir
// Config holds all the data that can be configured in the
// external configuran file
type Config struct {
Action map[string][]string `json:"Action"`
// Keymap used to be directly responsible for dispatching
// events against user input, but since then this has changed
// into something that just records the user's config input
Keymap map[string]string `json:"Keymap"`
Matcher string `json:"Matcher"` // Deprecated.
InitialMatcher string `json:"InitialMatcher"` // Use this instead of Matcher
Style *StyleSet `json:"Style"`
Prompt string `json:"Prompt"`
Layout string `json:"Layout"`
CustomMatcher map[string][]string
}
// NewConfig creates a new Config
func NewConfig() *Config {
return &Config{
Keymap: make(map[string]string),
InitialMatcher: IgnoreCaseMatch,
Style: NewStyleSet(),
Prompt: "QUERY>",
Layout: "top-down",
}
}
// ReadFilename reads the config from the given file, and
// does the appropriate processing, if any
func (c *Config) ReadFilename(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
err = json.NewDecoder(f).Decode(c)
if err != nil {
return err
}
if !IsValidLayoutType(LayoutType(c.Layout)) {
return fmt.Errorf("invalid layout type: %s", c.Layout)
}
return nil
}
var (
stringToFg = map[string]termbox.Attribute{
"default": termbox.ColorDefault,
"black": termbox.ColorBlack,
"red": termbox.ColorRed,
"green": termbox.ColorGreen,
"yellow": termbox.ColorYellow,
"blue": termbox.ColorBlue,
"magenta": termbox.ColorMagenta,
"cyan": termbox.ColorCyan,
"white": termbox.ColorWhite,
}
stringToBg = map[string]termbox.Attribute{
"on_default": termbox.ColorDefault,
"on_black": termbox.ColorBlack,
"on_red": termbox.ColorRed,
"on_green": termbox.ColorGreen,
"on_yellow": termbox.ColorYellow,
"on_blue": termbox.ColorBlue,
"on_magenta": termbox.ColorMagenta,
"on_cyan": termbox.ColorCyan,
"on_white": termbox.ColorWhite,
}
stringToFgAttr = map[string]termbox.Attribute{
"bold": termbox.AttrBold,
"underline": termbox.AttrUnderline,
"reverse": termbox.AttrReverse,
}
stringToBgAttr = map[string]termbox.Attribute{
"on_bold": termbox.AttrBold,
}
)
// StyleSet holds styles for various sections
type StyleSet struct {
Basic Style `json:"Basic"`
SavedSelection Style `json:"SavedSelection"`
Selected Style `json:"Selected"`
Query Style `json:"Query"`
Matched Style `json:"Matched"`
}
// NewStyleSet creates a new StyleSet struct
func NewStyleSet() *StyleSet {
return &StyleSet{
Basic: Style{fg: termbox.ColorDefault, bg: termbox.ColorDefault},
Query: Style{fg: termbox.ColorDefault, bg: termbox.ColorDefault},
Matched: Style{fg: termbox.ColorCyan, bg: termbox.ColorDefault},
SavedSelection: Style{fg: termbox.ColorBlack | termbox.AttrBold, bg: termbox.ColorCyan},
Selected: Style{fg: termbox.ColorDefault | termbox.AttrUnderline, bg: termbox.ColorMagenta},
}
}
func (s StyleSet) BasicFG() termbox.Attribute {
return s.Basic.fg
}
func (s StyleSet) BasicBG() termbox.Attribute {
return s.Basic.bg
}
func (s StyleSet) QueryFG() termbox.Attribute {
return s.Query.fg
}
func (s StyleSet) QueryBG() termbox.Attribute {
return s.Query.bg
}
func (s StyleSet) MatchedFG() termbox.Attribute {
return s.Matched.fg
}
func (s StyleSet) MatchedBG() termbox.Attribute {
return s.Matched.bg
}
func (s StyleSet) SavedSelectionFG() termbox.Attribute {
return s.SavedSelection.fg
}
func (s StyleSet) SavedSelectionBG() termbox.Attribute {
return s.SavedSelection.bg
}
func (s StyleSet) SelectedFG() termbox.Attribute {
return s.Selected.fg
}
func (s StyleSet) SelectedBG() termbox.Attribute {
return s.Selected.bg
}
// Style describes termbox styles
type Style struct {
fg termbox.Attribute
bg termbox.Attribute
}
// UnmarshalJSON satisfies json.RawMessage.
func (s *Style) UnmarshalJSON(buf []byte) error {
raw := []string{}
if err := json.Unmarshal(buf, &raw); err != nil {
return err
}
*s = *stringsToStyle(raw)
return nil
}
func stringsToStyle(raw []string) *Style {
style := &Style{
fg: termbox.ColorDefault,
bg: termbox.ColorDefault,
}
for _, s := range raw {
fg, ok := stringToFg[s]
if ok {
style.fg = fg
}
bg, ok := stringToBg[s]
if ok {
style.bg = bg
}
}
for _, s := range raw {
if fgAttr, ok := stringToFgAttr[s]; ok {
style.fg |= fgAttr
}
if bgAttr, ok := stringToBgAttr[s]; ok {
style.bg |= bgAttr
}
}
return style
}
var _locateRcfileIn = locateRcfileIn
func locateRcfileIn(dir string) (string, error) {
const basename = "config.json"
file := filepath.Join(dir, basename)
if _, err := os.Stat(file); err != nil {
return "", err
}
return file, nil
}
// LocateRcfile attempts to find the config file in various locations
func LocateRcfile() (string, error) {
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
//
// Try in this order:
// $XDG_CONFIG_HOME/peco/config.json
// $XDG_CONFIG_DIR/peco/config.json (where XDG_CONFIG_DIR is listed in $XDG_CONFIG_DIRS)
// ~/.peco/config.json
home, uErr := homedirFunc()
// Try dir supplied via env var
if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
file, err := _locateRcfileIn(filepath.Join(dir, "peco"))
if err == nil {
return file, nil
}
} else if uErr == nil { // silently ignore failure for homedir()
// Try "default" XDG location, is user is available
file, err := _locateRcfileIn(filepath.Join(home, ".config", "peco"))
if err == nil {
return file, nil
}
}
// this standard does not take into consideration windows (duh)
// while the spec says use ":" as the separator, Go provides us
// with filepath.ListSeparator, so use it
if dirs := os.Getenv("XDG_CONFIG_DIRS"); dirs != "" {
for _, dir := range strings.Split(dirs, fmt.Sprintf("%c", filepath.ListSeparator)) {
file, err := _locateRcfileIn(filepath.Join(dir, "peco"))
if err == nil {
return file, nil
}
}
}
if uErr == nil { // silently ignore failure for homedir()
file, err := _locateRcfileIn(filepath.Join(home, ".peco"))
if err == nil {
return file, nil
}
}
return "", fmt.Errorf("error: Config file not found")
}