File: expvarIndentMap.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 836 kB
  • sloc: makefile: 4
file content (35 lines) | stat: -rw-r--r-- 536 bytes parent folder | download
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
package missinggo

import (
	"bytes"
	"expvar"
	"fmt"
)

type IndentMap struct {
	expvar.Map
}

var _ expvar.Var = (*IndentMap)(nil)

func NewExpvarIndentMap(name string) *IndentMap {
	v := new(IndentMap)
	v.Init()
	expvar.Publish(name, v)
	return v
}

func (v *IndentMap) String() string {
	var b bytes.Buffer
	fmt.Fprintf(&b, "{")
	first := true
	v.Do(func(kv expvar.KeyValue) {
		if !first {
			fmt.Fprintf(&b, ",")
		}
		fmt.Fprintf(&b, "\n\t%q: %v", kv.Key, kv.Value)
		first = false
	})
	fmt.Fprintf(&b, "}")
	return b.String()
}