-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_functions.go
320 lines (279 loc) · 7.82 KB
/
common_functions.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package zaje
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
// "reflect"
"github.com/fatih/color"
highlight "github.com/jessp01/gohighlight"
"github.com/urfave/cli"
)
var def *highlight.Def
// SynDir path to github.com/jessp01/gohighlight/syntax_files
var SynDir string
// HighlightLexer lexer to use
var HighlightLexer string
// Debug print debug info
var Debug bool
// AddLineNumbers prefix output with line numbers
var AddLineNumbers bool
// RemoveLineNumbers useful when working on an image input
var RemoveLineNumbers bool
var userSynDir = os.Getenv("HOME") + "/.config/zaje/syntax_files"
var globalSynDir = "/etc/zaje/syntax_files"
// PopulateAppMetadata see https://github.com/urfave/cli/blob/v1.22.14/docs/v1/manual.md#customization-1
func PopulateAppMetadata(app *cli.App) {
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[input-file || - ]{{end}}
{{if len .Authors}}
COMMANDS:
{{range .Commands}}{{if not .HideHelp}} {{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range .VisibleFlags}}{{.}}{{ "\n" }}
{{end}}{{end}}{{if .Copyright }}
EXAMPLES:
To use {{.Name}} as a cat replacement:
$ {{.Name}} /path/to/file
To replace tail -f:
$ tail -f /path/to/file | {{.Name}} -l server-log -
(- will make {{.Name}} read progressively from STDIN)
AUTHOR:
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
app.Usage = "Syntax highlighter to cover all your shell needs"
app.Version = "0.21.19"
app.EnableBashCompletion = true
cli.VersionFlag = cli.BoolFlag{
Name: "print-version, V",
Usage: "print only the version",
}
app.Compiled = time.Now()
app.Description = "Highlights text based on regular expressions/strings/characters matching.\n Can operate on files or data sent to STDIN.\n"
app.Authors = []cli.Author{
{
Name: "Jesse Portnoy",
Email: "[email protected]",
},
}
app.Copyright = "(c) packman.io"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "syn-dir, s",
Usage: "Path to lexer files. The `ZAJE_SYNDIR` ENV var is also honoured.\n If neither is set, " + userSynDir + " will be used.\n",
EnvVar: "ZAJE_SYNDIR",
Destination: &SynDir,
},
cli.StringFlag{
Name: "lexer, l",
Usage: `config file to use when parsing input.
When none is passed, zaje will attempt to autodetect based on the file name or first line of input.
You can set the path to lexer files by exporting the ZAJE_SYNDIR ENV var.
If not exported, /etc/zaje/syntax_files will be used.`,
Destination: &HighlightLexer,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Run in debug mode.\n",
Destination: &Debug,
},
cli.BoolFlag{
Name: "add-line-numbers, ln",
Usage: "Add line numbers.\n",
Destination: &AddLineNumbers,
},
}
}
func printDebugInfo() {
fmt.Println("DEBUG INFO:")
fmt.Println("Syntax files dir: " + SynDir)
fmt.Println("Lexer: " + HighlightLexer)
fmt.Println("DEFINITIONS:")
fmt.Println(def)
}
// NullifyDef this is only needed for the test in `zaje_test.go`
func NullifyDef() {
def = nil
}
func getDefs(filename string, data []byte) []highlight.LineMatch {
if SynDir == "" {
if SynDir == "" {
if stat, err := os.Stat(userSynDir); err == nil && stat.IsDir() {
SynDir = userSynDir
} else {
if stat, err := os.Stat(globalSynDir); err == nil && stat.IsDir() {
SynDir = globalSynDir
}
}
}
}
var defs []*highlight.Def
warnings, lerr := highlight.ParseSyntaxFiles(SynDir, &defs)
if lerr != nil {
log.Fatal(lerr)
}
highlight.ResolveIncludes(defs)
// Always try to auto detect the best lexer
if def == nil {
def = highlight.DetectFiletype(defs, filename, bytes.Split(data, []byte("\n"))[0])
}
// if a specific lexer was requested by setting the ENV var, try to load it
if HighlightLexer != "" {
syntaxFile, lerr := ioutil.ReadFile(SynDir + "/" + HighlightLexer + ".yaml")
if lerr == nil {
// Parse it into a `*highlight.Def`
def, _ = highlight.ParseDef(syntaxFile)
}
}
if Debug {
printDebugInfo()
if len(warnings) > 0 {
fmt.Println(warnings)
}
}
if def == nil {
return nil
}
h := highlight.NewHighlighter(def)
return h.HighlightString(string(data))
}
func colourOutput(matches []highlight.LineMatch, data []byte) {
lines := strings.Split(string(data), "\n")
lastLineNumberLength := len(fmt.Sprint(len(lines)))
for lineN, l := range lines {
colN := 0
if AddLineNumbers {
fmt.Print(strings.Repeat(" ", lastLineNumberLength-len(fmt.Sprint(lineN+1))))
color.Set(color.FgYellow)
fmt.Print(fmt.Sprintf("%d", lineN+1) + " ")
color.Unset()
}
for _, c := range l {
if group, ok := matches[lineN][colN]; ok {
switch group {
case highlight.Groups["default"]:
fallthrough
case highlight.Groups[""]:
color.Unset()
case highlight.Groups["statement"]:
fallthrough
case highlight.Groups["green"]:
color.Set(color.FgGreen)
case highlight.Groups["identifier"]:
fallthrough
case highlight.Groups["blue"]:
color.Set(color.FgHiBlue)
case highlight.Groups["preproc"]:
color.Set(color.FgHiRed)
case highlight.Groups["special"]:
fallthrough
case highlight.Groups["type.keyword"]:
fallthrough
case highlight.Groups["red"]:
color.Set(color.FgRed)
case highlight.Groups["constant"]:
fallthrough
case highlight.Groups["constant.number"]:
fallthrough
case highlight.Groups["constant.bool"]:
fallthrough
case highlight.Groups["symbol.brackets"]:
fallthrough
case highlight.Groups["identifier.var"]:
fallthrough
case highlight.Groups["cyan"]:
color.Set(color.FgCyan)
case highlight.Groups["constant.specialChar"]:
fallthrough
case highlight.Groups["constant.string.url"]:
fallthrough
case highlight.Groups["constant.string"]:
fallthrough
case highlight.Groups["magenta"]:
color.Set(color.FgHiMagenta)
case highlight.Groups["type"]:
fallthrough
case highlight.Groups["symbol.operator"]:
fallthrough
case highlight.Groups["symbol.tag.extended"]:
fallthrough
case highlight.Groups["yellow"]:
color.Set(color.FgYellow)
case highlight.Groups["comment"]:
fallthrough
case highlight.Groups["high.green"]:
color.Set(color.FgHiGreen)
default:
color.Unset()
}
}
fmt.Print(string(c))
colN++
}
color.Unset()
fmt.Print("\n")
}
}
// HandleData process input
func HandleData(filename string, data []byte) {
matches := getDefs(filename, data)
if matches == nil {
fmt.Println(string(data))
return
}
colourOutput(matches, data)
}
// DownloadFile helper function to download image file (when input is a remote image)
func DownloadFile(url, fileName string) error {
response, err := http.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errors.New("Received non 200 response code")
}
// Create a empty file
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
// Write the bytes to the fiel
_, err = io.Copy(file, response.Body)
if err != nil {
return err
}
return nil
}
// ReadDataFromFile reads from local file or a remote HTTP(s) URL and returns data
func ReadDataFromFile(filename string) ([]byte, error) {
var data []byte
var resp *http.Response
var err error
httpRegex := regexp.MustCompile("^http(s)?://")
if httpRegex.Match([]byte(filename)) {
resp, err = http.Get(filename)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
} else {
data, _ = ioutil.ReadFile(filename)
}
return data, err
}