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/tui/styles"
14func SyntaxHighlight(source, fileName string, bg color.Color) (string, error) {
15 // Determine the language lexer to use
16 l := lexers.Match(fileName)
18 l = lexers.Analyse(source)
23 l = chroma.Coalesce(l)
26 f := formatters.Get("terminal16m")
28 f = formatters.Fallback
31 style := chroma.MustNewStyle("crush", styles.GetChromaTheme())
33 // Modify the style to use the provided background
34 s, err := style.Builder().Transform(
35 func(t chroma.StyleEntry) chroma.StyleEntry {
36 r, g, b, _ := bg.RGBA()
37 t.Background = chroma.NewColour(uint8(r>>8), uint8(g>>8), uint8(b>>8))
42 s = chromaStyles.Fallback
45 // Tokenize and format
46 it, err := l.Tokenise(nil, source)
52 err = f.Format(&buf, s, it)
53 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