-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsyncat.go
91 lines (79 loc) · 2.27 KB
/
syncat.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/fatih/color"
"github.com/zyedidia/highlight"
)
func main() {
if len(os.Args) <= 1 {
fmt.Println("No input file")
return
}
var defs []*highlight.Def
gopath := os.Getenv("GOPATH")
files, _ := ioutil.ReadDir(gopath + "/src/github.com/zyedidia/highlight/syntax_files")
for _, f := range files {
if strings.HasSuffix(f.Name(), ".yaml") {
input, _ := ioutil.ReadFile(gopath + "/src/github.com/zyedidia/highlight/syntax_files/" + f.Name())
d, err := highlight.ParseDef(input)
if err != nil {
fmt.Println(err)
continue
}
defs = append(defs, d)
}
}
highlight.ResolveIncludes(defs)
fileSrc, _ := ioutil.ReadFile(os.Args[1])
def := highlight.DetectFiletype(defs, os.Args[1], bytes.Split(fileSrc, []byte("\n"))[0])
if def == nil {
fmt.Println(string(fileSrc))
return
}
h := highlight.NewHighlighter(def)
matches := h.HighlightString(string(fileSrc))
lines := strings.Split(string(fileSrc), "\n")
for lineN, l := range lines {
colN := 0
for _, c := range l {
if group, ok := matches[lineN][colN]; ok {
// There are more possible groups available than just these ones
if group == highlight.Groups["statement"] {
color.Set(color.FgGreen)
} else if group == highlight.Groups["identifier"] {
color.Set(color.FgBlue)
} else if group == highlight.Groups["preproc"] {
color.Set(color.FgHiRed)
} else if group == highlight.Groups["special"] {
color.Set(color.FgRed)
} else if group == highlight.Groups["constant.string"] {
color.Set(color.FgCyan)
} else if group == highlight.Groups["constant"] {
color.Set(color.FgCyan)
} else if group == highlight.Groups["constant.specialChar"] {
color.Set(color.FgHiMagenta)
} else if group == highlight.Groups["type"] {
color.Set(color.FgYellow)
} else if group == highlight.Groups["constant.number"] {
color.Set(color.FgCyan)
} else if group == highlight.Groups["comment"] {
color.Set(color.FgHiGreen)
} else {
color.Unset()
}
}
fmt.Print(string(c))
colN++
}
if group, ok := matches[lineN][colN]; ok {
if group == highlight.Groups["default"] || group == highlight.Groups[""] {
color.Unset()
}
}
fmt.Print("\n")
}
}