Update cmd/internal/printer/printer.go (#4415)

Co-authored-by: Jannik Clausen <12862103+masseelch@users.noreply.github.com>
Signed-off-by: jensholdgaard <jens@holdgaard.org>

feat(printer): update github.com/olekukonko/tablewriter to v1.0.8

# Conflicts:
#	go.sum
This commit is contained in:
Jens Holdgaard Pedersen
2025-10-03 19:03:42 +02:00
committed by GitHub
parent 175d96f7ec
commit 01063ef639
3 changed files with 89 additions and 15 deletions

View File

@@ -14,6 +14,7 @@ import (
"entgo.io/ent/entc/gen"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
)
// A Config controls the output of Fprint.
@@ -47,8 +48,24 @@ func (p Config) node(t *gen.Type) {
header = []string{"Field", "Type", "Unique", "Optional", "Nillable", "Default", "UpdateDefault", "Immutable", "StructTag", "Validators", "Comment"}
)
b.WriteString(t.Name + ":\n")
table.SetAutoFormatHeaders(false)
table.SetHeader(header)
table.Options(
tablewriter.WithHeaderConfig(tw.CellConfig{
Padding: tw.CellPadding{
Global: tw.Padding{
Left: tw.Space,
Right: tw.Space,
},
},
Formatting: tw.CellFormatting{
AutoFormat: tw.Off,
},
}),
tablewriter.WithRendition(tw.Rendition{
Symbols: tw.NewSymbols(tw.StyleASCII),
}),
)
table.Header(header)
var alignment = make([]tw.Align, 0)
if t.ID != nil {
id = append(id, t.ID)
}
@@ -61,16 +78,46 @@ func (p Config) node(t *gen.Type) {
return name == "Name" && i == 0 || name == header[i]
})
row[i] = fmt.Sprint(field.Interface())
_, err := strconv.Atoi(row[i])
if err == nil {
alignment = append(alignment, tw.AlignRight)
} else {
alignment = append(alignment, tw.AlignLeft)
}
}
row[len(row)-1] = f.Comment()
table.Append(row)
_ = table.Append(row)
table.Options(
tablewriter.WithRowAlignmentConfig(
tw.CellAlignment{PerColumn: alignment},
),
)
}
table.Render()
err := table.Render()
if err != nil {
return
}
// Create new table for edges
table = tablewriter.NewWriter(&b)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Edge", "Type", "Inverse", "BackRef", "Relation", "Unique", "Optional", "Comment"})
table.Options(
tablewriter.WithHeaderConfig(tw.CellConfig{
Formatting: tw.CellFormatting{AutoFormat: tw.Off},
Padding: tw.CellPadding{
Global: tw.Padding{
Left: tw.Space,
Right: tw.Space,
},
},
}),
tablewriter.WithRendition(tw.Rendition{
Symbols: tw.NewSymbols(tw.StyleASCII),
}),
)
table.Header([]string{"Edge", "Type", "Inverse", "BackRef", "Relation", "Unique", "Optional", "Comment"})
hasEdges := false
for _, e := range t.Edges {
table.Append([]string{
hasEdges = true
err := table.Append([]string{
e.Name,
e.Type.Name,
strconv.FormatBool(e.IsInverse()),
@@ -80,9 +127,15 @@ func (p Config) node(t *gen.Type) {
strconv.FormatBool(e.Optional),
e.Comment(),
})
if err != nil {
return
}
}
if table.NumLines() > 0 {
table.Render()
if hasEdges {
err := table.Render()
if err != nil {
return
}
}
io.WriteString(p, strings.ReplaceAll(b.String(), "\n", "\n\t")+"\n")
}