Skip to content

Commit

Permalink
👔 up(show): MList support struct and any map data for render
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 26, 2023
1 parent 21da8f9 commit 9dc5969
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
5 changes: 4 additions & 1 deletion show/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,14 @@ func (item *Item) IsEmpty() bool {
case reflect.Interface, reflect.Slice, reflect.Ptr:
return item.rftVal.IsNil()
}
return false
return !item.rftVal.IsValid()
}

// ValString get
func (item *Item) ValString() string {
if item.IsEmpty() {
return ""
}
return strutil.QuietString(item.rftVal.Interface())
}

Expand Down
65 changes: 43 additions & 22 deletions show/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/gookit/color"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/reflects"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/strutil"
)

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

// NewListOption instance
func NewListOption() *ListOption {
return &ListOption{
SepChar: " ",
KeyStyle: "info",
// more
LeftIndent: " ",
KeyMinWidth: 8,
IgnoreEmpty: true,
TitleStyle: "comment",
}
}

/*************************************************************
* List
*************************************************************/
Expand Down Expand Up @@ -59,15 +74,7 @@ func NewList(title string, data any, fns ...ListOpFunc) *List {
// base
Base: Base{out: Output},
// options
Opts: &ListOption{
SepChar: " ",
KeyStyle: "info",
LeftIndent: " ",
// more settings
KeyMinWidth: 8,
IgnoreEmpty: true,
TitleStyle: "comment",
},
Opts: NewListOption(),
}

return l.WithOptionFns(fns)
Expand Down Expand Up @@ -194,24 +201,32 @@ type Lists struct {
buffer *bytes.Buffer
}

// NewLists create lists
func NewLists(listMap map[string]any, fns ...ListOpFunc) *Lists {
// NewEmptyLists create empty lists
func NewEmptyLists(fns ...ListOpFunc) *Lists {
ls := &Lists{
Base: Base{out: Output},
Opts: &ListOption{
SepChar: " ",
KeyStyle: "info",
// more
LeftIndent: " ",
KeyMinWidth: 8,
IgnoreEmpty: true,
TitleStyle: "comment",
},
Opts: NewListOption(),
}
return ls.WithOptionFns(fns)
}

for title, data := range listMap {
ls.rows = append(ls.rows, NewList(title, data))
// NewLists create lists. allow: map[string]any, struct-ptr
func NewLists(mlist any, fns ...ListOpFunc) *Lists {
ls := NewEmptyLists()
rv := reflect.Indirect(reflect.ValueOf(mlist))

if rv.Kind() == reflect.Map {
reflects.EachStrAnyMap(rv, func(key string, val any) {
ls.AddSublist(key, val)
})
} else if rv.Kind() == reflect.Struct {
for title, data := range structs.ToMap(mlist) {
ls.rows = append(ls.rows, NewList(title, data))
}
} else {
panic("not support type: " + rv.Kind().String())
}

return ls.WithOptionFns(fns)
}

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

// AddSublist with options func list
func (ls *Lists) AddSublist(title string, data any) *Lists {
ls.rows = append(ls.rows, NewList(title, data))
return ls
}

// Format as string
func (ls *Lists) Format() {
if len(ls.rows) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func AList(title string, data any, fns ...ListOpFunc) {
// show.MList(data, func(opts *ListOption) {
// opts.LeftIndent = " "
// })
func MList(listMap map[string]any, fns ...ListOpFunc) {
func MList(listMap any, fns ...ListOpFunc) {
NewLists(listMap).WithOptionFns(fns).Println()
}

Expand Down

0 comments on commit 9dc5969

Please sign in to comment.