Files
membank/auto-check/pkg/report/template.go
12600k-rog-d4 2aaaa16ae2 0813
2026-08-13 22:49:50 +08:00

65 lines
1.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package report
import (
"fmt"
"strings"
"text/template"
"time"
)
// ============================
// 模板渲染80mm 热敏纸适配)
// ============================
// TemplateConfig 模板渲染配置
type TemplateConfig struct {
Width int // 行宽字符数80mm 热敏纸建议 42
}
// DefaultTemplateConfig 默认配置80mm 热敏纸)
func DefaultTemplateConfig() TemplateConfig {
return TemplateConfig{Width: 42}
}
// RenderTemplate 使用 Go template 渲染报告
// tpl 为模板内容data 为报告数据
func RenderTemplate(tpl string, data interface{}, cfg TemplateConfig) (string, error) {
if cfg.Width <= 0 {
cfg.Width = 42
}
funcMap := template.FuncMap{
// 格式化辅助
"timeFmt": func(t time.Time) string { return t.Format("2006-01-02 15:04:05") },
// 字符串截断/填充到固定宽度
"trunc": func(s string, n int) string {
r := []rune(s)
if len(r) <= n {
return string(r)
}
return string(r[:n-1]) + "…"
},
// 左侧填充(右对齐)
"padRight": func(s string, n int) string {
r := []rune(s)
if len(r) >= n {
return string(r[:n])
}
return s + strings.Repeat(" ", n-len(r))
},
// 分隔线
"hr": func(sharp rune) string { return strings.Repeat(string(sharp), cfg.Width) },
}
t, err := template.New("report").Funcs(funcMap).Parse(tpl)
if err != nil {
return "", fmt.Errorf("解析模板失败: %w", err)
}
var sb strings.Builder
if err := t.Execute(&sb, data); err != nil {
return "", fmt.Errorf("渲染模板失败: %w", err)
}
return sb.String(), nil
}