File: format.go

package info (click to toggle)
golang-github-cli-go-gh 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 444 kB
  • sloc: makefile: 2
file content (144 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package jsonpretty implements a terminal pretty-printer for JSON.
package jsonpretty

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"strings"
)

const (
	colorDelim  = "\x1b[1;38m" // bright white
	colorKey    = "\x1b[1;34m" // bright blue
	colorNull   = "\x1b[36m"   // cyan
	colorString = "\x1b[32m"   // green
	colorBool   = "\x1b[33m"   // yellow
	colorReset  = "\x1b[m"
)

// Format reads JSON from r and writes a prettified version of it to w.
func Format(w io.Writer, r io.Reader, indent string, colorize bool) error {
	dec := json.NewDecoder(r)
	dec.UseNumber()

	c := func(ansi string) string {
		if !colorize {
			return ""
		}
		return ansi
	}

	var idx int
	var stack []json.Delim

	for {
		t, err := dec.Token()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}

		switch tt := t.(type) {
		case json.Delim:
			switch tt {
			case '{', '[':
				stack = append(stack, tt)
				idx = 0
				if _, err := fmt.Fprint(w, c(colorDelim), tt, c(colorReset)); err != nil {
					return err
				}
				if dec.More() {
					if _, err := fmt.Fprint(w, "\n", strings.Repeat(indent, len(stack))); err != nil {
						return err
					}
				}
				continue
			case '}', ']':
				stack = stack[:len(stack)-1]
				idx = 0
				if _, err := fmt.Fprint(w, c(colorDelim), tt, c(colorReset)); err != nil {
					return err
				}
			}
		default:
			b, err := marshalJSON(tt)
			if err != nil {
				return err
			}

			isKey := len(stack) > 0 && stack[len(stack)-1] == '{' && idx%2 == 0
			idx++

			var color string
			if isKey {
				color = colorKey
			} else if tt == nil {
				color = colorNull
			} else {
				switch t.(type) {
				case string:
					color = colorString
				case bool:
					color = colorBool
				}
			}

			if color != "" {
				if _, err := fmt.Fprint(w, c(color)); err != nil {
					return err
				}
			}
			if _, err := w.Write(b); err != nil {
				return err
			}
			if color != "" {
				if _, err := fmt.Fprint(w, c(colorReset)); err != nil {
					return err
				}
			}

			if isKey {
				if _, err := fmt.Fprint(w, c(colorDelim), ":", c(colorReset), " "); err != nil {
					return err
				}
				continue
			}
		}

		if dec.More() {
			if _, err := fmt.Fprint(w, c(colorDelim), ",", c(colorReset), "\n", strings.Repeat(indent, len(stack))); err != nil {
				return err
			}
		} else if len(stack) > 0 {
			if _, err := fmt.Fprint(w, "\n", strings.Repeat(indent, len(stack)-1)); err != nil {
				return err
			}
		} else {
			if _, err := fmt.Fprint(w, "\n"); err != nil {
				return err
			}
		}
	}

	return nil
}

// marshalJSON works like json.Marshal, but with HTML-escaping disabled.
func marshalJSON(v interface{}) ([]byte, error) {
	buf := bytes.Buffer{}
	enc := json.NewEncoder(&buf)
	enc.SetEscapeHTML(false)
	if err := enc.Encode(v); err != nil {
		return nil, err
	}
	bb := buf.Bytes()
	// omit trailing newline added by json.Encoder
	if len(bb) > 0 && bb[len(bb)-1] == '\n' {
		return bb[:len(bb)-1], nil
	}
	return bb, nil
}