Skip to content

Commit

Permalink
Move table columns so that the important column is displayed first (#87)
Browse files Browse the repository at this point in the history
(and last to get cropped)

Also only do unicode output when printing to terminal, otherwise print default ascii when being piped to file or piped to a pager.
  • Loading branch information
another-rex authored Dec 19, 2022
1 parent e2ef690 commit ea7499c
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions internal/output/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ import (
func PrintTableResults(vulnResult *models.VulnerabilityResults, outputWriter io.Writer) {
outputTable := table.NewWriter()
outputTable.SetOutputMirror(outputWriter)
outputTable.AppendHeader(table.Row{"Source", "Ecosystem", "Affected Package", "Version", "OSV URL (ID In Bold)"})
outputTable.AppendHeader(table.Row{"OSV URL (ID In Bold)", "Ecosystem", "Affected Package", "Version", "Source"})

width, _, err := term.GetSize(int(os.Stdout.Fd()))
isTerminal := false
if err == nil { // If output is a terminal, set max length to width and add styling
outputTable.SetStyle(table.StyleRounded)
outputTable.Style().Color.Row = text.Colors{text.Reset, text.BgHiBlack}
outputTable.Style().Color.RowAlternate = text.Colors{text.Reset, text.BgBlack}
outputTable.SetAllowedRowLength(width)
isTerminal = true
} // Otherwise use default ascii (e.g. getting piped to a file)

for _, sourceRes := range vulnResult.Results {
for _, pkg := range sourceRes.Packages {
workingDir, err := os.Getwd()
Expand All @@ -31,37 +42,36 @@ func PrintTableResults(vulnResult *models.VulnerabilityResults, outputWriter io.
}

for _, group := range pkg.Groups {
outputRow := table.Row{source.Path}
outputRow := table.Row{}
shouldMerge := false
if pkg.Package.Ecosystem == "GIT" {
outputRow = append(outputRow, "GIT", pkg.Package.Version, pkg.Package.Version)
shouldMerge = true
} else {
outputRow = append(outputRow, pkg.Package.Ecosystem, pkg.Package.Name, pkg.Package.Version)
}

var ids []string
var links []string

for _, vuln := range group.IDs {
ids = append(ids, vuln)
links = append(links, osv.BaseVulnerabilityURL+text.Bold.EscapeSeq()+vuln+text.Reset.EscapeSeq())
if isTerminal {
links = append(links, osv.BaseVulnerabilityURL+text.Bold.EscapeSeq()+vuln+text.Reset.EscapeSeq())
} else {
links = append(links, osv.BaseVulnerabilityURL+vuln)
}
}

outputRow = append(outputRow, strings.Join(links, "\n"))

if pkg.Package.Ecosystem == "GIT" {
outputRow = append(outputRow, "GIT", pkg.Package.Version, pkg.Package.Version)
shouldMerge = true
} else {
outputRow = append(outputRow, pkg.Package.Ecosystem, pkg.Package.Name, pkg.Package.Version)
}

outputRow = append(outputRow, source.Path)
outputTable.AppendRow(outputRow, table.RowConfig{AutoMerge: shouldMerge})
}
}
}

outputTable.SetStyle(table.StyleRounded)
outputTable.Style().Color.Row = text.Colors{text.Reset, text.BgHiBlack}
outputTable.Style().Color.RowAlternate = text.Colors{text.Reset, text.BgBlack}

width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err == nil { // If output is a terminal, set max length to width
outputTable.SetAllowedRowLength(width)
} // Otherwise don't set max width (e.g. getting piped to a file)
if outputTable.Length() == 0 {
return
}
Expand Down

0 comments on commit ea7499c

Please sign in to comment.