-
Notifications
You must be signed in to change notification settings - Fork 240
/
presenter.go
128 lines (102 loc) · 2.57 KB
/
presenter.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
122
123
124
125
126
127
128
package presenters
import (
"encoding/json"
"fmt"
"io"
"github.com/logrusorgru/aurora"
"github.com/olekukonko/tablewriter"
)
// Presentable - Records (and field names) which may be presented by a Presenter
type Presentable interface {
FieldNames() []string
Records() []map[string]string
APIStruct() interface{}
}
// Presenter - A self managing presenter which can be rendered in multiple ways
type Presenter struct {
Item Presentable
Out io.Writer
Opts Options
}
// Options - Presenter options
type Options struct {
Vertical bool
HideHeader bool
Title string
AsJSON bool
}
// Render - Renders a presenter as a field list or table
func (p *Presenter) Render() error {
if p.Opts.AsJSON {
return p.renderJSON()
}
if p.Opts.Vertical {
return p.renderFieldList()
}
return p.renderTable()
}
func (p *Presenter) renderTable() error {
if p.Opts.Title != "" {
fmt.Fprintln(p.Out, aurora.Bold(p.Opts.Title))
}
table := tablewriter.NewWriter(p.Out)
cols := p.Item.FieldNames()
if !p.Opts.HideHeader {
table.SetHeader(cols)
}
table.SetBorder(false)
table.SetHeaderLine(false)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetColumnSeparator(" ")
table.SetNoWhiteSpace(true)
table.SetTablePadding(" ") // pad with tabs
for _, kv := range p.Item.Records() {
fields := []string{}
for _, col := range cols {
fields = append(fields, kv[col])
}
table.Append(fields)
}
table.Render()
fmt.Fprintln(p.Out)
return nil
}
func (p *Presenter) renderFieldList() error {
table := tablewriter.NewWriter(p.Out)
if p.Opts.Title != "" {
fmt.Fprintln(p.Out, aurora.Bold(p.Opts.Title))
}
cols := p.Item.FieldNames()
table.SetBorder(false)
table.SetAutoWrapText(false)
table.SetColumnSeparator("=")
table.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_LEFT})
for _, kv := range p.Item.Records() {
for _, col := range cols {
table.Append([]string{col, kv[col]})
}
table.Render()
fmt.Fprintln(p.Out)
}
return nil
}
func (p *Presenter) renderJSON() error {
data := p.Item.APIStruct()
if data == nil {
return fmt.Errorf("JSON output not available")
}
if p.Opts.Title == "" {
prettyJSON, err := json.MarshalIndent(data, "", " ")
fmt.Fprintln(p.Out, string(prettyJSON))
return err
} else {
wrapper := make(map[string]interface{}, 1)
wrapper[p.Opts.Title] = p.Item.APIStruct()
prettyJSON, err := json.MarshalIndent(wrapper, "", " ")
fmt.Fprintln(p.Out, string(prettyJSON))
return err
}
}