-
Notifications
You must be signed in to change notification settings - Fork 20
/
colorscheme.go
236 lines (184 loc) · 5.39 KB
/
colorscheme.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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"path"
"gopkg.in/yaml.v2"
)
type Base16Colorscheme struct {
Name string `yaml:"scheme"`
Author string `yaml:"author"`
Color00 string `yaml:"base00"`
Color01 string `yaml:"base01"`
Color02 string `yaml:"base02"`
Color03 string `yaml:"base03"`
Color04 string `yaml:"base04"`
Color05 string `yaml:"base05"`
Color06 string `yaml:"base06"`
Color07 string `yaml:"base07"`
Color08 string `yaml:"base08"`
Color09 string `yaml:"base09"`
Color10 string `yaml:"base0A"`
Color11 string `yaml:"base0B"`
Color12 string `yaml:"base0C"`
Color13 string `yaml:"base0D"`
Color14 string `yaml:"base0E"`
Color15 string `yaml:"base0F"`
RepoURL string
RawBaseURL string
FileName string
}
func (s Base16Colorscheme) MustacheContext(ext string) map[string]interface{} {
var bases = map[string]string{
"00": s.Color00,
"01": s.Color01,
"02": s.Color02,
"03": s.Color03,
"04": s.Color04,
"05": s.Color05,
"06": s.Color06,
"07": s.Color07,
"08": s.Color08,
"09": s.Color09,
"0A": s.Color10,
"0B": s.Color11,
"0C": s.Color12,
"0D": s.Color13,
"0E": s.Color14,
"0F": s.Color15,
}
slug := strings.Replace(strings.ToLower(s.FileName), " ", "-", -1)
ret := map[string]interface{}{
"scheme-name": s.Name,
"scheme-author": s.Author,
"scheme-slug": slug,
"scheme-slug-underscored": strings.Replace(slug, "-", "_", -1),
}
for base, color := range bases {
baseKey := "base" + base
//TODO
rVal := color[:2]
gVal := color[2:4]
bVal := color[4:6]
rValf, err := strconv.ParseUint(rVal, 16, 32)
check(err)
gValf, err := strconv.ParseUint(gVal, 16, 32)
check(err)
bValf, err := strconv.ParseUint(bVal, 16, 32)
check(err)
ret[baseKey+"-hex"] = rVal + gVal + bVal
ret[baseKey+"-hex-r"] = rVal
ret[baseKey+"-hex-g"] = gVal
ret[baseKey+"-hex-b"] = bVal
ret[baseKey+"-rgb-r"] = rValf
ret[baseKey+"-rgb-g"] = gValf
ret[baseKey+"-rgb-b"] = bValf
ret[baseKey+"-dec-r"] = rValf / 255
ret[baseKey+"-dec-g"] = gValf / 255
ret[baseKey+"-dec-b"] = bValf / 255
}
return ret
}
func (l *Base16ColorschemeList) GetBase16Colorscheme(name string) (Base16Colorscheme, error) {
if len(name) == 0 {
panic("Colorscheme name was empty")
}
schemePath := path.Join(appConf.SchemesCachePath, name)
parts := strings.Split(l.colorschemes[name], "/")
yamlURL := strings.Join([]string{"https://raw.githubusercontent.com", parts[3], parts[4], parts[6], parts[7]}, "/")
// Create local schemes file, if not present
if _, err := os.Stat(schemePath); os.IsNotExist(err) {
fmt.Println("downloading theme from: ", yamlURL)
schemeData, err := DownloadFileToString(yamlURL)
check(err)
saveFile, err := os.Create(schemePath)
//TODO delete old file?
defer saveFile.Close()
check(err)
saveFile.Write([]byte(schemeData))
saveFile.Close()
}
colorscheme, err := ioutil.ReadFile(schemePath)
check(err)
scheme := NewBase16Colorscheme(string(colorscheme))
scheme.RawBaseURL = yamlURL
scheme.FileName = parts[7][:len(parts[7])-5]
return scheme, err
}
func (l *Base16ColorschemeList) GetBase16ColorschemeFile(name string) (Base16Colorscheme, error) {
if len(name) == 0 {
panic("Colorscheme path was empty")
}
_, err := os.Stat(name)
// no error is tolerable for local files
check(err)
colorscheme, err := ioutil.ReadFile(name)
check(err)
scheme := NewBase16Colorscheme(string(colorscheme))
_, fileName := path.Split(name)
scheme.FileName = fileName[:len(fileName)-5]
return scheme, err
}
func NewBase16Colorscheme(yamlData string) Base16Colorscheme {
var scheme Base16Colorscheme
err := yaml.Unmarshal([]byte(yamlData), &scheme)
check(err)
return scheme
}
func LoadBase16ColorschemeList() Base16ColorschemeList {
colorschemes := LoadStringMap(appConf.SchemesListFile)
return Base16ColorschemeList{colorschemes}
}
func SaveBase16ColorschemeList(l Base16ColorschemeList) {
SaveStringMap(l.colorschemes, appConf.SchemesListFile)
}
type Base16ColorschemeList struct {
colorschemes map[string]string
}
func (l *Base16ColorschemeList) UpdateSchemes() {
//Get all repos from master source
schemeRepos := make(map[string]string)
schemesYAML, err := DownloadFileToString(appConf.SchemesMasterURL)
check(err)
err = yaml.Unmarshal([]byte(schemesYAML), &schemeRepos)
check(err)
fmt.Println("Found colorscheme repos: ", len(schemeRepos))
for k, v := range schemeRepos {
schemeRepos[k] = v
}
for _, v1 := range schemeRepos {
fmt.Println("Getting schemes from: " + v1)
for _, v2 := range findYAMLinRepo(v1) {
l.colorschemes[v2.Name] = v2.HTMLURL
}
}
fmt.Println("Found colorschemes: ", len(l.colorschemes))
SaveBase16ColorschemeList(Base16ColorschemeList{l.colorschemes})
}
func (c *Base16ColorschemeList) Find(input string) Base16Colorscheme {
if strings.Contains(input, "/") {
// a local scheme path, not a name
colorschemeName := input
scheme, err := c.GetBase16ColorschemeFile(colorschemeName)
check(err)
return scheme
} else {
// a name, look it up
if _, err := os.Stat(appConf.SchemesListFile); os.IsNotExist(err) {
check(err)
fmt.Println("Colorschemes list not found, pulling new one...")
c.UpdateSchemes()
}
if len(c.colorschemes) == 0 {
fmt.Println("No templates in list, pulling new one... ")
c.UpdateSchemes()
}
colorschemeName := FindMatchInMap(c.colorschemes, input)
scheme, err := c.GetBase16Colorscheme(colorschemeName)
check(err)
return scheme
}
}