-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.go
87 lines (73 loc) · 1.98 KB
/
opts.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
package tablewriter
///////////////////////////////////////////////////////////////////////////////
// TYPES
type opts struct {
tag string // Tag used to get additional struct metadata
delim rune // Delimiter used to separate fields
header bool // Whether to output a header
null string // How the nil value is represented in the output
timeLayout string // How time values are formatted in the output
format
}
// The output type
type format uint
// TableOpt is a function which can be used to set options on a table
type TableOpt func(*opts) error
///////////////////////////////////////////////////////////////////////////////
// CONSTANTS
const (
_ format = iota // Default output format
formatCSV // Output as CSV
formatText // Output as text
)
///////////////////////////////////////////////////////////////////////////////
// OPTIONS
// Set the struct field tag which is used to set table options, default is "json"
func OptHeader() TableOpt {
return func(o *opts) error {
o.header = true
return nil
}
}
// Set the struct field tag which is used to set table options, default is "json"
func OptTag(tag string) TableOpt {
return func(o *opts) error {
o.tag = tag
return nil
}
}
// Set the field delimiter, default is ',' for CSV and '|' for Text
func OptFieldDelim(delim rune) TableOpt {
return func(o *opts) error {
o.delim = delim
return nil
}
}
// Output as CSV
func OptOutputCSV() TableOpt {
return func(o *opts) error {
o.format = formatCSV
return nil
}
}
// Output as Text
func OptOutputText() TableOpt {
return func(o *opts) error {
o.format = formatText
return nil
}
}
// Set how the nil value is represented in the output, defaults to "<nil>"
func OptNull(v string) TableOpt {
return func(o *opts) error {
o.null = v
return nil
}
}
// Set how time values are formatted in the output
func OptTimeLayout(v string) TableOpt {
return func(o *opts) error {
o.timeLayout = v
return nil
}
}