-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinput.go
300 lines (247 loc) · 6.47 KB
/
input.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"encoding/hex"
"errors"
"image/color"
"io/ioutil"
"regexp"
"strconv"
"strings"
"sort"
)
func readFile(filename string) (string, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
config := string(bytes[:])
return config, nil
}
func parseColor(c string) (color.Color, error) {
// Takes in a string of the format #FFFFFF or #FFFFFFFFFFFF and returns a color
// Remove leading #
c = strings.TrimPrefix(c, "#")
// Convert hexadecimal string to array of bytes.
bytes, err := hex.DecodeString(c)
if err != nil {
return nil, err
}
// Covert to array of uint8s
uints := []uint8(bytes[:])
// Take 0,1,2 indexes for a 6 character string, and 0,2,4 indexes for 12 characters
if len(uints) == 6 {
return color.NRGBA{uints[0], uints[2], uints[4], 255}, nil
} else if len(uints) == 3 {
return color.NRGBA{uints[0], uints[1], uints[2], 255}, nil
}
return nil, errors.New("Could not parse color: " + c)
}
func inputXfce(filename string) ([]color.Color, error) {
// Read in file
config, err := readFile(filename)
if err != nil {
return nil, err
}
// Split into lines
lines := strings.Split(config, "\n")
// Remove all spaces
for i, l := range lines {
lines[i] = strings.Replace(l, " ", "", -1)
}
// Find line containing color palette
colorpalette := ""
for _, l := range lines {
if strings.HasPrefix(l, "ColorPalette") {
colorpalette = l
}
}
if colorpalette == "" {
return nil, errors.New("ColorPalette not found in XFCE4 Terminal input")
}
// Get colors from palette
colorpalette = strings.TrimPrefix(colorpalette, "ColorPalette=")
// Trim trailing semicolon and spaces
colorpalette = strings.TrimRight(colorpalette, "; ")
// Split by semicolons
colorStrings := strings.Split(colorpalette, ";")
colors := make([]color.Color, 0)
for _, c := range colorStrings {
col, err := parseColor(c)
if err != nil {
return nil, err
}
colors = append(colors, col)
}
return colors, nil
}
func inputLilyTerm(filename string) ([]color.Color, error) {
colors := make([]color.Color, 0)
// Read in file
config, err := readFile(filename)
if err != nil {
return nil, err
}
// Split into lines
lines := strings.Split(config, "\n")
// Remove all spaces
for i, l := range lines {
lines[i] = strings.Replace(l, " ", "", -1)
}
// For all 16 colors (Color1, Color2...), search for each.
// TODO: Support for 16 bit, and "black"
for i := 0; i < 16; i++ {
for _, l := range lines {
prefix := "Color"
prefix += strconv.Itoa(i)
prefix += "="
if strings.HasPrefix(l, prefix) {
// Trim Prefix
hexstring := strings.TrimPrefix(l, prefix)
col, err := parseColor(hexstring)
if err != nil {
return nil, err
}
colors = append(colors, col)
}
}
}
return colors, nil
}
func inputTermite(filename string) ([]color.Color, error) {
colors := make([]color.Color, 0)
// Read in file
config, err := readFile(filename)
if err != nil {
return nil, err
}
// Split into lines
lines := strings.Split(config, "\n")
// Remove all spaces
for i, l := range lines {
lines[i] = strings.Replace(l, " ", "", -1)
}
// For all 16 colors (Color1, Color2...), search for each.
for i := 0; i < 16; i++ {
for _, l := range lines {
prefix := "color"
prefix += strconv.Itoa(i)
prefix += "="
if strings.HasPrefix(l, prefix) {
// Trim Prefix
hexstring := strings.TrimPrefix(l, prefix)
col, err := parseColor(hexstring)
if err != nil {
return nil, err
}
colors = append(colors, col)
}
}
}
return colors, nil
}
func inputTerminator(filename string) ([]color.Color, error) {
// Read in file
config, err := readFile(filename)
if err != nil {
return nil, err
}
// Split into lines
lines := strings.Split(config, "\n")
// Remove all spaces
for i, l := range lines {
lines[i] = strings.Replace(l, " ", "", -1)
}
// Find line containing color palette
colorpalette := ""
for _, l := range lines {
if strings.HasPrefix(l, "palette") {
colorpalette = l
}
}
if colorpalette == "" {
return nil, errors.New("ColorPalette not found in XFCE4 Terminal input")
}
// Get colors from palette
colorpalette = strings.TrimPrefix(colorpalette, "palette=\"")
colorpalette = strings.TrimSuffix(colorpalette, "\"")
colorStrings := strings.Split(colorpalette, ":")
colors := make([]color.Color, 0)
for _, c := range colorStrings {
col, err := parseColor(c)
if err != nil {
return nil, err
}
colors = append(colors, col)
}
return colors, nil
}
func inputXterm(filename string) ([]color.Color, error) {
// Read in file
config, err := readFile(filename)
if err != nil {
return nil, err
}
// Split into lines
lines := strings.Split(config, "\n")
// Remove all spaces
for i, l := range lines {
lines[i] = strings.Replace(l, " ", "", -1)
}
colorlines := make([]string, 0)
// Search for lines containing color information
re := regexp.MustCompile("[\\*]?[URXvurxterm]*[\\*.]+color[0-9]*")
for _, l := range lines {
if len(re.FindAllString(l, 1)) != 0 {
colorlines = append(colorlines, l)
}
}
// Extract and parse colors
// TODO: Sort by number first?
colors := make([]color.Color, 0)
for _, l := range colorlines {
// Assuming the color to be the rightmost half of the last colon
splits := strings.Split(l, ":")
colorstring := splits[len(splits)-1]
col, err := parseColor(colorstring)
if err != nil {
return nil, err
}
colors = append(colors, col)
}
return colors, nil
}
func inputKittyTerm(filename string) ([]color.Color, error) {
// Read in file
config, err := readFile(filename)
if err != nil {
return nil, err
}
// Split into lines
lines := strings.Split(config, "\n")
colorlines := make([]string, 0)
// Search for lines containing color information
re := regexp.MustCompile("^color[0-9]*")
for _, l := range lines {
if len(re.FindAllString(l, 1)) != 0 {
colorlines = append(colorlines, strings.Replace(l, "color", "", 1))
}
}
sort.Slice(colorlines, func(i, j int) bool {
numA, _ := strconv.Atoi(strings.TrimSpace(colorlines[i][:2]))
numB, _ := strconv.Atoi(strings.TrimSpace(colorlines[j][:2]))
return numA < numB
})
// Extract and parse colors
colors := make([]color.Color, 0)
for _, l := range colorlines {
// Assuming the color to be the rightmost half of the last instance of space/tab
splits := strings.Fields(l)
colorstring := splits[len(splits)-1]
col, err := parseColor(colorstring)
if err != nil {
return nil, err
}
colors = append(colors, col)
}
return colors, nil
}