Skip to content

Commit

Permalink
adds utilities to render maps of ints
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Aug 8, 2023
1 parent cca19f4 commit 05236d6
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,32 @@ func (w *Writer) Println(msg ...string) {
w.rows = append(w.rows, &columnRow{kind: kindLine, values: val})
}

// AddMapIntsAsValue adds a row with title t and the data as value. Optionally sorts by value.
func (w *Writer) AddMapIntsAsValue(t string, data map[string]int, sortValues bool, reverse bool) {
var list []string
for k := range data {
list = append(list, k)
}

if sortValues {
sort.Slice(list, func(i, j int) bool {
if reverse {
return data[list[i]] > data[list[j]]
} else {
return data[list[i]] < data[list[j]]
}
})
}

for i, k := range list {
if i == 0 {
w.AddRowf(t, "%s: %d", k, F(data[k]))

Check failure on line 216 in columns/columns.go

View workflow job for this annotation

GitHub Actions / test (1.19)

(*github.com/nats-io/natscli/columns.Writer).AddRowf format %d has arg F(data[k]) of wrong type string

Check failure on line 216 in columns/columns.go

View workflow job for this annotation

GitHub Actions / test (1.20)

(*github.com/nats-io/natscli/columns.Writer).AddRowf format %d has arg F(data[k]) of wrong type string
} else {
w.AddRowf("", "%s: %d", k, F(data[k]))

Check failure on line 218 in columns/columns.go

View workflow job for this annotation

GitHub Actions / test (1.19)

(*github.com/nats-io/natscli/columns.Writer).AddRowf format %d has arg F(data[k]) of wrong type string

Check failure on line 218 in columns/columns.go

View workflow job for this annotation

GitHub Actions / test (1.20)

(*github.com/nats-io/natscli/columns.Writer).AddRowf format %d has arg F(data[k]) of wrong type string
}
}
}

// AddMapStringsAsValue adds a row with title t and the data as value, over multiple lines and correctly justified
func (w *Writer) AddMapStringsAsValue(t string, data map[string]string) {
maxLen := screenWidth()
Expand All @@ -220,6 +246,28 @@ func (w *Writer) AddMapStringsAsValue(t string, data map[string]string) {
}
}

// AddMapInts adds data with each key being a column title and value what follows the :. Optionally sorts by value
func (w *Writer) AddMapInts(data map[string]int, sortValues bool, reverse bool) {
var list []string
for k := range data {
list = append(list, k)
}

if sortValues {
sort.Slice(list, func(i, j int) bool {
if reverse {
return data[list[i]] > data[list[j]]
} else {
return data[list[i]] < data[list[j]]
}
})
}

for _, k := range list {
w.AddRowf(k, F(data[k]))
}
}

// AddMapStrings adds data with each key being a column title and value what follows the :
func (w *Writer) AddMapStrings(data map[string]string) {
maxLen := screenWidth()
Expand Down

0 comments on commit 05236d6

Please sign in to comment.