-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt.go
168 lines (153 loc) · 3.65 KB
/
fmt.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package streams
import "fmt"
const (
DefaultFirstLen = 5
DefaultLastLen = 5
DefaultMaxSkip = 1000
DefaultSkippedFormat = "..."
DefaultSeparator = ", "
)
type formatter[T any] struct {
stream Stream[T]
cfg formatterConfig
}
func (f *formatter[T]) Format(state fmt.State, verb rune) {
if _, err := fmt.Fprint(state, f.cfg.prefix); err != nil {
return
}
firstElement := true
first, skipped, last, hasRest := f.stream.CollectFirstLast(f.cfg.firstLen, f.cfg.lastLen, f.cfg.maxSkip)
fmtString := fmt.Sprintf("%%%c", verb)
if f.cfg.firstLen > 0 {
for i, v := range first {
if !firstElement {
if _, err := fmt.Fprint(state, f.cfg.separator); err != nil {
return
}
}
firstElement = false
if f.cfg.showIndex {
if _, err := fmt.Fprintf(state, "[%d]: ", i); err != nil {
return
}
}
if _, err := fmt.Fprintf(state, fmtString, v); err != nil {
return
}
}
}
if skipped > 0 && f.cfg.firstLen > 0 && f.cfg.lastLen > 0 {
if !firstElement {
if _, err := fmt.Fprint(state, f.cfg.separator); err != nil {
return
}
}
if f.cfg.showSkipped && f.cfg.skippedFormat != "" {
if _, err := fmt.Fprintf(state, f.cfg.skippedFormat, skipped); err != nil {
return
}
} else {
if _, err := fmt.Fprintf(state, f.cfg.skippedFormat); err != nil {
return
}
}
if f.cfg.skippedFormat != "" {
firstElement = false
}
}
if f.cfg.lastLen > 0 {
if skipped < 0 {
last = last[-skipped:]
}
idxOfs := len(first) + skipped
for i, v := range last {
if !firstElement {
if _, err := fmt.Fprint(state, f.cfg.separator); err != nil {
return
}
}
firstElement = false
if f.cfg.showIndex {
if _, err := fmt.Fprintf(state, "[%d]: ", i+idxOfs); err != nil {
return
}
}
if _, err := fmt.Fprintf(state, fmtString, v); err != nil {
return
}
}
if hasRest {
if !firstElement {
if _, err := fmt.Fprint(state, f.cfg.separator); err != nil {
return
}
}
if _, err := fmt.Fprint(state, "..."); err != nil {
return
}
}
}
if _, err := fmt.Fprint(state, f.cfg.suffix); err != nil {
return
}
}
type formatterConfig struct {
firstLen, lastLen, maxSkip int
showIndex, showSkipped bool
skippedFormat string
prefix, suffix, separator string
}
func FirstLen(len int) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.firstLen = len
}
}
func LastLen(len int) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.lastLen = len
}
}
func MaxSkip(skip int) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.maxSkip = skip
}
}
func ShowIndex(show bool) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.showIndex = show
}
}
func ShowSkipped(show bool, format string) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.showSkipped = show
f.skippedFormat = format
}
}
func Separator(s string) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.separator = s
}
}
func Prefix(s string) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.prefix = s
}
}
func Suffix(s string) func(cfg *formatterConfig) {
return func(f *formatterConfig) {
f.suffix = s
}
}
func Format[T any](s Stream[T], params ...func(*formatterConfig)) fmt.Formatter {
config := formatterConfig{
firstLen: DefaultFirstLen, lastLen: DefaultLastLen, maxSkip: DefaultMaxSkip, separator: DefaultSeparator,
skippedFormat: DefaultSkippedFormat,
}
for _, param := range params {
param(&config)
}
return &formatter[T]{s, config}
}
func (s Stream[T]) Format(params ...func(*formatterConfig)) fmt.Formatter {
return Format(s, params...)
}