-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathopt.go
87 lines (74 loc) · 1.67 KB
/
opt.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 ffmt
import (
"fmt"
"io"
"reflect"
)
type optional struct {
style style // Format style
depth int // Maximum recursion depth
opt option // Option
}
// NewOptional ffmt optional
func NewOptional(depth int, b style, opt option) *optional {
return &optional{
style: b,
opt: opt,
depth: depth,
}
}
func (s *optional) Fprint(w io.Writer, i ...interface{}) (int, error) {
return fmt.Fprint(w, s.Sprint(i...))
}
func (s *optional) Print(i ...interface{}) (int, error) {
return fmt.Print(s.Sprint(i...))
}
func (s *optional) Sprint(i ...interface{}) string {
switch len(i) {
case 0:
return ""
case 1:
buf := getBuilder()
defer putBuilder(buf)
sb := &format{
buf: buf,
filter: map[uintptr]bool{},
optional: *s,
}
sb.fmt(reflect.ValueOf(i[0]), 0)
sb.buf.WriteByte('\n')
ret := sb.buf.String()
if s.opt.IsCanRowSpan() {
return Align(ret)
}
return ret
default:
return s.Sprint(i)
}
}
type option uint32
// Formatted option
const (
_ option = 1 << (31 - iota)
CanDefaultString // can use .(fmt.Stringer)
CanFilterDuplicate // Filter duplicates
CanRowSpan // Fold line
)
func (t option) IsCanDefaultString() bool {
return (t & CanDefaultString) != 0
}
func (t option) IsCanFilterDuplicate() bool {
return (t & CanFilterDuplicate) != 0
}
func (t option) IsCanRowSpan() bool {
return (t & CanRowSpan) != 0
}
type style int
// Formatted style
const (
_ style = iota
StyleP // Display type and data
StylePuts // Display data
StylePrint // Display data; string without quotes
StylePjson // The json style display; Do not show private
)