format.go

 1package common
 2
 3import (
 4	"fmt"
 5	"strings"
 6
 7	"github.com/alecthomas/chroma/lexers"
 8	gansi "github.com/charmbracelet/glamour/ansi"
 9	"github.com/charmbracelet/soft-serve/server/ui/styles"
10	"github.com/muesli/termenv"
11)
12
13// FormatLineNumber adds line numbers to a string.
14func FormatLineNumber(styles *styles.Styles, s string, color bool) (string, int) {
15	lines := strings.Split(s, "\n")
16	// NB: len() is not a particularly safe way to count string width (because
17	// it's counting bytes instead of runes) but in this case it's okay
18	// because we're only dealing with digits, which are one byte each.
19	mll := len(fmt.Sprintf("%d", len(lines)))
20	for i, l := range lines {
21		digit := fmt.Sprintf("%*d", mll, i+1)
22		bar := "│"
23		if color {
24			digit = styles.Code.LineDigit.Render(digit)
25			bar = styles.Code.LineBar.Render(bar)
26		}
27		if i < len(lines)-1 || len(l) != 0 {
28			// If the final line was a newline we'll get an empty string for
29			// the final line, so drop the newline altogether.
30			lines[i] = fmt.Sprintf(" %s %s %s", digit, bar, l)
31		}
32	}
33	return strings.Join(lines, "\n"), mll
34}
35
36// FormatHighlight adds syntax highlighting to a string.
37func FormatHighlight(p, c string) (string, error) {
38	zero := uint(0)
39	lang := ""
40	lexer := lexers.Match(p)
41	if lexer != nil && lexer.Config() != nil {
42		lang = lexer.Config().Name
43	}
44	formatter := &gansi.CodeBlockElement{
45		Code:     c,
46		Language: lang,
47	}
48	r := strings.Builder{}
49	styles := StyleConfig()
50	styles.CodeBlock.Margin = &zero
51	rctx := gansi.NewRenderContext(gansi.Options{
52		Styles:       styles,
53		ColorProfile: termenv.TrueColor,
54	})
55	err := formatter.Render(&r, rctx)
56	if err != nil {
57		return "", err
58	}
59	return r.String(), nil
60}