7
7
"github.com/gookit/color"
8
8
"github.com/gookit/goutil/arrutil"
9
9
"github.com/gookit/goutil/maputil"
10
+ "github.com/gookit/goutil/reflects"
11
+ "github.com/gookit/goutil/structs"
10
12
"github.com/gookit/goutil/strutil"
11
13
)
12
14
@@ -28,6 +30,19 @@ type ListOption struct {
28
30
// ListOpFunc define
29
31
type ListOpFunc func (opts * ListOption )
30
32
33
+ // NewListOption instance
34
+ func NewListOption () * ListOption {
35
+ return & ListOption {
36
+ SepChar : " " ,
37
+ KeyStyle : "info" ,
38
+ // more
39
+ LeftIndent : " " ,
40
+ KeyMinWidth : 8 ,
41
+ IgnoreEmpty : true ,
42
+ TitleStyle : "comment" ,
43
+ }
44
+ }
45
+
31
46
/*************************************************************
32
47
* List
33
48
*************************************************************/
@@ -59,15 +74,7 @@ func NewList(title string, data any, fns ...ListOpFunc) *List {
59
74
// base
60
75
Base : Base {out : Output },
61
76
// options
62
- Opts : & ListOption {
63
- SepChar : " " ,
64
- KeyStyle : "info" ,
65
- LeftIndent : " " ,
66
- // more settings
67
- KeyMinWidth : 8 ,
68
- IgnoreEmpty : true ,
69
- TitleStyle : "comment" ,
70
- },
77
+ Opts : NewListOption (),
71
78
}
72
79
73
80
return l .WithOptionFns (fns )
@@ -194,24 +201,32 @@ type Lists struct {
194
201
buffer * bytes.Buffer
195
202
}
196
203
197
- // NewLists create lists
198
- func NewLists ( listMap map [ string ] any , fns ... ListOpFunc ) * Lists {
204
+ // NewEmptyLists create empty lists
205
+ func NewEmptyLists ( fns ... ListOpFunc ) * Lists {
199
206
ls := & Lists {
200
207
Base : Base {out : Output },
201
- Opts : & ListOption {
202
- SepChar : " " ,
203
- KeyStyle : "info" ,
204
- // more
205
- LeftIndent : " " ,
206
- KeyMinWidth : 8 ,
207
- IgnoreEmpty : true ,
208
- TitleStyle : "comment" ,
209
- },
208
+ Opts : NewListOption (),
210
209
}
210
+ return ls .WithOptionFns (fns )
211
+ }
211
212
212
- for title , data := range listMap {
213
- ls .rows = append (ls .rows , NewList (title , data ))
213
+ // NewLists create lists. allow: map[string]any, struct-ptr
214
+ func NewLists (mlist any , fns ... ListOpFunc ) * Lists {
215
+ ls := NewEmptyLists ()
216
+ rv := reflect .Indirect (reflect .ValueOf (mlist ))
217
+
218
+ if rv .Kind () == reflect .Map {
219
+ reflects .EachStrAnyMap (rv , func (key string , val any ) {
220
+ ls .AddSublist (key , val )
221
+ })
222
+ } else if rv .Kind () == reflect .Struct {
223
+ for title , data := range structs .ToMap (mlist ) {
224
+ ls .rows = append (ls .rows , NewList (title , data ))
225
+ }
226
+ } else {
227
+ panic ("not support type: " + rv .Kind ().String ())
214
228
}
229
+
215
230
return ls .WithOptionFns (fns )
216
231
}
217
232
@@ -228,6 +243,12 @@ func (ls *Lists) WithOptions(fns ...ListOpFunc) *Lists {
228
243
return ls .WithOptionFns (fns )
229
244
}
230
245
246
+ // AddSublist with options func list
247
+ func (ls * Lists ) AddSublist (title string , data any ) * Lists {
248
+ ls .rows = append (ls .rows , NewList (title , data ))
249
+ return ls
250
+ }
251
+
231
252
// Format as string
232
253
func (ls * Lists ) Format () {
233
254
if len (ls .rows ) == 0 {
0 commit comments