forked from jedib0t/go-pretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.go
109 lines (98 loc) · 2.79 KB
/
demo.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
package main
import (
"fmt"
"github.com/jedib0t/go-pretty/list"
"github.com/jedib0t/go-pretty/table"
"github.com/jedib0t/go-pretty/text"
)
// demoList demonstrates the capabilities of Writer with some of the
// available ready-to-use styles.
func demoList() string {
styles := []list.Style{
list.StyleDefault,
list.StyleBulletCircle,
list.StyleConnectedLight,
}
itemLevel1 := "Game Of Thrones"
itemsLevel2 := []interface{}{"Winter", "Is", "Coming"}
itemsLevel3 := []interface{}{"This", "Is", "Known"}
var header, content []interface{}
for _, style := range styles {
lw := list.NewWriter()
lw.AppendItem(itemLevel1)
lw.Indent()
lw.AppendItems(itemsLevel2)
lw.Indent()
lw.AppendItems(itemsLevel3)
lw.SetStyle(style)
header = append(header, style.Name)
content = append(content, lw.Render())
}
tw := table.NewWriter()
tw.AppendHeader(header)
tw.AppendRow(content)
tw.SetStyle(table.StyleLight)
tw.Style().Format.Header = text.FormatDefault
return tw.Render()
}
// demoTable demonstrates the capabilities of Writer with some of the
// available ready-to-use styles.
func demoTable() string {
styles := []table.Style{
table.StyleDefault,
table.StyleRounded,
table.StyleBold,
table.StyleColoredBright,
table.StyleColoredDark,
}
header := table.Row{"#", "First Name", "Last Name", "Salary"}
rows1And2 := []table.Row{
{1, "Arya", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
}
row3 := table.Row{300, "Tyrion", "Lannister", 5000}
footer := table.Row{"", "", "Total", 10000}
align := []text.Align{
text.AlignDefault,
text.AlignLeft,
text.AlignLeft,
text.AlignRight,
}
var content []table.Row
for _, style := range styles {
tw := table.NewWriter()
tw.AppendHeader(header)
tw.AppendRows(rows1And2)
tw.AppendRow(row3)
tw.AppendFooter(footer)
tw.SetAlign(align)
tw.SetIndexColumn(1)
tw.SetStyle(style)
if len(content) == 0 {
content = append(content, table.Row{"As CSV", tw.RenderCSV()})
}
content = append(content, table.Row{style.Name, tw.Render()})
}
tw := table.NewWriter()
tw.AppendRows(content)
tw.SetAlign([]text.Align{text.AlignRight, text.AlignLeft})
tw.SetStyle(table.StyleColoredBright)
tw.Style().Options.SeparateRows = false
tw.SetVAlign([]text.VAlign{text.VAlignMiddle, text.VAlignDefault})
return tw.Render()
}
func main() {
demo := table.NewWriter()
demo.AppendHeader([]interface{}{"#", "Feature", "Samples"})
demo.AppendRow(table.Row{"1", "List", demoList()})
demo.AppendRow(table.Row{"2", "Table", demoTable()})
demo.SetAlign([]text.Align{
text.AlignDefault,
text.AlignLeft,
text.AlignCenter,
})
demo.SetCaption("Generated with go-pretty; MIT License; Copyright (c) 2018 jedib0t.")
demo.SetStyle(table.StyleDouble)
demo.Style().Options.SeparateRows = true
fmt.Println(demo.Render())
}