Some checks failed
Release / build (push) Failing after 22s
- 使用 ProcessDirect 替代 ChatStream,支持工具调用结果显示 - 新增 project.config.yml 统一配置(Logo、用户前缀、流式延迟、Markdown等) - Markdown 渲染支持自动终端宽度换行 - 按行输出文本,每行延迟可配置 - 简化状态栏,只显示耗时(图标颜色 #f0c75e,文字颜色 #2b2e32) - spinner 动画右移两个字符 - 用户输入前缀可配置化
99 lines
1.5 KiB
Go
99 lines
1.5 KiB
Go
package internal
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"charm.land/glamour/v2"
|
|
"github.com/charmbracelet/x/term"
|
|
)
|
|
|
|
func RenderMarkdown(md string) string {
|
|
if md == "" {
|
|
return ""
|
|
}
|
|
|
|
style := getStyle()
|
|
wrapWidth := getWrapWidth()
|
|
|
|
r, err := glamour.NewTermRenderer(
|
|
glamour.WithStandardStyle(style),
|
|
glamour.WithWordWrap(wrapWidth),
|
|
)
|
|
if err != nil {
|
|
return md
|
|
}
|
|
defer r.Close()
|
|
|
|
out, err := r.Render(md)
|
|
if err != nil {
|
|
return md
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func RenderParagraph(text string) string {
|
|
if text == "" {
|
|
return ""
|
|
}
|
|
|
|
text = strings.TrimRight(text, "\n")
|
|
|
|
if text == "" {
|
|
return ""
|
|
}
|
|
|
|
style := getStyle()
|
|
wrapWidth := getWrapWidth()
|
|
|
|
r, err := glamour.NewTermRenderer(
|
|
glamour.WithStandardStyle(style),
|
|
glamour.WithWordWrap(wrapWidth),
|
|
)
|
|
if err != nil {
|
|
return text
|
|
}
|
|
defer r.Close()
|
|
|
|
out, err := r.Render(text)
|
|
if err != nil {
|
|
return text
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func getStyle() string {
|
|
if cfg := GetProjectConfig(); cfg != nil {
|
|
if cfg.Markdown.GlamourStyle != "" {
|
|
return cfg.Markdown.GlamourStyle
|
|
}
|
|
}
|
|
if s := os.Getenv("GLAMOUR_STYLE"); s != "" {
|
|
return s
|
|
}
|
|
return "dark"
|
|
}
|
|
|
|
func getWrapWidth() int {
|
|
if cfg := GetProjectConfig(); cfg != nil {
|
|
if cfg.Markdown.WrapWidth > 0 {
|
|
return cfg.Markdown.WrapWidth
|
|
}
|
|
}
|
|
|
|
if cols := os.Getenv("COLUMNS"); cols != "" {
|
|
if w, err := strconv.Atoi(cols); err == nil && w > 0 {
|
|
return w
|
|
}
|
|
}
|
|
|
|
width, _, err := term.GetSize(0)
|
|
if err != nil || width <= 0 {
|
|
return 80
|
|
}
|
|
return width
|
|
}
|