From d9ad3a88a143b055abe68f301c41eda3b9c143b3 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 5 Apr 2019 00:50:41 +0300 Subject: [PATCH] Added tags rendering --- cli/cli.go | 2 +- cli/render.go | 56 +++++++++++++++++++++++++++++++++------------- inspect/inspect.go | 1 + report/report.go | 1 + 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index c7b5349..7270f90 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -28,7 +28,7 @@ import ( // App info const ( APP = "aligo" - VER = "1.0.1" + VER = "1.1.0" DESC = "Utility for viewing and checking Golang struct alignment" ) diff --git a/cli/render.go b/cli/render.go index c7d78fd..cfeb15e 100644 --- a/cli/render.go +++ b/cli/render.go @@ -169,7 +169,7 @@ func printStructInfo(str *report.Struct, pkgPath string, detailed, optimal bool) // printDetailedFieldsInfo prints verbose fields info func printDetailedFieldsInfo(fields []*report.Field, pkgPath string) { - f := getFieldFormat(fields, pkgPath, false) + f := getFieldFormat(fields, pkgPath, false, false) counter := int64(0) maxAlign := inspect.GetMaxAlign() @@ -177,12 +177,9 @@ func printDetailedFieldsInfo(fields []*report.Field, pkgPath string) { for index, field := range fields { fType := getPrettyFieldType(field.Type, pkgPath) - fmtc.Printf(f, field.Name, strutil.Ellipsis(fType, MAX_TYPE_SIZE)) - fmtc.Printf(" ") + printFieldInfo(f, field.Name, fType, field.Tag) - if counter != 0 { - fmtc.Printf(strings.Repeat(" ", int(counter))) - } + fmtc.Printf(strings.Repeat(" ", int(counter+1))) for i := int64(0); i < field.Size; i++ { fmtc.Printf("{g}■ {!}") @@ -191,9 +188,7 @@ func printDetailedFieldsInfo(fields []*report.Field, pkgPath string) { if counter == maxAlign { if i+1 != field.Size { - fmtc.NewLine() - fmtc.Printf(f, "", "") - fmtc.Printf(" ") + printFieldInfo("\n"+f+" ", "", "", "") } counter = 0 } @@ -212,30 +207,59 @@ func printDetailedFieldsInfo(fields []*report.Field, pkgPath string) { // printSimpleFieldsInfo prints verbose fields info func printSimpleFieldsInfo(fields []*report.Field, pkgPath string) { - f := getFieldFormat(fields, pkgPath, true) + f := getFieldFormat(fields, pkgPath, true, true) + "\n" for _, field := range fields { fType := getPrettyFieldType(field.Type, pkgPath) - fmtc.Printf(f, field.Name, strutil.Ellipsis(fType, MAX_TYPE_SIZE)) - fmtc.NewLine() + printFieldInfo(f, field.Name, fType, field.Tag) } } // getFieldFormat generate format string for field output -func getFieldFormat(fields []*report.Field, pkgPath string, short bool) string { - var lName, lType int +func getFieldFormat(fields []*report.Field, pkgPath string, short, withTags bool) string { + var lName, lType, lTag int for _, field := range fields { fType := getPrettyFieldType(field.Type, pkgPath) lName = mathutil.Max(lName, len(field.Name)) lType = mathutil.Max(lType, len(strutil.Ellipsis(fType, MAX_TYPE_SIZE))) + lTag = mathutil.Max(lTag, len(field.Tag)) + } + + if lTag > 0 { + lTag += 2 } - if short { + if !withTags { + lTag = 0 + } + + switch { + case lTag > 0 && short: + return fmt.Sprintf(" %%-%ds {*}%%-%ds{!} {y}%%s{!}", lName, lType) + case lTag > 0 && !short: + return fmt.Sprintf(" %%-%ds {*}%%-%ds{!} {y}%%-%ds{!}", lName, lType, lTag) + case lTag == 0 && short: return fmt.Sprintf(" %%-%ds {*}%%s{!}", lName) + default: + return fmt.Sprintf(" %%-%ds {*}%%-%ds{!}", lName, lType) } +} - return fmt.Sprintf(" %%-%ds {*}%%-%ds{!}", lName, lType) +// printFieldInfo prints field info +func printFieldInfo(format, name, typ, tag string) { + var fTag string + + if tag != "" { + fTag = "`" + tag + "`" + } + + switch strings.Count(format, "%") { + case 3: + fmtc.Printf(format, name, strutil.Ellipsis(typ, MAX_TYPE_SIZE), fTag) + default: + fmtc.Printf(format, name, strutil.Ellipsis(typ, MAX_TYPE_SIZE)) + } } // getPrettyFieldType formats type name diff --git a/inspect/inspect.go b/inspect/inspect.go index 484c4c2..bcbb2d4 100644 --- a/inspect/inspect.go +++ b/inspect/inspect.go @@ -134,6 +134,7 @@ func getStructInfo(name string, str *types.Struct, pos token.Position) *report.S &report.Field{ Name: f.Name(), Type: f.Type().String(), + Tag: str.Tag(i), Size: size, }, ) diff --git a/report/report.go b/report/report.go index c350b56..398ebef 100644 --- a/report/report.go +++ b/report/report.go @@ -32,6 +32,7 @@ type Struct struct { type Field struct { Name string `json:"name"` Type string `json:"type"` + Tag string `json:"tag"` Size int64 `json:"size"` }