-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.go
64 lines (53 loc) · 1.24 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
package text
///////////////////////////////////////////////////////////////////////////////
// TYPES
type opts struct {
delim rune
format map[int]Format
}
// Format defines the format of a field
type Format struct {
// The maximum width of the field
Width int
// The alignment of the field (Left or Right)
Align Alignment
// Whether to wrap text
Wrap bool
}
// Opt is a function which can be used to set options on the text output
type Opt func(*opts) error
///////////////////////////////////////////////////////////////////////////////
// GLOBALS
const (
_ Alignment = iota
Left
Right
)
///////////////////////////////////////////////////////////////////////////////
// OPTIONS
// Set the text format for fields. If fields is omitted, then the format is
// set for all fields
func OptFormat(format Format, fields ...int) Opt {
return func(o *opts) error {
if o.format == nil {
o.format = make(map[int]Format)
}
if len(fields) == 0 {
// Set default format
o.format[-1] = format
}
for _, field := range fields {
if field >= 0 {
o.format[field] = format
}
}
return nil
}
}
// Set the field delimiter, default is '|'
func OptDelim(delim rune) Opt {
return func(o *opts) error {
o.delim = delim
return nil
}
}