~emersion/drmdb

dd8a22df15b31f52bfb357ec4cd0f2baac43660b — Simon Ser 2 years ago f944b80
drmtree: pretty-print prop spec
2 files changed, 79 insertions(+), 1 deletions(-)

M drmtree/drmtree.go
M public/assets/style.css
M drmtree/drmtree.go => drmtree/drmtree.go +78 -1
@@ 4,6 4,7 @@ package drmtree
import (
	"encoding/json"
	"fmt"
	"math"
	"sort"
	"strings"



@@ 335,6 336,39 @@ func (m *PropertyMap) UnmarshalJSON(b []byte) error {
	return nil
}

type prettyUint64 uint64

func (u prettyUint64) String() string {
	switch uint64(u) {
	case math.MaxUint64:
		return "UINT64_MAX"
	case math.MaxUint32:
		return "UINT32_MAX"
	case math.MaxInt64:
		return "INT64_MAX"
	case math.MaxInt32:
		return "INT32_MAX"
	default:
		return fmt.Sprintf("%v", uint64(u))
	}
}

type prettyInt64 int64

func (i prettyInt64) String() string {
	if i > 0 {
		return prettyUint64(i).String()
	}
	switch int64(i) {
	case math.MinInt64:
		return "INT64_MIN"
	case math.MinInt32:
		return "INT32_MIN"
	default:
		return fmt.Sprintf("%v", int64(i))
	}
}

func (m *PropertyMap) FormatTree(tf treefmt.Formatter) {
	names := make([]string, 0, len(*m))
	for name := range *m {


@@ 345,7 379,7 @@ func (m *PropertyMap) FormatTree(tf treefmt.Formatter) {
	for _, name := range names {
		prop := (*m)[name]

		s := fmt.Sprintf("%q: %v", name, prop.Type)
		s := fmt.Sprintf("%q", name)

		var attrs []string
		if prop.Atomic {


@@ 358,6 392,27 @@ func (m *PropertyMap) FormatTree(tf treefmt.Formatter) {
			s += " (" + strings.Join(attrs, ", ") +")"
		}

		s += ": " + prop.Type.String()

		var specStr string
		switch spec := prop.Spec.(type) {
		case *PropertySpecRange:
			specStr = fmt.Sprintf("[%v, %v]", prettyUint64(spec.Min), prettyUint64(spec.Max))
		case []PropertySpecEnum:
			var names []string
			for _, e := range spec {
				names = append(names, e.Name)
			}
			specStr = "{" + strings.Join(names, ", ") + "}"
		case drm.ObjectType:
			specStr = spec.String()
		case *PropertySpecSignedRange:
			specStr = fmt.Sprintf("[%v, %v]", prettyInt64(spec.Min), prettyInt64(spec.Max))
		}
		if specStr != "" {
			s += " " + specStr
		}

		val := prop.Data
		if val == nil {
			val = prop.Value


@@ 380,6 435,28 @@ func (m *PropertyMap) FormatTree(tf treefmt.Formatter) {
				tfc.Printf("%v", fmt)
			}
		default:
			switch prop.Type {
			case drm.PropertyEnum:
				u := val.(uint64)
				entries := prop.Spec.([]PropertySpecEnum)
				val = "<invalid>"
				for _, e := range entries {
					if e.Value == u {
						val = e.Name
						break
					}
				}
			case drm.PropertyBitmask:
				u := val.(uint64)
				entries := prop.Spec.([]PropertySpecEnum)
				var names []string
				for _, e := range entries {
					if e.Value&u == e.Value {
						names = append(names, e.Name)
					}
				}
				val = "(" + strings.Join(names, " | ") + ")"
			}
			tf.Printf("%v = %v", s, val)
		}
	}

M public/assets/style.css => public/assets/style.css +1 -0
@@ 59,6 59,7 @@ td.pre {

ul.pre {
    font-family: monospace;
    max-width: 750px;
}

ul.pre, ul.pre ul {