-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
49 lines (42 loc) · 1.09 KB
/
log.go
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Harry Randazzo
package vai
import (
"context"
"strings"
"github.com/alecthomas/chroma/v2/quick"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/muesli/termenv"
)
// very side effect heavy
// should rethink this
func printScript(ctx context.Context, prefix, script string) {
logger := log.FromContext(ctx)
script = strings.TrimSpace(script)
if termenv.EnvNoColor() {
for _, line := range strings.Split(script, "\n") {
logger.Printf("%s %s", prefix, line)
}
return
}
var buf strings.Builder
style := "catppuccin-latte"
if lipgloss.HasDarkBackground() {
style = "catppuccin-frappe"
}
lang := "shell"
if prefix == ">" {
lang = "go"
}
if err := quick.Highlight(&buf, script, lang, "terminal256", style); err != nil {
logger.Debugf("failed to highlight: %v", err)
for _, line := range strings.Split(script, "\n") {
logger.Printf("%s %s", prefix, line)
}
return
}
for _, line := range strings.Split(buf.String(), "\n") {
logger.Printf("%s %s", prefix, line)
}
}