-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
173 lines (145 loc) · 3.83 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
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
tcell "github.com/gdamore/tcell/v2"
"gopkg.in/yaml.v3"
)
type config struct {
cmd string
wait time.Duration
noTitle bool
groups []*configGroup
}
type highlight struct {
Regex string `yaml:"regex"`
Foreground string `yaml:"fg"`
Background string `yaml:"bg"`
rx *regexp.Regexp `yaml:"-"`
style tcell.Style `yaml:"-"`
}
type configGroup struct {
file string `yaml:"-"`
CmdRegex string `yaml:"cmd_regex"`
cmdrx *regexp.Regexp `yaml:"-"`
Highlights []highlight `yaml:"highlights"`
}
func loadConfig(configFile, configDir string) (*config, error) {
cfg := &config{}
d, err := os.Open(configDir)
if err == nil {
files, err := d.Readdirnames(-1)
if err != nil {
return nil, fmt.Errorf("couldn't read configuration directory %q: %v", configDir, err)
}
for _, file := range files {
filename := filepath.Join(configDir, file)
cfgGroup, err := loadConfigFile(filename)
if err != nil {
fmt.Printf("WARN: couldn't load configuration file %q: %v\n", filename, err)
continue
}
cfg.groups = append(cfg.groups, cfgGroup)
}
sort.Slice(cfg.groups, func(i, j int) bool {
return cfg.groups[i].file < cfg.groups[j].file
})
}
cfgGroup, err := loadConfigFile(configFile)
if err != nil {
fmt.Printf("WARN: couldn't load configuration file %q: %v\n", configFile, err)
return cfg, nil
}
cfg.groups = append(cfg.groups, cfgGroup)
return cfg, nil
}
func loadConfigFile(file string) (*configGroup, error) {
cfg := &configGroup{}
f, err := os.Open(file)
if err != nil {
return cfg, nil // nolint:nilerr
}
defer func() {
if err := f.Close(); err != nil {
fmt.Printf("WARN: couldn't close file %s: %v\n", file, err)
}
}()
if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
return nil, err
}
cfg.file = file
cfg.cmdrx, err = regexp.Compile(cfg.CmdRegex)
if err != nil {
return nil, fmt.Errorf("couldn't compile regular expression %q: %v", cfg.CmdRegex, err)
}
for idx, hl := range cfg.Highlights {
cfg.Highlights[idx].style, err = combineAttributes(hl.Foreground, hl.Background)
if err != nil {
return nil, err
}
cfg.Highlights[idx].rx, err = regexp.Compile(hl.Regex)
if err != nil {
return nil, fmt.Errorf("couldn't compile regular expression %q: %v", hl.Regex, err)
}
}
return cfg, nil
}
func combineAttributes(fgAttr, bgAttr string) (style tcell.Style, err error) {
style = tcell.StyleDefault
colorMap := map[string]tcell.Color{
"black": tcell.ColorBlack,
"red": tcell.ColorRed,
"green": tcell.ColorGreen,
"yellow": tcell.ColorYellow,
"blue": tcell.ColorBlue,
"magenta": tcell.ColorFuchsia,
"cyan": tcell.ColorAqua,
"white": tcell.ColorWhite,
}
fgAttr = strings.TrimSpace(fgAttr)
if fgAttr != "" {
fgAttrs := strings.Split(fgAttr, ",")
for _, an := range fgAttrs {
an = strings.TrimSpace(an)
switch a := strings.ToLower(an); a {
case "bold":
style = style.Bold(true)
case "underline":
style = style.Underline(true)
case "reverse":
style = style.Reverse(true)
case "italic":
style = style.Italic(true)
case "blink":
style = style.Blink(true)
case "dim":
style = style.Dim(true)
case "strikethrough":
style = style.StrikeThrough(true)
default:
color, ok := colorMap[a]
if !ok {
return tcell.StyleDefault, fmt.Errorf("unknown foreground colour or attribute %q", an)
}
style = style.Foreground(color)
}
}
}
bgAttr = strings.TrimSpace(bgAttr)
if bgAttr != "" {
bgAttrs := strings.Split(bgAttr, ",")
for _, an := range bgAttrs {
color, ok := colorMap[strings.ToLower(an)]
if !ok {
return tcell.StyleDefault, fmt.Errorf("unknown background colour %q", an)
}
style = style.Background(color)
}
}
return style, nil
}