-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.go
146 lines (124 loc) · 2.69 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
package main
import (
"embed"
"encoding/base64"
"fmt"
"io/ioutil"
log "github.com/sirupsen/logrus"
"github.com/shimmerglass/bar3x/ui"
"gopkg.in/yaml.v2"
)
var defaultCtx ui.Context
//go:embed resources/*
var assets embed.FS
func mustB64Asset(path string) string {
data, err := assets.ReadFile(path)
if err != nil {
log.Fatalf("could not open embeded asset %s: %s", path, err)
}
b64 := base64.StdEncoding.EncodeToString(data)
return "base64:" + b64
}
func init() {
defaultCtx = ui.Context{
"tray_icon_size": 20,
"tray_icon_padding": 2,
"h_padding": 5,
"v_padding": 5,
"bar_height": 30,
"text_font_size": 13.0,
"text_small_font_size": 11.0,
"icon_font_size": 13.0,
"text_font": mustB64Asset("resources/fonts/noto-sans.ttf"),
"icon_font": mustB64Asset("resources/fonts/nerdfont-noto-mono.ttf"),
"bg_color": "#17191e",
"text_color": "#d4e5f7",
"accent_color": "#1ebce8",
"neutral_color": "#37393e",
"neutral_light_color": "#90949d",
"success_color": "#28a745",
"warning_color": "#ffc107",
"danger_color": "#dc3545",
"icons": map[string]interface{}{
"error": "\uf071",
"dot": "\uf444",
"transfer": "\ufa4e",
"chip": "\uf85a",
"chip2": "\uf2db",
"lock": "\uf023",
"calendar": "\uf073",
"disk": "\uf0a0",
},
"bar_left": `
<ModuleRow>
<Volume />
</ModuleRow>
`,
"bar_center": `
<ModuleRow>
<DateTime />
</ModuleRow>
`,
"bar_right": `
<ModuleRow>
<Connections />
<CPU />
<RAM />
<DiskUsage />
</ModuleRow>
`,
"bar_background": `
<Rect
Width="{bar_width}"
Height="{bar_height}"
Color="{bg_color}"
/>
`,
"module": `
<Row ctx:mfirst="{is_first_visible}">
<Sizer
Visible="{!mfirst}"
PaddingLeft="10"
PaddingRight="10"
>
<Rect
Width="5"
Height="5"
Color="{neutral_color}"
/>
</Sizer>
<Sizer ref="Content" />
</Row>
`,
}
}
func getConfig(cfgPath, themePath string) (ui.Context, error) {
ctx := defaultCtx
if themePath != "" {
tctx, err := loadConfigFile(themePath)
if err != nil {
return nil, err
}
ctx = ctx.New(tctx)
}
if cfgPath != "" {
tctx, err := loadConfigFile(cfgPath)
if err != nil {
return nil, err
}
ctx = ctx.New(tctx)
}
return ctx, nil
}
func loadConfigFile(path string) (ui.Context, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("cannot read config file: %w", err)
}
ctx := ui.Context{}
err = yaml.Unmarshal(contents, &ctx)
if err != nil {
return nil, fmt.Errorf("cannot parse config file: %w", err)
}
return ctx, nil
}