Skip to content

Add dashboard list command #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Limit number of widgets per row
  • Loading branch information
polldo committed Oct 11, 2021
commit 972676a0c9a8105a138d4c33765d6b37c8ed5a55
32 changes: 24 additions & 8 deletions cli/dashboard/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package dashboard

import (
"math"
"os"
"strings"

Expand All @@ -29,6 +30,10 @@ import (
"github.com/spf13/cobra"
)

const (
widgetsPerRow = 3
)

var listFlags struct {
showSharing bool
}
Expand Down Expand Up @@ -72,20 +77,31 @@ func (r listResult) String() string {
t := table.New()

head := []interface{}{"Name", "ID", "Widgets", "UpdatedAt"}
if listFlags.showSharing {
head = append(head, "SharedBy", "SharedWith")
}
t.SetHeader(head...)

for _, dash := range r.dashboards {
row := []interface{}{dash.Name, dash.ID}
row = append(row, strings.Join(dash.Widgets, ", "))
row = append(row, dash.UpdatedAt)
if listFlags.showSharing {
row = append(row, dash.SharedBy)
row = append(row, strings.Join(dash.SharedWith, ", "))

// Limit number of widgets per row.
if len(dash.Widgets) > widgetsPerRow {
row = append(row, strings.Join(dash.Widgets[:widgetsPerRow], ", "))
dash.Widgets = dash.Widgets[widgetsPerRow:]
} else {
row = append(row, strings.Join(dash.Widgets, ", "))
dash.Widgets = nil
}
row = append(row, dash.UpdatedAt)
t.AddRow(row...)

// Print remaining widgets in new rows
for len(dash.Widgets) > 0 {
row := []interface{}{"", ""}
l := int(math.Min(float64(len(dash.Widgets)), widgetsPerRow))
row = append(row, strings.Join(dash.Widgets[:l], ", "))
dash.Widgets = dash.Widgets[l:]
row = append(row, "")
t.AddRow(row...)
}
}
return t.Render()
}