7 "github.com/alecthomas/chroma/v2"
8 "github.com/alecthomas/chroma/v2/formatters"
9 "github.com/alecthomas/chroma/v2/lexers"
10 chromastyles "github.com/alecthomas/chroma/v2/styles"
11 "github.com/charmbracelet/crush/internal/ui/styles"
14// SyntaxHighlight applies syntax highlighting to the given source code based
15// on the file name and background color. It returns the highlighted code as a
17func SyntaxHighlight(st *styles.Styles, source, fileName string, bg color.Color) (string, error) {
18 // Determine the language lexer to use
19 l := lexers.Match(fileName)
21 l = lexers.Analyse(source)
26 l = chroma.Coalesce(l)
29 f := formatters.Get("terminal16m")
31 f = formatters.Fallback
34 style := chroma.MustNewStyle("crush", st.ChromaTheme())
36 // Modify the style to use the provided background
37 s, err := style.Builder().Transform(
38 func(t chroma.StyleEntry) chroma.StyleEntry {
39 r, g, b, _ := bg.RGBA()
40 t.Background = chroma.NewColour(uint8(r>>8), uint8(g>>8), uint8(b>>8))
45 s = chromastyles.Fallback
48 // Tokenize and format
49 it, err := l.Tokenise(nil, source)
55 err = f.Format(&buf, s, it)
56 return buf.String(), err
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list