mirror of
https://github.com/anchore/syft.git
synced 2026-10-31 06:26:30 +01:00
chore: upgrade tablewriter dependency to use new API (#3990)
* upgrade tablewriter Signed-off-by: Carlos Panato <ctadeu@gmail.com> * remove header line whitespace Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix static analysis Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Carlos Panato <ctadeu@gmail.com> Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> Co-authored-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
co-authored by
Alex Goodman
parent
179cc70a36
commit
2111d4d0e4
@@ -68,15 +68,15 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon
|
||||
if cfg.IsJSON {
|
||||
require.JSONEq(t, string(expected), string(actual))
|
||||
} else {
|
||||
requireEqual(t, expected, actual)
|
||||
requireEqual(t, string(expected), string(actual))
|
||||
}
|
||||
}
|
||||
|
||||
func requireEqual(t *testing.T, expected any, actual any) {
|
||||
func requireEqual(t *testing.T, expected string, actual string) {
|
||||
if diff := cmp.Diff(expected, actual); diff != "" {
|
||||
// uncomment to debug
|
||||
// t.Logf("expected: %s", expected)
|
||||
// t.Logf("actual: %s", actual)
|
||||
// t.Logf("expected:\n%s", expected)
|
||||
// t.Logf("actual:\n%s", actual)
|
||||
t.Fatalf("mismatched output: %s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/olekukonko/tablewriter/renderer"
|
||||
"github.com/olekukonko/tablewriter/tw"
|
||||
|
||||
"github.com/anchore/syft/syft/sbom"
|
||||
)
|
||||
@@ -66,25 +68,90 @@ func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error {
|
||||
columns = append(columns, "") // add a column for duplicate annotations
|
||||
rows = markDuplicateRows(rows)
|
||||
|
||||
table := tablewriter.NewWriter(writer)
|
||||
table := newTableWriter(writer, columns)
|
||||
|
||||
table.SetHeader(columns)
|
||||
table.SetHeaderLine(false)
|
||||
table.SetBorder(false)
|
||||
table.SetAutoWrapText(false)
|
||||
table.SetAutoFormatHeaders(true)
|
||||
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetCenterSeparator("")
|
||||
table.SetColumnSeparator("")
|
||||
table.SetRowSeparator("")
|
||||
table.SetTablePadding(" ")
|
||||
table.SetNoWhiteSpace(true)
|
||||
if err := table.Bulk(rows); err != nil {
|
||||
return fmt.Errorf("failed to add table rows: %w", err)
|
||||
}
|
||||
|
||||
table.AppendBulk(rows)
|
||||
table.Render()
|
||||
return table.Render()
|
||||
}
|
||||
|
||||
return nil
|
||||
func newTableWriter(writer io.Writer, columns []string) *tablewriter.Table {
|
||||
// Here’s a simplified diagram of a table with a header, rows, and footer:
|
||||
//
|
||||
// [Borders.Top]
|
||||
// | Header1 | Header2 | (Line below header: Lines.ShowTop)
|
||||
// [Separators.BetweenRows]
|
||||
// | Row1 | Row1 |
|
||||
// [Separators.BetweenRows]
|
||||
// | Row2 | Row2 |
|
||||
// [Lines.ShowBottom]
|
||||
// | Footer1 | Footer2 |
|
||||
// [Borders.Bottom]
|
||||
//
|
||||
// So for example:
|
||||
// ┌──────┬─────┐ <- Borders.Top
|
||||
// │ NAME │ AGE │
|
||||
// ├──────┼─────┤ <- Lines.ShowTop
|
||||
// │ Alice│ 25 │
|
||||
// ├──────┼─────┤ <- Separators.BetweenRows
|
||||
// │ Bob │ 30 │
|
||||
// ├──────┼─────┤ <- Lines.ShowBottom
|
||||
// │ Total│ 2 │
|
||||
// └──────┴─────┘ <- Borders.Bottom
|
||||
|
||||
return tablewriter.NewTable(writer,
|
||||
tablewriter.WithHeader(columns),
|
||||
tablewriter.WithHeaderAutoFormat(tw.On),
|
||||
tablewriter.WithHeaderAutoWrap(tw.WrapNone),
|
||||
tablewriter.WithHeaderAlignment(tw.AlignLeft),
|
||||
tablewriter.WithRowAutoFormat(tw.Off),
|
||||
tablewriter.WithRowAutoWrap(tw.WrapNone),
|
||||
tablewriter.WithRowAlignment(tw.AlignLeft),
|
||||
tablewriter.WithTrimSpace(tw.On),
|
||||
tablewriter.WithAutoHide(tw.On),
|
||||
tablewriter.WithRenderer(renderer.NewBlueprint()),
|
||||
tablewriter.WithBehavior(
|
||||
tw.Behavior{
|
||||
TrimSpace: tw.On,
|
||||
AutoHide: tw.On,
|
||||
},
|
||||
),
|
||||
tablewriter.WithPadding(
|
||||
tw.Padding{
|
||||
Left: "",
|
||||
Right: " ",
|
||||
Top: "",
|
||||
Bottom: "",
|
||||
},
|
||||
),
|
||||
tablewriter.WithRendition(
|
||||
tw.Rendition{
|
||||
Symbols: tw.NewSymbols(tw.StyleNone),
|
||||
Borders: tw.Border{
|
||||
Left: tw.Off,
|
||||
Top: tw.Off,
|
||||
Right: tw.Off,
|
||||
Bottom: tw.Off,
|
||||
},
|
||||
Settings: tw.Settings{
|
||||
Separators: tw.Separators{
|
||||
ShowHeader: tw.Off,
|
||||
ShowFooter: tw.Off,
|
||||
BetweenRows: tw.Off,
|
||||
BetweenColumns: tw.Off,
|
||||
},
|
||||
Lines: tw.Lines{
|
||||
ShowTop: tw.Off,
|
||||
ShowBottom: tw.Off,
|
||||
ShowHeaderLine: tw.Off,
|
||||
ShowFooterLine: tw.Off,
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func markDuplicateRows(items [][]string) [][]string {
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
NAME VERSION TYPE
|
||||
package-1 1.0.1 python
|
||||
package-2 2.0.1 deb
|
||||
NAME VERSION TYPE
|
||||
package-1 1.0.1 python
|
||||
package-2 2.0.1 deb
|
||||
|
||||
Reference in New Issue
Block a user