Skip to content

Commit

Permalink
up: show - update some show util struct logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 3, 2022
1 parent 5cfe464 commit edde870
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 61 deletions.
2 changes: 1 addition & 1 deletion builtin/gen_auto_complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func doGen(c *gcli.Command, _ []string) (err error) {
"FileName": genOpts.output,
}

show.AList("Information", data, nil)
show.AList("Information", data)

if interact.Unconfirmed("Please confirm the above information", true) {
color.Info.Print("\nBye :)\n")
Expand Down
1 change: 1 addition & 0 deletions show/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func NewItems(data interface{}) *Items {
keyWidth = item.maxLen(keyWidth)
}
case reflect.Struct:
// structs.ToMap()
rt := rv.Type()
for i := 0; i < rt.NumField(); i++ {
ft := rt.Field(i)
Expand Down
96 changes: 69 additions & 27 deletions show/emoji/simple_emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package emoji

// some simple emoji chars
const (
ID = "🆔"
KEY = "🔑"
BOX = "📦"
GIFT = "🎁"
CLOCK = "⏰"
FLAG = "🚩"
TOOL = "🔧"
GUN = "🔫"
DING = "📌"
STOP = "🚫"
ID = "🆔"
Key = "🔑"
Box = "📦"
Gift = "🎁"
Flag = "🚩"
Tool = "🔧"
GUN = "🔫"
Ding = "📌"
Stop = "🚫"

DOC = "📄"
DIR = "📂"
Expand All @@ -38,33 +37,76 @@ const (
LEFT = "👈"
RIGHT = "👉"

TopArrow = "🔝"
BackArrow = "🔙"
SoonArrow = "🔜"

FIRE = "🔥"
SNOW = "❄"
WATER = "💧"
FLASH = "⚡"

EYE = "👀"
HEART = "💖"
HEARTBREAK = "💔"
Eye = "👀"

HeartStar = "💖"
HeartBreak = "💔"
HeartRed = "❤️"
HeartOrange = "🧡"
HeartYellow = "💛"
HeartGreen = "💚"

// SUC 🔝➕➖🎶✖️💲✔️☑️🔘🟢🟡🔵🟣🟠⚪️🟩🔲🔳
SUC = "✅"
FAIL = "❌"

// TickSGreen square green tick 方框绿色勾
TickSGreen = "✅"
// TickBlack black tick
TickBlack = "✔️"
// TickSBlack square black tick
TickSBlack = "☑️"
// BtnSingle 单选按钮
BtnSingle = "🔘"
BtnSquare = "🔲"

SUC = "✅"
FAIL = "❌"
WAN = "❗"
// CircleSRed red solid circle 红色实心圆圈
CircleSRed = "🔴"
// CircleSGreen green solid circle 绿色实心圆圈
CircleSGreen = "🟢"
// CircleSYellow yellow solid circle 绿色实心圆圈
CircleSYellow = "🟡"

Warning = "⚠️"
QUESTION = "❓"
// RedExcMark 红色感叹号
RedExcMark = "❗"
// RedExcMarkD red double exclamation mark 红色双感叹号
RedExcMarkD = "‼️"

// Points100 100分符号
Points100 = "💯"
// Recycling 回收标志
Recycling = "♻️"

Music1 = "🎵"
Music2 = "🎶"

Clock = "⏰"
Clock4 = "🕓"

CAR = "🚕"
Car = "🚕"

TREE = "🌲"
FLOWER = "🌺"
Tree = "🌲"
Flower = "🌺"

PEAR = "🍐"
APPLE = "🍎"
Pear = "🍐"
Apple = "🍎"

ELEPHANT = "🐘"
WHALE = "🐳"
Elephant = "🐘"
Whale = "🐳"

SUN = "🌞"
STAR = "⭐"
MOON = "🌜"
EARTH = "🌏"
Sun = "🌞"
Star = "⭐"
Moon = "🌜"
Earth = "🌏"
)
50 changes: 36 additions & 14 deletions show/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ type ListOption struct {
UpperFirst bool
SepChar string // split key value
LeftIndent string
KeyWidth int // if not set, will auto detect.
KeyWidth int // if not set, will be auto-detected.
KeyMinWidth int
KeyStyle string
ValueStyle string
TitleStyle string
}

// ListOpFunc define
type ListOpFunc func(opts *ListOption)

/*************************************************************
* List
*************************************************************/

// List definition
//
// String len:
// len("你好"), len("hello"), len("hello你好") -> 6 5 11
//
// len("你好"), len("hello"), len("hello你好") -> 6 5 11
type List struct {
Base // use for internal
// options
Expand All @@ -51,9 +55,13 @@ func (l *List) SetBuffer(buffer *bytes.Buffer) {
l.buffer = buffer
}

// NewList instance
func NewList(title string, data interface{}) *List {
return &List{
// NewList instance.
//
// data allow type:
//
// struct, slice, array, map
func NewList(title string, data interface{}, fns ...ListOpFunc) *List {
l := &List{
title: title,
data: data,
// base
Expand All @@ -69,16 +77,25 @@ func NewList(title string, data interface{}) *List {
TitleStyle: "comment",
},
}

return l.WithOptionFns(fns)
}

// WithOptions with options func
func (l *List) WithOptions(fn func(opts *ListOption)) *List {
if fn != nil {
fn(l.Opts)
// WithOptionFns with options func
func (l *List) WithOptionFns(fns []ListOpFunc) *List {
for _, fn := range fns {
if fn != nil {
fn(l.Opts)
}
}
return l
}

// WithOptions with options func
func (l *List) WithOptions(fns ...ListOpFunc) *List {
return l.WithOptionFns(fns)
}

// Format as string
func (l *List) Format() string {
if l.data == nil || l.formatted != "" {
Expand Down Expand Up @@ -190,7 +207,7 @@ type Lists struct {
}

// NewLists create lists
func NewLists(listMap map[string]interface{}) *Lists {
func NewLists(listMap map[string]interface{}, fns ...ListOpFunc) *Lists {
ls := &Lists{
Opts: &ListOption{
SepChar: " ",
Expand All @@ -206,17 +223,22 @@ func NewLists(listMap map[string]interface{}) *Lists {
for title, data := range listMap {
ls.rows = append(ls.rows, NewList(title, data))
}
return ls
return ls.WithOptionFns(fns)
}

// WithOptions with options func
func (ls *Lists) WithOptions(fn func(opts *ListOption)) *Lists {
if fn != nil {
// WithOptionFns with options func
func (ls *Lists) WithOptionFns(fns []ListOpFunc) *Lists {
for _, fn := range fns {
fn(ls.Opts)
}
return ls
}

// WithOptions with options func list
func (ls *Lists) WithOptions(fns ...ListOpFunc) *Lists {
return ls.WithOptionFns(fns)
}

// Format as string
func (ls *Lists) Format() string {
if len(ls.rows) == 0 || ls.formatted != "" {
Expand Down
31 changes: 17 additions & 14 deletions show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,34 @@ func JSON(v interface{}, settings ...string) int {
// AList create a List instance and print.
//
// Usage:
// show.AList("some info", map[string]string{"name": "tom"}, nil)
func AList(title string, data interface{}, optFn func(opts *ListOption)) {
NewList(title, data).WithOptions(optFn).Println()
//
// show.AList("some info", map[string]string{"name": "tom"})
func AList(title string, data interface{}, fns ...ListOpFunc) {
NewList(title, data).WithOptionFns(fns).Println()
}

// MList show multi list data.
//
// Usage:
// show.MList(data, nil)
// show.MList(data, func(opts *ListOption) {
// opts.LeftIndent = " "
// })
func MList(listMap map[string]interface{}, fn func(opts *ListOption)) {
NewLists(listMap).WithOptions(fn).Println()
//
// show.MList(data)
// show.MList(data, func(opts *ListOption) {
// opts.LeftIndent = " "
// })
func MList(listMap map[string]interface{}, fns ...ListOpFunc) {
NewLists(listMap).WithOptionFns(fns).Println()
}

// TabWriter create.
// more please see: package text/tabwriter/example_test.go
//
// Usage:
// w := TabWriter(os.Stdout, []string{
// "a\tb\tc\td\t.",
// "123\t12345\t1234567\t123456789\t."
// })
// w.Flush()
//
// w := TabWriter(os.Stdout, []string{
// "a\tb\tc\td\t.",
// "123\t12345\t1234567\t123456789\t."
// })
// w.Flush()
func TabWriter(outTo io.Writer, rows []string) *tabwriter.Writer {
w := tabwriter.NewWriter(outTo, 0, 4, 2, ' ', tabwriter.Debug)

Expand Down
20 changes: 16 additions & 4 deletions show/symbols/chars.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package symbols

// links:
// http://cn.piliapp.com/symbol/
//
// http://cn.piliapp.com/symbol/
//
// 卍 卐 ■ ▶ ☐☑☒ ❖
const (
OK = '✔'
Expand All @@ -23,7 +25,7 @@ const (
FLOWER = '✿'
MUSIC = '♬'

// ☚ ☜ ☛ ☞
// UP ☚ ☜ ☛ ☞
UP = '⇧'
DOWN = '⇩'
LEFT = '⇦'
Expand Down Expand Up @@ -51,9 +53,19 @@ const (
Equal1 rune = '═'
Space rune = ' '
// Hyphen Minus
Hyphen rune = '-'
CNHyphen rune = '—'
Hyphen rune = '-' // eg: -------
CNHyphen rune = '—' // eg: —————
Hyphen2 rune = '─' // eg: ────

Underline rune = '_'
LeftArrow rune = '<'
RightArrow rune = '>'

VLine rune = '|'
VLineFull rune = '│'

// TChar eg TChar + Hyphen2: ──┬──
TChar rune = '┬'
// CCChar criss-cross
CCChar rune = '┼'
)
2 changes: 1 addition & 1 deletion show/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/gookit/gcli/v3/show/symbols"

// some constants
const (
// position
// Left position
Left = iota
Middle
Right
Expand Down

0 comments on commit edde870

Please sign in to comment.