This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhighlighting.go
More file actions
97 lines (83 loc) · 2.83 KB
/
Copy pathhighlighting.go
File metadata and controls
97 lines (83 loc) · 2.83 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"strings"
)
const (
spaceChar = " " // lipgloss seems to strip out regular trailing whitespace
maxPreviewWidth = 58
sampleText = `
=== Python ===
{keyword}def{/keyword} {function}return_pi_or_nan{/function}(return_pi: {type}bool{/type}) -> {type}float{/type}:
{function}print{/function}({string}"Hello world!"{/string})
{comment}# This is a comment{/comment}
pi: {type}float{/type} = {number}3.14159{/number}
{keyword}if{/keyword} return_pi:
{keyword}return{/keyword} pi
{keyword}return{/keyword} {constant}numpy{/constant}.nan
=== Go ===
{keyword}func{/keyword} {function}returnPiOrNaN{/function}(returnPi {type}bool{/type}) {type}float64{/type} {
{constant}fmt{/constant}.{function}Println{/function}({string}"Hello world!"{/string})
{comment}// This is a comment{/comment}
pi := {number}3.14159{/number}
{keyword}if{/keyword} returnPi {
{keyword}return{/keyword} pi
}
{keyword}return{/keyword} {constant}math{/constant}.{function}NaN(){/function}
}`
)
func parseSyntaxHighlighting(code string, theme *Theme) string {
var result strings.Builder
lines := strings.Split(code, "\n")
r := lipgloss.Renderer{}
r.SetColorProfile(termenv.TrueColor)
baseStyle := lipgloss.NewStyle().
Renderer(&r).
Foreground(lipgloss.Color(*theme.Foreground)).
Background(lipgloss.Color(*theme.Background))
for _, line := range lines {
i := 0
previewLine := strings.Builder{}
rawLine := strings.Builder{}
for i < len(line) {
if i+1 < len(line) && line[i:i+1] == "{" {
tagEnd := strings.Index(line[i:], "}")
if tagEnd == -1 {
previewLine.WriteString(line[i:])
rawLine.WriteString(line[i:])
break
}
tagEnd += i
tag := line[i+1 : tagEnd]
closingTag := "{/" + tag + "}"
closingTagPos := strings.Index(line[tagEnd+1:], closingTag)
if closingTagPos == -1 {
previewLine.WriteString(line[i:])
rawLine.WriteString(line[i:])
break
}
closingTagPos += tagEnd + 1
content := line[tagEnd+1 : closingTagPos]
style := lipgloss.NewStyle().
Renderer(&r).
Foreground(lipgloss.Color(theme.getColor(tag))).
Background(lipgloss.Color(*theme.Background))
previewLine.WriteString(style.Render(content))
rawLine.WriteString(content)
i = closingTagPos + len(closingTag)
} else {
previewLine.WriteString(baseStyle.Render(line[i : i+1]))
rawLine.WriteString(line[i : i+1])
i++
}
}
if len(rawLine.String()) < maxPreviewWidth {
padding := strings.Repeat(spaceChar, maxPreviewWidth-len(rawLine.String()))
previewLine.WriteString(baseStyle.Render(padding))
}
result.WriteString(previewLine.String())
result.WriteString("\n")
}
return result.String()
}