-
Notifications
You must be signed in to change notification settings - Fork 62
/
tableprinter_options.go
54 lines (47 loc) · 1.65 KB
/
tableprinter_options.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
package tableprinter
import "fmt"
// TablePrinterOption is a type of func(*TablePrinter).
type TablePrinterOption func(*TablePrinter) error
// WithOutputFormat returns a function func(opts *TablePrinter)
// that sets `format` in TablePrinter pointer instance.
func WithOutputFormat(format TableOutputFormat) TablePrinterOption {
return func(opts *TablePrinter) error {
opts.format = format
return nil
}
}
// WithOutputFormatFromString returns a function func(opts *TablePrinter)
// that sets `format` in TablePrinter pointer instance of type `TableOutputFormat` from string.
func WithOutputFormatFromString(format string) TablePrinterOption {
return func(opts *TablePrinter) error {
if format == "" {
return fmt.Errorf("unsupported table printer format: %s", format)
}
opts.format = TableOutputFormat(format)
return nil
}
}
// WithTableDelimeter returns a function func(opts *TablePrinter)
// that sets `delimeter` in TablePrinter pointer instance.
func WithTableDelimeter(delim string) TablePrinterOption {
return func(opts *TablePrinter) error {
opts.delimeter = delim
return nil
}
}
// WithFieldTruncateFunc returns a function func(opts *TablePrinter)
// that sets `truncateFunc` in TablePrinter pointer instance.
func WithFieldTruncateFunc(truncateFunc func(int, string) string) TablePrinterOption {
return func(opts *TablePrinter) error {
opts.truncateFunc = truncateFunc
return nil
}
}
// WithMaxWidth returns a function func(opts *TablePrinter)
// that sets `maxWidth` in TablePrinter pointer instance.
func WithMaxWidth(maxWidth int) TablePrinterOption {
return func(opts *TablePrinter) error {
opts.maxWidth = maxWidth
return nil
}
}