Skip to content

Commit

Permalink
Merge pull request #3 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.1.0
  • Loading branch information
andyone authored Apr 5, 2019
2 parents 6810958 + d9ad3a8 commit fb23956
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
56 changes: 40 additions & 16 deletions cli/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,17 @@ 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()

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}■ {!}")
Expand All @@ -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
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
)
Expand Down
1 change: 1 addition & 0 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit fb23956

Please sign in to comment.