~emersion/drmdb

ref: 8a09ed1498fe95ce875be0113d25cadd878829c8 drmdb/template.go -rw-r--r-- 1.1 KiB
8a09ed14Simon Ser Upgrade dependencies 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
44
45
46
47
48
49
50
51
52
package drmdb

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

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

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))
		},
		"bits": func(mask uint32) []int {
			var l []int
			for i := 0; i < 32; i++ {
				if mask&(uint32(1)<<uint(i)) != 0 {
					l = append(l, i)
				}
			}
			return l
		},
	}).ParseGlob("public/*.html")

	return &tmpl{t}, err
}