forked from owenthereal/ccat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
68 lines (57 loc) · 1.74 KB
/
printer.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
package main
import (
"bytes"
"io"
"github.com/jingweno/ccat/Godeps/_workspace/src/github.com/sourcegraph/syntaxhighlight"
)
type ColorDefs map[syntaxhighlight.Kind]string
var LightColorDefs = ColorDefs{
syntaxhighlight.String: "brown",
syntaxhighlight.Keyword: "darkblue",
syntaxhighlight.Comment: "lightgrey",
syntaxhighlight.Type: "teal",
syntaxhighlight.Literal: "teal",
syntaxhighlight.Punctuation: "darkred",
syntaxhighlight.Plaintext: "darkblue",
syntaxhighlight.Tag: "blue",
syntaxhighlight.HTMLTag: "lightgreen",
syntaxhighlight.HTMLAttrName: "blue",
syntaxhighlight.HTMLAttrValue: "green",
syntaxhighlight.Decimal: "darkblue",
}
var DarkColorDefs = ColorDefs{
syntaxhighlight.String: "brown",
syntaxhighlight.Keyword: "blue",
syntaxhighlight.Comment: "darkgrey",
syntaxhighlight.Type: "turquoise",
syntaxhighlight.Literal: "turquoise",
syntaxhighlight.Punctuation: "red",
syntaxhighlight.Plaintext: "blue",
syntaxhighlight.Tag: "blue",
syntaxhighlight.HTMLTag: "lightgreen",
syntaxhighlight.HTMLAttrName: "blue",
syntaxhighlight.HTMLAttrValue: "green",
syntaxhighlight.Decimal: "blue",
}
func AsCCat(src []byte, cdefs ColorDefs) ([]byte, error) {
var buf bytes.Buffer
err := syntaxhighlight.Print(syntaxhighlight.NewScanner(src), &buf, Printer{cdefs})
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type Printer struct {
ColorDefs ColorDefs
}
func (p Printer) Print(w io.Writer, kind syntaxhighlight.Kind, tokText string) error {
c := p.ColorDefs[kind]
if c != "" {
tokText = Colorize(c, tokText)
}
_, err := io.WriteString(w, tokText)
if err != nil {
return err
}
return nil
}