~emersion/drmdb

13f521baaa73d117245a5d187a5231a3f0a18415 — Simon Ser 2 years ago 0b3ed7e
Add colors to formats page
3 files changed, 44 insertions(+), 17 deletions(-)

M public/formats.html
M server.go
A template.go
M public/formats.html => public/formats.html +6 -2
@@ 6,6 6,10 @@

<h2 id="in-formats">Input formats</h2>

{{define "pct-cell"}}
    <td style="color: {{pctColor .}};">{{printf "%.0f%%" .}}</td>
{{end}}

<table>
    <thead>
        <tr>


@@ 30,10 34,10 @@
            <td class="pre">{{.Modifier}}</td>
            <td class="pre">{{.Format}}</td>
            {{range $plane, $_ := $.Planes}}
            <td>{{pct (index $row.Planes $plane) $.Total}}</td>
            {{template "pct-cell" (pct (index $row.Planes $plane) $.Total)}}
            {{end}}
            {{range $drv, $_ := $.Drivers}}
            <td>{{pct (index $row.Drivers $drv) $.Total}}</td>
            {{template "pct-cell" (pct (index $row.Drivers $drv) $.Total)}}
            {{end}}
        </tr>
        {{end}}

M server.go => server.go +4 -15
@@ 3,7 3,6 @@ package drmdb
import (
	"encoding/json"
	"fmt"
	"html/template"
	"io"
	"net/http"
	"os"


@@ 18,14 17,6 @@ import (
	"github.com/mcuadros/go-version"
)

type Template struct {
	t *template.Template
}

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

func badRequest(c echo.Context, msg string, err error) error {
	if err != nil {
		c.Logger().Printf("%v: %v", msg, err)


@@ 161,12 152,10 @@ func New() *echo.Echo {
		e.Logger.Fatal("Failed to create DB dir:", err)
	}

	e.Renderer = &Template{
		t: template.Must(template.New("drmdb").Funcs(template.FuncMap{
			"pct": func (num, tot int) string {
				return fmt.Sprintf("%v%%", int(100 * float32(num) / float32(tot)))
			},
		}).ParseGlob("public/*.html")),
	var err error
	e.Renderer, err = loadTemplates()
	if err != nil {
		e.Logger.Fatal("Failed to load templates:", err)
	}

	pciVendors, pciDevices, err := loadPCIIDs()

A template.go => template.go +34 -0
@@ 0,0 1,34 @@
package drmdb

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

	"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{
		"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))
		},
	}).ParseGlob("public/*.html")

	return &tmpl{t}, err
}