-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.go
More file actions
58 lines (52 loc) · 1.55 KB
/
Copy pathhighlight.go
File metadata and controls
58 lines (52 loc) · 1.55 KB
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
package main
import (
"bytes"
"path/filepath"
"strings"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters"
"github.com/alecthomas/chroma/v2/lexers"
chromastyles "github.com/alecthomas/chroma/v2/styles"
)
// highlighter applies chroma syntax highlighting (catppuccin-mocha) to
// source lines shown in the diff view, keyed by file extension.
type highlighter struct {
style *chroma.Style
}
func newHighlighter() *highlighter {
return &highlighter{style: chromastyles.Get(ThemeName)}
}
// lexerFor picks a chroma lexer from the file's extension/name.
func lexerFor(rel string) chroma.Lexer {
if l := lexers.Match(filepath.Base(rel)); l != nil {
return chroma.Coalesce(l)
}
if ext := filepath.Ext(rel); ext != "" {
if l := lexers.Get(ext[1:]); l != nil {
return chroma.Coalesce(l)
}
}
return nil
}
// paint renders a source line through chroma and returns ANSI-colored
// text for the given token range of one line. It returns "" when no
// lexer applies (caller falls back to plain styling).
//
// Because lexing is line-oriented, we lex the single line; this is fast
// and good enough for diff rows (multi-line constructs may color
// slightly differently, which is acceptable for a minimal TUI).
func (h *highlighter) paint(rel, line string) string {
lx := lexerFor(rel)
if lx == nil {
return ""
}
it, err := lx.Tokenise(nil, line)
if err != nil {
return ""
}
var buf bytes.Buffer
if err := formatters.TTY16m.Format(&buf, h.style, it); err != nil {
return ""
}
return strings.TrimSuffix(buf.String(), "\n")
}