-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_tables.go
225 lines (190 loc) · 4.48 KB
/
gen_tables.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
// Copyright 2023-2024 Takashi Takizawa. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// +build ignore
package main
import (
"bytes"
"encoding/csv"
"html/template"
"log"
"os"
"path/filepath"
)
const dataDir = "_data"
type Data struct {
ForegroundSystemColors []Color
BackgroundSystemColors []Color
Colors []Color
}
type Color struct {
Name string
HtmlHexColorCode string
AnsiColorCode string
}
func main() {
colors := readColors("8bit-color.csv")
foregroundSystemColors := readColors("foreground-system-color.csv")
backgroundSystemColors := readColors("background-system-color.csv")
data := Data{
ForegroundSystemColors: foregroundSystemColors,
BackgroundSystemColors: backgroundSystemColors,
Colors: colors,
}
generate(data, templateCode, "tables.go")
generate(data, templateText, "colors.md")
}
func readColors(filename string) []Color {
var colors []Color
f, err := os.Open(filepath.Join(dataDir, filename))
if err != nil {
log.Fatal(err)
}
r := csv.NewReader(f)
records, err := r.ReadAll()
if err != nil {
log.Fatal(err)
}
for _, record := range records {
if len(record) == 0 {
continue
}
c := Color{
Name: record[0],
HtmlHexColorCode: record[1],
AnsiColorCode: record[2],
}
colors = append(colors, c)
}
return colors
}
func generate(data Data, text string, filename string) {
b := bytes.NewBuffer([]byte{})
tmpl, err := template.New("table").Parse(text)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(b, data)
if err != nil {
log.Fatal(err)
}
f, err := os.CreateTemp(".", filename+".*")
if err != nil {
log.Fatal(err)
}
defer os.Remove(f.Name())
f.Write(b.Bytes())
f.Close()
os.Rename(f.Name(), filename)
}
var templateCode = `// Copyright 2021-2024 Takashi Takizawa. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Code generated by running go generate in github.com/ttkzw/go-color. DO NOT EDIT.
package color
// Foreground color name
const (
// Default foreground color
Default = Color(iota)
/*
* system colors for foreground colors
*/
{{range .ForegroundSystemColors}}
// {{.Name}} for system foreground color
System{{.Name}}
{{end}}
/*
* 8-bit colors for foreground colors
*/
{{range .Colors}}
// {{.Name}}: HTML Hex Color Code {{.HtmlHexColorCode}}, Ansi 8-bit Color Index {{.AnsiColorCode}}
{{.Name}}
{{end}}
)
var colorNames = []string{
// Default foreground color
"Default",
/*
* system colors for foreground colors
*/
{{range .ForegroundSystemColors}}
"System{{.Name}}",{{end}}
/*
* 8-bit colors for foreground colors
*/
{{range .Colors}}
"{{.Name}}",{{end}}
}
var colorParameters = [...]string{
// Default foreground color
Default: "39",
/*
* system colors for foreground colors
*/
{{range .ForegroundSystemColors}}
System{{.Name}}: "{{.AnsiColorCode}}",{{end}}
/*
* 8-bit colors for foreground colors
*/
{{range .Colors}}
{{.Name}}: "38;5;{{.AnsiColorCode}}",{{end}}
}
// Background color name
const (
// Default background color
DefaultBackground = BackgroundColor(iota)
/*
* system colors for background colors
*/
{{range .BackgroundSystemColors}}
// {{.Name}} for system background color
System{{.Name}}Background
{{end}}
/*
* 8-bit colors for background colors
*/
{{range .Colors}}
// {{.Name}} for background color: HTML Hex Color Code {{.HtmlHexColorCode}}, Ansi 8-bit Color Index {{.AnsiColorCode}}
{{.Name}}Background
{{end}}
)
var backgroundColorNames = []string{
// Default background color
"Default",
/*
* system colors for background colors
*/
{{range .BackgroundSystemColors}}
"System{{.Name}}",{{end}}
/*
* 8-bit colors for background colors
*/
{{range .Colors}}
"{{.Name}}",{{end}}
}
var backgroundColorParameters = [...]string{
// Default background color
DefaultBackground: "49",
/*
* system colors for background colors
*/
{{range .BackgroundSystemColors}}
System{{.Name}}Background: "{{.AnsiColorCode}}",{{end}}
/*
* 8-bit colors for background colors
*/
{{range .Colors}}
{{.Name}}Background: "48;5;{{.AnsiColorCode}}",{{end}}
}
`
var templateText = `
# Supported colors
## System Colors
{{range .ForegroundSystemColors}}
- <span style="color:{{.HtmlHexColorCode}};">System{{.Name}}</span>{{end}}
## Colors
{{range .Colors}}
- <span style="color:{{.HtmlHexColorCode}};">{{.Name}}</span>{{end}}
`