Skip to content

Commit

Permalink
Merge pull request #869 from ripienaar/tablewriter
Browse files Browse the repository at this point in the history
Remove the tablewriter dependency completely
  • Loading branch information
ripienaar authored Sep 21, 2023
2 parents f7e1c2c + 4b1e9db commit fe08768
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/prometheus/common v0.44.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f
github.com/xlab/tablewriter v0.0.0-20160610135559-80b567a11ad5
golang.org/x/crypto v0.13.0
golang.org/x/term v0.12.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f h1:SGznmvCovewbaSgBsHgdThtWsLj5aCLX/3ZXMLd1UD0=
github.com/tylertreat/hdrhistogram-writer v0.0.0-20210816161836-2e440612a39f/go.mod h1:IY84XkhrEJTdHYLNy/zObs8mXuUAp9I65VyarbPSCCY=
github.com/xlab/tablewriter v0.0.0-20160610135559-80b567a11ad5 h1:gmD7q6cCJfBbcuobWQe/KzLsd9Cd3amS1Mq5f3uU1qo=
github.com/xlab/tablewriter v0.0.0-20160610135559-80b567a11ad5/go.mod h1:fVwOndYN3s5IaGlMucfgxwMhqwcaJtlGejBU6zX6Yxw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
16 changes: 10 additions & 6 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
package monitor

import (
"github.com/xlab/tablewriter"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)

type Status string
Expand All @@ -26,12 +27,15 @@ var (
UnknownStatus Status = "UNKNOWN"
)

func newTableWriter(title string) *tablewriter.Table {
table := tablewriter.CreateTable()
table.UTF8Box()
func newTableWriter(title string) table.Writer {
tbl := table.NewWriter()
tbl.SetStyle(table.StyleRounded)
tbl.Style().Title.Align = text.AlignCenter
tbl.Style().Format.Header = text.FormatDefault

if title != "" {
table.AddTitle(title)
tbl.SetTitle(title)
}

return table
return tbl
}
26 changes: 16 additions & 10 deletions monitor/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"path/filepath"
"strings"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/nats-io/natscli/columns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
Expand Down Expand Up @@ -109,40 +111,40 @@ func (r *Result) renderHuman() string {

fmt.Fprintf(buf, "%s: %s\n\n", r.Name, r.Status)

table := newTableWriter("")
table.AddHeaders("Status", "Message")
tblWriter := newTableWriter("")
tblWriter.AppendHeader(table.Row{"Status", "Message"})
lines := 0
for _, ok := range r.OKs {
table.AddRow("OK", ok)
tblWriter.AppendRow(table.Row{"OK", ok})
lines++
}
for _, warn := range r.Warnings {
table.AddRow("Warning", warn)
tblWriter.AppendRow(table.Row{"Warning", warn})
lines++
}
for _, crit := range r.Criticals {
table.AddRow("Critical", crit)
tblWriter.AppendRow(table.Row{"Critical", crit})
lines++
}

if lines > 0 {
fmt.Fprintln(buf, "Status Detail")
fmt.Fprintln(buf)
fmt.Fprint(buf, table.Render())
fmt.Fprint(buf, tblWriter.Render())
fmt.Fprintln(buf)
}

table = newTableWriter("")
table.AddHeaders("Metric", "Value", "Unit", "Critical Threshold", "Warning Threshold", "Description")
tblWriter = newTableWriter("")
tblWriter.AppendHeader(table.Row{"Metric", "Value", "Unit", "Critical Threshold", "Warning Threshold", "Description"})
lines = 0
for _, pd := range r.PerfData {
table.AddRow(pd.Name, pd.Value, pd.Unit, pd.Crit, pd.Warn, pd.Help)
tblWriter.AppendRow(table.Row{pd.Name, f(pd.Value), pd.Unit, f(pd.Crit), f(pd.Warn), pd.Help})
lines++
}
if lines > 0 {
fmt.Fprintln(buf, "Check Metrics")
fmt.Fprintln(buf)
fmt.Fprint(buf, table.Render())
fmt.Fprint(buf, tblWriter.Render())
fmt.Fprintln(buf)
}

Expand Down Expand Up @@ -296,3 +298,7 @@ func (r *Result) GenericExit() {

r.Exit()
}

func f(v any) string {
return columns.F(v)
}

0 comments on commit fe08768

Please sign in to comment.