~emersion/drmdb

ref: 09fb82de3ecc5a42a61fbb6bf71029d6a45c8e8a drmdb/template.go -rw-r--r-- 951 bytes
09fb82deSimon Ser Fix format/modifier pretty-printing 5 months ago
                                                                                
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
package drmdb

import (
	"fmt"
	"html/template"
	"io"
	"strings"

	"github.com/labstack/echo/v4"
	"git.sr.ht/~emersion/go-drm"
)

type tmpl struct {
	t *template.Template
}

var _ = fmt.Printf

func (t *tmpl) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
	return t.t.ExecuteTemplate(w, name, data)
}

func loadTemplates() (*tmpl, error) {
	t, err := template.New("drmdb").Funcs(template.FuncMap{
		"join": strings.Join,
		"pct": func(num, tot int) float32 {
			return 100 * float32(num) / float32(tot)
		},
		"pctColor": func(pct float32) template.CSS {
			f := float32(pct) / 100
			hue := int(f * 120)
			return template.CSS(fmt.Sprintf("hsl(%d, 100%%, 30%%)", hue))
		},
		"drmFormatHex": func(f drm.Format) string {
			return fmt.Sprintf("0x%X", uint32(f))
		},
		"drmModifierHex": func(mod drm.Modifier) string {
			return fmt.Sprintf("0x%X", uint64(mod))
		},
	}).ParseGlob("public/*.html")

	return &tmpl{t}, err
}