Skip to content

Commit

Permalink
Revert "Revert errand transposing logic"
Browse files Browse the repository at this point in the history
This reverts commit 88aa790.
  • Loading branch information
Rob Day-Reynolds committed May 11, 2017
1 parent 617276e commit 64a1d74
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 23 deletions.
4 changes: 3 additions & 1 deletion cmd/run_errand.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c RunErrandCmd) Run(opts RunErrandOpts) error {

func (c RunErrandCmd) summarize(errandName string, results []boshdir.ErrandResult) error {
table := boshtbl.Table{
Content: "errands",
Content: "errand(s)",

Header: []boshtbl.Header{
boshtbl.NewHeader("Exit Code"),
Expand All @@ -64,6 +64,8 @@ func (c RunErrandCmd) summarize(errandName string, results []boshdir.ErrandResul
},

Notes: []string{},

Transpose: true,
}

var errandErr error
Expand Down
8 changes: 6 additions & 2 deletions cmd/run_errand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var _ = Describe("RunErrandCmd", func() {

Expect(ui.Table).To(Equal(
boshtbl.Table{
Content: "errands",
Content: "errand(s)",

Header: []boshtbl.Header{
boshtbl.NewHeader("Exit Code"),
Expand All @@ -147,6 +147,8 @@ var _ = Describe("RunErrandCmd", func() {
},

Notes: []string{},

Transpose: true,
}))
})

Expand Down Expand Up @@ -223,7 +225,7 @@ var _ = Describe("RunErrandCmd", func() {

Expect(ui.Table).To(Equal(
boshtbl.Table{
Content: "errands",
Content: "errand(s)",

Header: []boshtbl.Header{
boshtbl.NewHeader("Exit Code"),
Expand All @@ -244,6 +246,8 @@ var _ = Describe("RunErrandCmd", func() {
},

Notes: []string{},

Transpose: true,
}))

Expect(err.Error()).To(Equal("Errand 'errand-name' completed with error (exit code 1)"))
Expand Down
2 changes: 2 additions & 0 deletions ui/table/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type ValueString struct {
S string
}

type EmptyValue struct{}

type ValueStrings struct {
S []string
}
Expand Down
21 changes: 16 additions & 5 deletions ui/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,30 @@ func (t Table) Print(w io.Writer) error {
}

writer := NewWriter(w, "-", t.BackgroundStr, t.BorderStr)
rowCount := len(t.Rows)
for _, section := range t.Sections {
rowCount += len(section.Rows)
}

if t.Transpose {
var newRows [][]Value

headerVals := buildHeaderVals(t)

for _, row := range t.Rows {
for i, val := range row {
if t.Header[i].Hidden {
for i, row := range t.Rows {
for j, val := range row {
if t.Header[j].Hidden {
continue
}

newRows = append(newRows, []Value{headerVals[i], val})
newRows = append(newRows, []Value{headerVals[j], val})
}

if i < (len(t.Rows) - 1) {
newRows = append(newRows, []Value{
EmptyValue{},
EmptyValue{},
})
}
}

Expand All @@ -124,7 +135,7 @@ func (t Table) Print(w io.Writer) error {
}

if !t.DataOnly {
err = t.printFooter(w, len(rows))
err = t.printFooter(w, rowCount)
}

return err
Expand Down
56 changes: 50 additions & 6 deletions ui/table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,46 @@ r3c1|r3c2|
r4c1|r4c2|
r4c1|r5c2|
r4c1|r6c2|
`))
})

It("prints a footer including the counts for rows in sections", func() {
table := Table{
Content: "things",
Header: []Header{
NewHeader("Header1"),
NewHeader("Header2"),
},
Sections: []Section{
{
FirstColumn: ValueString{"s1c1"},
Rows: [][]Value{
{ValueString{""}, ValueString{"s1r1c2"}},
{ValueString{""}, ValueString{"s1r2c2"}},
},
},
{
Rows: [][]Value{
{ValueString{"r3c1"}, ValueString{"r3c2"}},
},
},
},
Rows: [][]Value{
{ValueString{"r4c1"}, ValueString{"r4c2"}},
},
FillFirstColumn: true,
BackgroundStr: ".",
BorderStr: "|",
}
table.Print(buf)
Expect("\n" + buf.String()).To(Equal(`
Header1|Header2|
s1c1...|s1r1c2.|
s1c1...|s1r2c2.|
r3c1...|r3c2...|
r4c1...|r4c2...|
4 things
`))
})
})
Expand Down Expand Up @@ -448,14 +488,15 @@ Header1|Header2|
Context("table has Transpose:true", func() {
It("prints as transposed table", func() {
table := Table{
Content: "content",
Content: "errands",
Header: []Header{
NewHeader("Header1"),
NewHeader("OtherHeader2"),
NewHeader("Header3"),
},
Rows: [][]Value{
{ValueString{"r1c1"}, ValueString{"longr1c2"}},
{ValueString{"r2c1"}, ValueString{"r2c2"}},
{ValueString{"r1c1"}, ValueString{"longr1c2"}, ValueString{"r1c3"}},
{ValueString{"r2c1"}, ValueString{"r2c2"}, ValueString{"r2c3"}},
},
BackgroundStr: ".",
BorderStr: "|",
Expand All @@ -465,10 +506,13 @@ Header1|Header2|
Expect("\n" + buf.String()).To(Equal(`
Header1.....|r1c1....|
OtherHeader2|longr1c2|
Header3.....|r1c3....|
Header1.....|r2c1....|
OtherHeader2|r2c2....|
Header3.....|r2c3....|
4 content
2 errands
`))
})

Expand All @@ -477,7 +521,7 @@ OtherHeader2|r2c2....|
nonVisibleHeader.Hidden = true

table := Table{
Content: "content",
Content: "errands",

Header: []Header{
NewHeader("Header1"),
Expand All @@ -495,7 +539,7 @@ OtherHeader2|r2c2....|
Header1|v1|
Header2|v2|
2 content
1 errands
`))
})

Expand Down
4 changes: 4 additions & 0 deletions ui/table/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (t ValueString) Compare(other Value) int {
}
}

func (t EmptyValue) String() string { return "" }
func (t EmptyValue) Value() Value { return t }
func (t EmptyValue) Compare(Value) int { return 0 }

func NewValueStrings(s []string) ValueStrings { return ValueStrings{S: s} }

func (t ValueStrings) String() string { return strings.Join(t.S, "\n") }
Expand Down
47 changes: 38 additions & 9 deletions ui/table/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package table
import (
"fmt"
"io"
"reflect"
"strings"
)

Expand All @@ -12,13 +13,19 @@ type Writer struct {
bgStr string
borderStr string

rows [][]writerCell
rows []writerRow
widths map[int]int
}

type writerCell struct {
Value Value
String string
Value Value
String string
IsEmpty bool
}

type writerRow struct {
Values []writerCell
IsSpacer bool
}

type hasCustomWriter interface {
Expand Down Expand Up @@ -51,10 +58,19 @@ func (w *Writer) Write(headers []Header, vals []Value) {
lines := strings.Split(cleanStr, "\n")

if len(lines) == 1 && lines[0] == "" {
rowsInCol = append(rowsInCol, writerCell{Value: val, String: w.emptyStr})
cell := writerCell{Value: val, String: w.emptyStr}

if reflect.TypeOf(val) == reflect.TypeOf(EmptyValue{}) {
cell.IsEmpty = true
}
rowsInCol = append(rowsInCol, cell)
} else {
for _, line := range lines {
rowsInCol = append(rowsInCol, writerCell{Value: val, String: line})
cell := writerCell{Value: val, String: line}
if reflect.TypeOf(val) == reflect.TypeOf(EmptyValue{}) {
cell.IsEmpty = true
}
rowsInCol = append(rowsInCol, cell)
}
}

Expand All @@ -76,23 +92,36 @@ func (w *Writer) Write(headers []Header, vals []Value) {
}

for i := 0; i < rowsToAdd; i++ {
var row []writerCell
var row writerRow

rowIsSeparator := true
for _, col := range colsWithRows {
if i < len(col) {
row = append(row, col[i])
row.Values = append(row.Values, col[i])
if !col[i].IsEmpty {
rowIsSeparator = false
}
} else {
row = append(row, writerCell{})
row.Values = append(row.Values, writerCell{})
}
}
row.IsSpacer = rowIsSeparator

w.rows = append(w.rows, row)
}
}

func (w *Writer) Flush() error {
for _, row := range w.rows {
for colIdx, col := range row {
if row.IsSpacer {
_, err := fmt.Fprintln(w.w)
if err != nil {
return err
}
continue
}

for colIdx, col := range row.Values {
if customWriter, ok := col.Value.(hasCustomWriter); ok {
_, err := customWriter.Fprintf(w.w, "%s", col.String)
if err != nil {
Expand Down

0 comments on commit 64a1d74

Please sign in to comment.