chinampa/pkg/render/render.go

89 lines
2.1 KiB
Go
Raw Permalink Normal View History

2022-12-19 03:04:34 +00:00
// Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx>
2022-12-31 05:53:24 +00:00
// SPDX-License-Identifier: Apache-2.0
2022-12-19 03:04:34 +00:00
package render
import (
"bytes"
"os"
2022-12-31 05:53:24 +00:00
"strings"
"text/template"
2022-12-19 03:04:34 +00:00
_c "git.rob.mx/nidito/chinampa/internal/constants"
2022-12-31 05:53:24 +00:00
"git.rob.mx/nidito/chinampa/pkg/env"
2022-12-19 03:04:34 +00:00
"git.rob.mx/nidito/chinampa/pkg/runtime"
"github.com/charmbracelet/glamour"
"github.com/sirupsen/logrus"
"golang.org/x/term"
)
func addBackticks(str []byte) []byte {
return bytes.ReplaceAll(str, []byte("﹅"), []byte("`"))
}
func Markdown(content []byte, withColor bool) ([]byte, error) {
content = addBackticks(content)
if runtime.UnstyledHelpEnabled() {
return content, nil
}
width, _, err := term.GetSize(0)
if err != nil {
logrus.Debugf("Could not get terminal width")
width = 80
}
var styleFunc glamour.TermRendererOption
if withColor {
2022-12-31 05:53:24 +00:00
style := os.Getenv(env.HelpStyle)
2022-12-19 03:04:34 +00:00
switch style {
case "dark":
styleFunc = glamour.WithStandardStyle("dark")
case "light":
styleFunc = glamour.WithStandardStyle("light")
default:
styleFunc = glamour.WithStandardStyle("auto")
}
} else {
styleFunc = glamour.WithStandardStyle("notty")
}
renderer, err := glamour.NewTermRenderer(
styleFunc,
glamour.WithEmoji(),
glamour.WithWordWrap(width),
)
if err != nil {
return content, err
}
return renderer.RenderBytes(content)
}
2022-12-31 05:53:24 +00:00
// TemplateFuncs is a FuncMap with aliases to the strings package.
var TemplateFuncs = template.FuncMap{
"contains": strings.Contains,
"hasSuffix": strings.HasSuffix,
"hasPrefix": strings.HasPrefix,
"replace": strings.ReplaceAll,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"trim": strings.TrimSpace,
"trimSuffix": strings.TrimSuffix,
"trimPrefix": strings.TrimPrefix,
2023-01-23 04:29:18 +00:00
"list": func(args ...any) []any { return args },
2022-12-31 05:53:24 +00:00
}
// TemplateCommandHelp holds a template for rendering command help.
var TemplateCommandHelp *template.Template
func HelpTemplate(executableName string) *template.Template {
if TemplateCommandHelp == nil {
TemplateCommandHelp = template.Must(template.New("help").Funcs(TemplateFuncs).Parse(strings.ReplaceAll(_c.HelpTemplate, "@chinampa@", executableName)))
}
return TemplateCommandHelp
}