Skip to content

Commit 4e23f35

Browse files
committed
syntax
1 parent 0a2dcc2 commit 4e23f35

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

cmd/utils.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -95,49 +95,48 @@ func syntaxHighlight(message string) {
9595
yellow := "\033[33m" // Yellow color ANSI escape code
9696
reset := "\033[0m" // Reset ANSI escape code
9797

98+
processLine := func(line string) string {
99+
line = backtickRegex.ReplaceAllStringFunc(line, func(match string) string {
100+
return cyan + strings.Trim(match, "`") + reset
101+
})
102+
line = doubleQuoteRegex.ReplaceAllStringFunc(line, func(match string) string {
103+
return yellow + match + reset
104+
})
105+
return line
106+
}
107+
98108
for _, line := range lines {
99-
if strings.HasPrefix(strings.TrimSpace(line), "```") {
109+
trimmedLine := strings.TrimSpace(line)
110+
if strings.HasPrefix(trimmedLine, "```") {
100111
if inCodeBlock {
101-
// Ending a code block, apply syntax highlighting
102112
iterator, err := currentLexer.Tokenise(nil, codeBuffer.String())
103113
if err == nil {
104114
formatter.Format(os.Stdout, style, iterator)
105115
}
106-
fmt.Println() // Ensure there's a newline after the code block
116+
fmt.Println()
107117
codeBuffer.Reset()
108118
inCodeBlock = false
109119
} else {
110-
// Starting a code block
111120
inCodeBlock = true
112-
lang := strings.TrimPrefix(strings.TrimSpace(line), "```")
121+
lang := strings.TrimPrefix(trimmedLine, "```")
113122
currentLexer = lexers.Get(lang)
114123
if currentLexer == nil {
115124
currentLexer = lexers.Fallback
116125
}
117-
continue // Skip the line with opening backticks
118126
}
119127
} else if inCodeBlock {
120-
codeBuffer.WriteString(line + "\n") // Collect code lines
128+
codeBuffer.WriteString(line + "\n")
121129
} else {
122-
// Process and set colors
123-
processedLine := line
124-
processedLine = backtickRegex.ReplaceAllStringFunc(processedLine, func(match string) string {
125-
return cyan + strings.Trim(match, "`") + reset
126-
})
127-
processedLine = doubleQuoteRegex.ReplaceAllStringFunc(processedLine, func(match string) string {
128-
return yellow + match + reset
129-
})
130-
fmt.Println(" " + processedLine) // Print with white color
130+
fmt.Println(" " + processLine(line))
131131
}
132132
}
133133

134-
// Flush the remaining content if still in a code block
135134
if inCodeBlock {
136135
iterator, err := currentLexer.Tokenise(nil, codeBuffer.String())
137136
if err == nil {
138137
formatter.Format(os.Stdout, style, iterator)
139138
}
140-
fmt.Println() // Ensure there's a newline after the code block
139+
fmt.Println()
141140
}
142141
}
143142

0 commit comments

Comments
 (0)