-
Notifications
You must be signed in to change notification settings - Fork 9
/
term.go
242 lines (195 loc) · 4.37 KB
/
term.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
package highlight
import (
"bytes"
"io"
"github.com/fatih/color"
)
// Term highlights a piece of code for rendering in the terminal.
func Term(lang string, code []byte) ([]byte, error) {
h, err := makeAndHighlight(lang, code)
if err != nil {
return nil, err
}
var buf bytes.Buffer
h.render(&buf, func(w io.Writer, class string, body []byte) {
color, ok := termColors[class]
if !ok {
color = resetColor
}
w.Write([]byte(color("%s", body)))
})
return buf.Bytes(), nil
}
// Theme borrowed from Felix Frederick Becker
// See THEME-LICENSE.txt
// https://github.com/felixfbecker/cli-highlight/blob/master/src/theme.ts
var resetColor = color.New(color.Reset).SprintfFunc()
var greyColor = color.New(color.FgBlack, color.Faint).SprintfFunc()
var termColors = map[string]func(format string, a ...interface{}) string{
/**
* keyword in a regular Algol-style language
*/
"keyword": color.BlueString,
/**
* built-in or library object (constant, class, function)
*/
"built_in": color.CyanString,
/**
* user-defined type in a language with first-class syntactically significant types, like
* Haskell
*/
"type": color.New(color.FgCyan, color.Faint).SprintfFunc(),
/**
* special identifier for a built-in value ("true", "false", "null")
*/
"literal": color.BlueString,
/**
* number, including units and modifiers, if any.
*/
"number": color.GreenString,
/**
* literal regular expression
*/
"regexp": color.RedString,
/**
* literal string, character
*/
"string": color.RedString,
/**
* parsed section inside a literal string
*/
"subst": resetColor,
/**
* symbolic constant, interned string, goto label
*/
"symbol": resetColor,
/**
* class or class-level declaration (interfaces, traits, modules, etc)
*/
"class": color.BlueString,
/**
* function or method declaration
*/
"function": color.YellowString,
/**
* name of a class or a function at the place of declaration
*/
"title": resetColor,
/**
* block of function arguments (parameters) at the place of declaration
*/
"params": resetColor,
/**
* comment
*/
"comment": color.GreenString,
/**
* documentation markup within comments
*/
"doctag": color.GreenString,
/**
* flags, modifiers, annotations, processing instructions, preprocessor directive, etc
*/
"meta": greyColor,
/**
* keyword or built-in within meta construct
*/
"meta-keyword": resetColor,
/**
* string within meta construct
*/
"meta-string": resetColor,
/**
* heading of a section in a config file, heading in text markup
*/
"section": resetColor,
/**
* XML/HTML tag
*/
"tag ": greyColor,
/**
* name of an XML tag, the first word in an s-expression
*/
"name": color.BlueString,
/**
* s-expression name from the language standard library
*/
"builtin-name": resetColor,
/**
* name of an attribute with no language defined semantics (keys in JSON, setting names in
* .ini), also sub-attribute within another highlighted object, like XML tag
*/
"attr": color.CyanString,
/**
* name of an attribute followed by a structured value part, like CSS properties
*/
"attribute": resetColor,
/**
* variable in a config or a template file, environment var expansion in a script
*/
"variable": resetColor,
/**
* list item bullet in text markup
*/
"bullet": resetColor,
/**
* code block in text markup
*/
"code": resetColor,
/**
* emphasis in text markup
*/
"emphasis": color.New(color.Italic).SprintfFunc(),
/**
* strong emphasis in text markup
*/
"strong": color.New(color.Bold).SprintfFunc(),
/**
* mathematical formula in text markup
*/
"formula": resetColor,
/**
* hyperlink in text markup
*/
"link": color.New(color.Underline).SprintfFunc(),
/**
* quotation in text markup
*/
"quote": resetColor,
/**
* tag selector in CSS
*/
"selector-tag": resetColor,
/**
* #id selector in CSS
*/
"selector-id": resetColor,
/**
* .class selector in CSS
*/
"selector-class": resetColor,
/**
* [attr] selector in CSS
*/
"selector-attr": resetColor,
/**
* :pseudo selector in CSS
*/
"selector-pseudo": resetColor,
/**
* tag of a template language
*/
"template-tag": resetColor,
/**
* variable in a template language
*/
"template-variable": resetColor,
/**
* added or changed line in a diff
*/
"addition": color.GreenString,
/**
* deleted line in a diff
*/
"deletion": color.RedString,
}