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"
|
2023-03-24 01:51:36 +00:00
|
|
|
"git.rob.mx/nidito/chinampa/pkg/logger"
|
2022-12-19 03:04:34 +00:00
|
|
|
"github.com/charmbracelet/glamour"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
func addBackticks(str []byte) []byte {
|
|
|
|
return bytes.ReplaceAll(str, []byte("﹅"), []byte("`"))
|
|
|
|
}
|
|
|
|
|
2023-03-21 06:28:25 +00:00
|
|
|
// Markdown renders markdown-formatted content to the tty.
|
2022-12-19 03:04:34 +00:00
|
|
|
func Markdown(content []byte, withColor bool) ([]byte, error) {
|
|
|
|
content = addBackticks(content)
|
2023-03-24 01:51:36 +00:00
|
|
|
var styleFunc glamour.TermRendererOption
|
2022-12-19 03:04:34 +00:00
|
|
|
|
2023-03-24 01:51:36 +00:00
|
|
|
style := os.Getenv(env.HelpStyle)
|
|
|
|
if style == "markdown" {
|
|
|
|
// markdown will render frontmatter along content and will not format for
|
|
|
|
// tty readability
|
2022-12-19 03:04:34 +00:00
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
if withColor {
|
|
|
|
switch style {
|
|
|
|
case "dark":
|
2023-03-24 01:51:36 +00:00
|
|
|
// For color TTYs with light text on dark background
|
2022-12-19 03:04:34 +00:00
|
|
|
styleFunc = glamour.WithStandardStyle("dark")
|
|
|
|
case "light":
|
2023-03-24 01:51:36 +00:00
|
|
|
// For color TTYs with dark text on light background
|
2022-12-19 03:04:34 +00:00
|
|
|
styleFunc = glamour.WithStandardStyle("light")
|
|
|
|
default:
|
2023-03-24 01:51:36 +00:00
|
|
|
// Glamour selects a style for the user.
|
2022-12-19 03:04:34 +00:00
|
|
|
styleFunc = glamour.WithStandardStyle("auto")
|
2024-06-22 01:38:29 +00:00
|
|
|
if style != "auto" {
|
2023-03-24 01:51:36 +00:00
|
|
|
logger.Warnf("Unknown %s=%s, assuming \"auto\"", env.HelpStyle, style)
|
|
|
|
}
|
2022-12-19 03:04:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-24 01:51:36 +00:00
|
|
|
// basically the same as the "markdown" style, except formatted for
|
|
|
|
// tty redability, prettifying and indenting, while not adding color.
|
2022-12-19 03:04:34 +00:00
|
|
|
styleFunc = glamour.WithStandardStyle("notty")
|
|
|
|
}
|
|
|
|
|
2023-03-24 01:51:36 +00:00
|
|
|
width, _, err := term.GetSize(0)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Could not get terminal width")
|
|
|
|
width = 80
|
|
|
|
}
|
|
|
|
|
2022-12-19 03:04:34 +00:00
|
|
|
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
|
|
|
|
}
|