Skip to content

Commit 9dc5969

Browse files
committed
👔 up(show): MList support struct and any map data for render
1 parent 21da8f9 commit 9dc5969

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

show/base.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,14 @@ func (item *Item) IsEmpty() bool {
270270
case reflect.Interface, reflect.Slice, reflect.Ptr:
271271
return item.rftVal.IsNil()
272272
}
273-
return false
273+
return !item.rftVal.IsValid()
274274
}
275275

276276
// ValString get
277277
func (item *Item) ValString() string {
278+
if item.IsEmpty() {
279+
return ""
280+
}
278281
return strutil.QuietString(item.rftVal.Interface())
279282
}
280283

show/list.go

+43-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/gookit/color"
88
"github.com/gookit/goutil/arrutil"
99
"github.com/gookit/goutil/maputil"
10+
"github.com/gookit/goutil/reflects"
11+
"github.com/gookit/goutil/structs"
1012
"github.com/gookit/goutil/strutil"
1113
)
1214

@@ -28,6 +30,19 @@ type ListOption struct {
2830
// ListOpFunc define
2931
type ListOpFunc func(opts *ListOption)
3032

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+
3146
/*************************************************************
3247
* List
3348
*************************************************************/
@@ -59,15 +74,7 @@ func NewList(title string, data any, fns ...ListOpFunc) *List {
5974
// base
6075
Base: Base{out: Output},
6176
// 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(),
7178
}
7279

7380
return l.WithOptionFns(fns)
@@ -194,24 +201,32 @@ type Lists struct {
194201
buffer *bytes.Buffer
195202
}
196203

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 {
199206
ls := &Lists{
200207
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(),
210209
}
210+
return ls.WithOptionFns(fns)
211+
}
211212

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())
214228
}
229+
215230
return ls.WithOptionFns(fns)
216231
}
217232

@@ -228,6 +243,12 @@ func (ls *Lists) WithOptions(fns ...ListOpFunc) *Lists {
228243
return ls.WithOptionFns(fns)
229244
}
230245

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+
231252
// Format as string
232253
func (ls *Lists) Format() {
233254
if len(ls.rows) == 0 {

show/show.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func AList(title string, data any, fns ...ListOpFunc) {
7373
// show.MList(data, func(opts *ListOption) {
7474
// opts.LeftIndent = " "
7575
// })
76-
func MList(listMap map[string]any, fns ...ListOpFunc) {
76+
func MList(listMap any, fns ...ListOpFunc) {
7777
NewLists(listMap).WithOptionFns(fns).Println()
7878
}
7979

0 commit comments

Comments
 (0)