Skip to content

Commit

Permalink
Improvement: Align text output
Browse files Browse the repository at this point in the history
This commit changes the output of the text printer such as that
the '# comment' is vertically aligned across all lines. This
makes it easier to read.

Old:

    tailscale/tailscale:v1.72.1@svc-tailscale-container-app-1 # fetched in 1.062s
    =       tailscale/tailscale:1.72.1
    mariadb:10.11.8-jammy@svc-git-db-1 # fetched in 1.088s
    ▲       mariadb:11.6.1-ubi9-rc
    nginx:1.27.0-alpine@svc-abc-web-1 # fetched in 1.1s
    ▲       nginx:1.27.1
    nginx:1.27.0-alpine@svc-def-web-1 # fetched in 1.1s
    ▲       nginx:1.27.1
    nginx:1.27.0-alpine@svc-ghi-web-1 # fetched in 1.1s
    ▲       nginx:1.27.1
    mariadb:10.11.8-jammy@svc-abc-db-1 # fetched in 1.088s
    ▲       mariadb:11.6.1-ubi9-rc
    nginx:1.27.0-alpine@svc-abc-web-1 # fetched in 1.1s
    ▲       nginx:1.27.1

New:

    tailscale/tailscale:v1.72.1@svc-tailscale-container-app-1 # fetched in 1.062s
    =       tailscale/tailscale:1.72.1
    mariadb:10.11.8-jammy@svc-git-db-1                        # fetched in 1.088s
    ▲   mariadb:11.6.1-ubi9-rc
    nginx:1.27.0-alpine@svc-abc-web-1                         # fetched in 1.1s
    ▲   nginx:1.27.1
    nginx:1.27.0-alpine@svc-def-web-1                         # fetched in 1.1s
    ▲   nginx:1.27.1
    nginx:1.27.0-alpine@svc-ghi-web-1                         # fetched in 1.1s
    ▲   nginx:1.27.1
    mariadb:10.11.8-jammy@svc-abc-db-1                        # fetched in 1.088s
    ▲   mariadb:11.6.1-ubi9-rc
    nginx:1.27.0-alpine@svc-abc-web-1                         # fetched in 1.1s
    ▲   nginx:1.27.1
  • Loading branch information
mgumz committed Sep 8, 2024
1 parent bcc48c1 commit c49e678
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pkg/printer/text_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package printer
import (
"fmt"
"io"
"text/tabwriter"
"time"

"github.com/Masterminds/semver/v3"
Expand All @@ -25,7 +26,7 @@ var (
// TextPrinter is a small helper to print the requested images, fetched tags
// etc to create useful output
type TextPrinter struct {
w io.Writer
w *tabwriter.Writer
showOld bool
showStats bool
printedTag bool
Expand All @@ -35,8 +36,9 @@ type TextPrinter struct {
// NewTextPrinter returns a TextPrinter which prints to w
// if simpleMarkers is true, use simple ASCII chars as vertdict markers
func NewTextPrinter(w io.Writer, simpleMarkers bool) *TextPrinter {
tw := tabwriter.NewWriter(w, 0, 0, 1, ' ', 0)
p := &TextPrinter{
w: w,
w: tw,
verdictMarkers: verdictMarkersUnicode,
}
if simpleMarkers {
Expand All @@ -49,12 +51,13 @@ func NewTextPrinter(w io.Writer, simpleMarkers bool) *TextPrinter {
func (p *TextPrinter) Flush(stats *stats.AllStats) {
if p.showStats && stats != nil {
fmt.Fprintln(p.w, "---")
fmt.Fprintf(p.w, "duration: %s\n", stats.Duration)
fmt.Fprintf(p.w, "asked: %d\n", stats.Asked)
fmt.Fprintf(p.w, "checked: %d\n", stats.Checked)
fmt.Fprintf(p.w, "non-semver: %d\n", stats.NonSemVer)
fmt.Fprintf(p.w, "duplicates: %d\n", stats.Duplicates)
fmt.Fprintf(p.w, "duration:\t%s\n", stats.Duration)
fmt.Fprintf(p.w, "asked:\t%d\n", stats.Asked)
fmt.Fprintf(p.w, "checked:\t%d\n", stats.Checked)
fmt.Fprintf(p.w, "non-semver:\t%d\n", stats.NonSemVer)
fmt.Fprintf(p.w, "duplicates:\t%d\n", stats.Duplicates)
}
p.w.Flush()
}

// SetShowOldTags configures TextPrinter p to also show older, outdated tags
Expand All @@ -71,13 +74,13 @@ func (p *TextPrinter) SetShowStats(s bool) {
// NewSpec starts printing the tags for "name" - its like a headline
func (p *TextPrinter) NewSpec(name string, dur time.Duration, err error) {
p.printedTag = false
comment := "# skipped"
comment := "\t# skipped"
if dur > 0 {
comment = fmt.Sprintf("# fetched in %s", dur.Round(time.Millisecond))
comment = fmt.Sprintf("\t# fetched in %s", dur.Round(time.Millisecond))
}
fmt.Fprintln(p.w, name, comment)
if err != nil {
fmt.Fprintf(p.w, "\t%s\n", err)
fmt.Fprintf(p.w, " %s\n", err)
return
}
}
Expand Down Expand Up @@ -114,7 +117,7 @@ func (p *TextPrinter) PrintTag(name string, base, other *semver.Version) {
verdict = p.verdictMarkers[markOutdated]
}

fmt.Fprintf(p.w, "%s\t%s:%s\n", verdict, name, other)
fmt.Fprintf(p.w, "%s %s:%s\t\n", verdict, name, other)

p.printedTag = true
}

0 comments on commit c49e678

Please sign in to comment.