This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
format.go
121 lines (105 loc) · 2.38 KB
/
format.go
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"io"
"io/ioutil"
"strings"
)
type Format int
const (
Human Format = iota
Json
JsonPP
Raw
)
type Humanize func(input io.Reader) string
type Formatter struct {
format Format
}
func NewFormatter(f string) Formatter {
switch f {
case "jsonpp":
return Formatter{JsonPP}
case "json":
return Formatter{Json}
case "raw":
return Formatter{Raw}
default:
return Formatter{Human}
}
}
func (f Formatter) Format(input io.Reader, h Humanize) string {
switch f.format {
case JsonPP:
return f.JsonPPize(input)
case Json:
return f.Jsonize(input)
case Raw:
return f.Raw(input)
default:
return h(input)
}
}
// Jsonize returns raw response on one line with no extra space.
func (f Formatter) Jsonize(input io.Reader) string {
var s bytes.Buffer
b, e := ioutil.ReadAll(input)
Check(e == nil, "failed to read input", e)
json.Compact(&s, b)
return s.String()
}
// JsonPPize takes the raw response and adds newlines and indentations.
func (f Formatter) JsonPPize(input io.Reader) string {
var s bytes.Buffer
b, e := ioutil.ReadAll(input)
Check(e == nil, "failed to read input", e)
json.Indent(&s, b, "", " ")
return s.String()
}
// Raw returns the raw input unmodified.
func (f Formatter) Raw(input io.Reader) string {
b, e := ioutil.ReadAll(input)
Check(e == nil, "failed to read input", e)
return string(b)
}
const maxCols = 100
// Columnize will pretty print columns of information
// rows are by newline
// columns are by whitespace
func Columnize(text string) string {
scanner := bufio.NewScanner(strings.NewReader(text))
all := [][]string{}
longests := [maxCols]int{} // index=col, val=maxlength
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
for i, field := range fields {
if len(field) > longests[i] {
longests[i] = len(field)
}
}
all = append(all, fields)
}
e := scanner.Err()
Check(e == nil, "scanner error", e)
return strings.TrimSpace(fmtFields(longests, all))
}
func fmtFields(longests [maxCols]int, matrix [][]string) string {
var b bytes.Buffer
for _, fields := range matrix {
for col, field := range fields {
b.WriteString(pad(longests[col], field))
}
b.WriteString("\n")
}
return b.String()
}
func pad(length int, text string) string {
var b bytes.Buffer
b.WriteString(text)
for i := 0; i < (2+length)-len(text); i++ {
b.WriteString(" ")
}
return b.String()
}