From da79ff3602eb3cdf3067bb7ee070bc36c69247b9 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 16 Nov 2018 09:20:38 -0500 Subject: [PATCH] Feature/dynamic padding (#143) * IC for dyanmic padding * Factor out some methods * Add DynamicPadding boolean * Remove unnecessary conditional * Refactor AddRows method to use AddRow method * Add support for workspaces and coloring * Add dynamicPadding to serviceAccount * Remove header padding method; remove tempRow; moved padding to print method * Change Temp row append back to AddRow method --- deployment/deployment.go | 8 ++-- pkg/printutil/printutil.go | 74 +++++++++++++++++++++++--------- serviceaccount/serviceaccount.go | 5 ++- workspace/workspace.go | 19 +++++--- 4 files changed, 75 insertions(+), 31 deletions(-) diff --git a/deployment/deployment.go b/deployment/deployment.go index 141136586..a001efe6a 100644 --- a/deployment/deployment.go +++ b/deployment/deployment.go @@ -10,8 +10,9 @@ import ( var ( tab = printutil.Table{ - Padding: []int{30, 30, 10, 50}, - Header: []string{"NAME", "RELEASE NAME", "CHART", "DEPLOYMENT ID"}, + Padding: []int{30, 30, 10, 50}, + DynamicPadding: true, + Header: []string{"NAME", "RELEASE NAME", "ASTRO", "DEPLOYMENT ID"}, } ) @@ -92,8 +93,7 @@ func List(ws string, all bool) error { if all { ws = d.Workspace.Uuid } - - tab.AddRow([]string{d.Label, d.ReleaseName, d.Version, d.Id}, false) + tab.AddRow([]string{d.Label, d.ReleaseName, "v" + d.Version, d.Id}, false) } tab.Print() diff --git a/pkg/printutil/printutil.go b/pkg/printutil/printutil.go index 71043cfe0..36cf08ed5 100644 --- a/pkg/printutil/printutil.go +++ b/pkg/printutil/printutil.go @@ -35,6 +35,10 @@ type Table struct { // Function which will eval whether to apply color to a col // ColorColCond func(t *Table) (bool, error) // ColorColCode [2]string + + altPadding []int + + DynamicPadding bool } // Row represents a row to be printed @@ -45,19 +49,16 @@ type Row struct { } // AddRow is the preferred interface for adding a row to a table -func (t *Table) AddRow(row []string, color bool) { - if len(t.RenderedPadding) == 0 { - p := t.GetPadding() - t.RenderedPadding = p +func (t *Table) AddRow(values []string, color bool) { + if t.DynamicPadding { + t.dynamicPadding(Row{Raw: values, Colored: color}) + } else { + t.altPadding = t.Padding } - ri := strSliceToInterSlice(row) - rr := fmt.Sprintf(t.RenderedPadding, ri...) - r := Row{ - Raw: row, - Rendered: rr, - Colored: color, + Raw: values, + Colored: color, } t.Rows = append(t.Rows, r) @@ -81,47 +82,57 @@ func (t *Table) Print() error { // PrintHeader prints header func (t *Table) PrintHeader() { - if len(t.RenderedPadding) == 0 { - p := t.GetPadding() - t.RenderedPadding = p + if t.DynamicPadding { + t.dynamicPadding(Row{Raw: t.Header, Colored: false}) + } else { + t.altPadding = t.Padding } + p := t.GetPadding(t.altPadding) + headerSelectPrefix := "" if t.GetUserInput { headerSelectPrefix = fmt.Sprintf("%-5s", "#") } header := strSliceToInterSlice(t.Header) - t.RenderedHeader = fmt.Sprintf(t.RenderedPadding, header...) + t.RenderedHeader = fmt.Sprintf(p, header...) fmt.Println(headerSelectPrefix + t.RenderedHeader) } // PrintRows prints rows with an "S" func (t *Table) PrintRows() { + + if len(t.RenderedPadding) == 0 { + p := t.GetPadding(t.altPadding) + t.RenderedPadding = p + } + for i, r := range t.Rows { + ri := strSliceToInterSlice(r.Raw) + rr := fmt.Sprintf(t.RenderedPadding, ri...) + // Responsible for adding the int in front of a row for selection by user rowSelectPrefix := "" if t.GetUserInput { rowSelectPrefix = fmt.Sprintf("%-5s", strconv.Itoa(i+1)) } - if r.Colored && len(t.ColorRowCode) == 2 { - fmt.Println(rowSelectPrefix + t.ColorRowCode[0] + r.Rendered + t.ColorRowCode[1]) + fmt.Println(rowSelectPrefix + t.ColorRowCode[0] + rr + t.ColorRowCode[1]) } else { - fmt.Println(rowSelectPrefix + r.Rendered) + fmt.Println(rowSelectPrefix + rr) } } } // GetPadding converts an array of ints into template padding for str fmting -func (t *Table) GetPadding() string { +func (t *Table) GetPadding(padding []int) string { padStr := " " - for _, x := range t.Padding { + for _, x := range padding { temp := "%ds" padStr += "%-" + fmt.Sprintf(temp, x) } - return padStr } @@ -136,3 +147,26 @@ func strSliceToInterSlice(ss []string) []interface{} { } return is } + +// If the altPadding slice is smaller than the number of incoming Values +// slice, then that means that values still need to be added to fill it up. +// +// If the altPadding slice length is equivalent to the Values slice length, +// then it should compare the length of the incoming value being evaluated +// to see if it longer than the value already stored in it's place in +// altPadding. If it is, replace it's value with the length of the +// incoming value. +// +// This helps ensure as it iterates through each row that the length +// of the longest value for each column is kept or replaced as new +// values are introduced. +func (t *Table) dynamicPadding(row Row) { + for i, col := range row.Raw { + colLength := len(col) + 5 + if len(t.altPadding) < len(row.Raw) { + t.altPadding = append(t.altPadding, colLength) + } else if t.altPadding[i] < colLength { + t.altPadding[i] = colLength + } + } +} diff --git a/serviceaccount/serviceaccount.go b/serviceaccount/serviceaccount.go index b1a06150d..7e3c376b8 100644 --- a/serviceaccount/serviceaccount.go +++ b/serviceaccount/serviceaccount.go @@ -9,8 +9,9 @@ import ( var ( tab = printutil.Table{ - Padding: []int{40, 40, 50, 50}, - Header: []string{"NAME", "CATEGORY", "UUID", "APIKEY"}, + Padding: []int{40, 40, 50, 50}, + DynamicPadding: true, + Header: []string{"NAME", "CATEGORY", "UUID", "APIKEY"}, } ) diff --git a/workspace/workspace.go b/workspace/workspace.go index f5a18c9ff..4ab1412e6 100644 --- a/workspace/workspace.go +++ b/workspace/workspace.go @@ -14,7 +14,8 @@ import ( var ( tab = printutil.Table{ - Padding: []int{44, 50}, + Padding: []int{44, 50}, + DynamicPadding: true, Header: []string{"NAME", "UUID"}, ColorRowCode: [2]string{"\033[1;32m", "\033[0m"}, } @@ -55,15 +56,19 @@ func List() error { ws := r.Data.GetWorkspaces c, err := config.GetCurrentContext() + for _, w := range ws { name := w.Label workspace := w.Uuid + var color bool + if c.Workspace == w.Uuid { - tab.AddRow([]string{name, workspace}, true) + color = true } else { - tab.AddRow([]string{name, workspace}, false) + color = false } + tab.AddRow([]string{name, workspace}, color) } tab.Print() @@ -122,15 +127,19 @@ func getWorkspaceSelection() (string, error) { ws := r.Data.GetWorkspaces c, err := config.GetCurrentContext() + for _, w := range ws { name := w.Label workspace := w.Uuid + var color bool + if c.Workspace == w.Uuid { - tab.AddRow([]string{name, workspace}, true) + color = true } else { - tab.AddRow([]string{name, workspace}, false) + color = false } + tab.AddRow([]string{name, workspace}, color) } tab.Print()