Skip to content

Commit

Permalink
Feature/dynamic padding (#143)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Ben authored and andscoop committed Nov 16, 2018
1 parent b37cb74 commit da79ff3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 31 deletions.
8 changes: 4 additions & 4 deletions deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
)

Expand Down Expand Up @@ -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()
Expand Down
74 changes: 54 additions & 20 deletions pkg/printutil/printutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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
}
}
}
5 changes: 3 additions & 2 deletions serviceaccount/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
)

Expand Down
19 changes: 14 additions & 5 deletions workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit da79ff3

Please sign in to comment.