Skip to content

Commit

Permalink
👔 up: all - update some for dump format and cflag arg format
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 25, 2023
1 parent 406a233 commit 7bfa5f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cflag/optarg.go
Expand Up @@ -65,15 +65,15 @@ func (a *FlagArg) check() error {
return nil
}

// HelpDesc string
// HelpDesc string build
func (a *FlagArg) HelpDesc() string {
desc := strutil.UpperFirst(a.Desc)
if a.Required {
desc = "<red>*</>" + desc
}

if a.defVal != "" {
desc += "(Default:" + a.defVal + ")"
desc += "(default: <mga>" + a.defVal + "</>)"
}
return desc
}
19 changes: 13 additions & 6 deletions dump/dumper.go
Expand Up @@ -198,7 +198,8 @@ func (d *Dumper) printOne(v any) {
// print reflect value
func (d *Dumper) printRValue(t reflect.Type, v reflect.Value) {
// if is a ptr, get real type and value
if t.Kind() == reflect.Ptr {
isPtr := t.Kind() == reflect.Ptr
if isPtr {
if v.IsNil() {
d.printf("%s<nil>,\n", t.String())
return
Expand Down Expand Up @@ -254,8 +255,9 @@ func (d *Dumper) printRValue(t reflect.Type, v reflect.Value) {
eleNum := v.Len()
lenTip := d.ColorTheme.lenTip("#len=" + strconv.Itoa(eleNum) + ",cap=" + strconv.Itoa(v.Cap()))

d.indentPrint(t.String(), " [ ", lenTip, "\n")
d.write(!isPtr, t.String(), " [ ", lenTip, "\n")
d.msValue = false

for i := 0; i < eleNum; i++ {
sv := v.Index(i)
d.advance(1)
Expand All @@ -274,7 +276,7 @@ func (d *Dumper) printRValue(t reflect.Type, v reflect.Value) {
break // don't print v again
}

d.indentPrint(d.ColorTheme.msType(t.String()), " {\n")
d.write(!isPtr, d.ColorTheme.msType(t.String()), " {\n")
d.msValue = false

fldNum := v.NumField()
Expand Down Expand Up @@ -304,7 +306,8 @@ func (d *Dumper) printRValue(t reflect.Type, v reflect.Value) {
d.indentPrint("},\n")
case reflect.Map:
lenTip := d.ColorTheme.lenTip("#len=" + strconv.Itoa(v.Len()))
d.indentPrint(d.ColorTheme.msType(t.String()), " { ", lenTip, "\n")

d.write(!isPtr, d.ColorTheme.msType(t.String()), " { ", lenTip, "\n")
d.msValue = false

for _, key := range v.MapKeys() {
Expand Down Expand Up @@ -412,8 +415,8 @@ func (d *Dumper) printf(f string, v ...any) {
}
}

func (d *Dumper) indentPrint(v ...any) {
if !d.msValue {
func (d *Dumper) write(indent bool, v ...any) {
if indent && !d.msValue {
_, _ = d.Output.Write(d.indentBytes)
}

Expand All @@ -423,3 +426,7 @@ func (d *Dumper) indentPrint(v ...any) {
color.Fprint(d.Output, v...)
}
}

func (d *Dumper) indentPrint(v ...any) {
d.write(true, v...)
}
1 change: 0 additions & 1 deletion reflects/util.go
Expand Up @@ -100,7 +100,6 @@ func SetUnexportedValue(rv reflect.Value, value any) {
func SetValue(rv reflect.Value, val any) error {
// get real type of the ptr value
if rv.Kind() == reflect.Ptr {
// init if is nil
if rv.IsNil() {
elemTyp := rv.Type().Elem()
rv.Set(reflect.New(elemTyp))
Expand Down

0 comments on commit 7bfa5f4

Please sign in to comment.