Skip to content

Commit

Permalink
Render missing data for columns with format string (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjli authored Oct 17, 2023
1 parent 6544304 commit 4423af9
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 6 deletions.
12 changes: 6 additions & 6 deletions table/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ func (m Model) renderRowColumnData(row Row, column Column, rowStyle lipgloss.Sty
} else if column.key == columnKeyOverflowLeft {
str = "<"
} else {
fmtString := "%v"

var data interface{}

if entry, exists := row.Data[column.key]; exists {
data = entry

if column.fmtString != "" {
fmtString = column.fmtString
}
} else if m.missingDataIndicator != nil {
data = m.missingDataIndicator
} else {
data = ""
}

fmtString := "%v"

if column.fmtString != "" {
fmtString = column.fmtString
}

switch entry := data.(type) {
case StyledCell:
str = fmt.Sprintf(fmtString, entry.Data)
Expand Down
132 changes: 132 additions & 0 deletions table/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,49 @@ func TestSimple3x3WithMissingData(t *testing.T) {
assert.Equal(t, expectedTable, rendered)
}

func TestFmtStringWithMissingData(t *testing.T) {
model := New([]Column{
NewColumn("1", "1", 4),
NewColumn("2", "2", 4).WithFormatString("%.1f"),
NewColumn("3", "3", 4),
})

rows := []Row{}

for rowIndex := 1; rowIndex <= 3; rowIndex++ {
rowData := RowData{}

for columnIndex := 1; columnIndex <= 3; columnIndex++ {
// Take out the center
if rowIndex == 2 && columnIndex == 2 {
continue
}

id := fmt.Sprintf("%d", columnIndex)

rowData[id] = float64(columnIndex) + float64(rowIndex)/10.0
}

rows = append(rows, NewRow(rowData))
}

model = model.WithRows(rows).WithStaticFooter("Footer")

const expectedTable = `┏━━━━┳━━━━┳━━━━┓
┃ 1┃ 2┃ 3┃
┣━━━━╋━━━━╋━━━━┫
┃ 1.1┃ 2.1┃ 3.1┃
┃ 1.2┃ ┃ 3.2┃
┃ 1.3┃ 2.3┃ 3.3┃
┣━━━━┻━━━━┻━━━━┫
┃ Footer┃
┗━━━━━━━━━━━━━━┛`

rendered := model.View()

assert.Equal(t, expectedTable, rendered)
}

func TestSimple3x3WithMissingIndicator(t *testing.T) {
model := New([]Column{
NewColumn("1", "1", 4),
Expand Down Expand Up @@ -504,6 +547,49 @@ func TestSimple3x3WithMissingIndicator(t *testing.T) {
assert.Equal(t, expectedTable, rendered)
}

func TestFmtStringWithMissingIndicator(t *testing.T) {
model := New([]Column{
NewColumn("1", "1", 4),
NewColumn("2", "2", 4).WithFormatString("%.1f"),
NewColumn("3", "3", 4),
}).WithMissingDataIndicator("XX")

rows := []Row{}

for rowIndex := 1; rowIndex <= 3; rowIndex++ {
rowData := RowData{}

for columnIndex := 1; columnIndex <= 3; columnIndex++ {
// Take out the center
if rowIndex == 2 && columnIndex == 2 {
continue
}

id := fmt.Sprintf("%d", columnIndex)

rowData[id] = float64(columnIndex) + float64(rowIndex)/10.0
}

rows = append(rows, NewRow(rowData))
}

model = model.WithRows(rows).WithStaticFooter("Footer")

const expectedTable = `┏━━━━┳━━━━┳━━━━┓
┃ 1┃ 2┃ 3┃
┣━━━━╋━━━━╋━━━━┫
┃ 1.1┃ 2.1┃ 3.1┃
┃ 1.2┃ XX┃ 3.2┃
┃ 1.3┃ 2.3┃ 3.3┃
┣━━━━┻━━━━┻━━━━┫
┃ Footer┃
┗━━━━━━━━━━━━━━┛`

rendered := model.View()

assert.Equal(t, expectedTable, rendered)
}

func TestSimple3x3WithMissingIndicatorStyled(t *testing.T) {
model := New([]Column{
NewColumn("1", "1", 4),
Expand Down Expand Up @@ -550,6 +636,52 @@ func TestSimple3x3WithMissingIndicatorStyled(t *testing.T) {
assert.Equal(t, expectedTable, rendered)
}

func TestFmtStringWithMissingIndicatorStyled(t *testing.T) {
model := New([]Column{
NewColumn("1", "1", 4),
NewColumn("2", "2", 4).WithFormatString("%.1f"),
NewColumn("3", "3", 4),
}).WithMissingDataIndicatorStyled(StyledCell{
Style: lipgloss.NewStyle().Align(lipgloss.Left),
Data: "XX",
})

rows := []Row{}

for rowIndex := 1; rowIndex <= 3; rowIndex++ {
rowData := RowData{}

for columnIndex := 1; columnIndex <= 3; columnIndex++ {
// Take out the center
if rowIndex == 2 && columnIndex == 2 {
continue
}

id := fmt.Sprintf("%d", columnIndex)

rowData[id] = float64(columnIndex) + float64(rowIndex)/10.0
}

rows = append(rows, NewRow(rowData))
}

model = model.WithRows(rows).WithStaticFooter("Footer")

const expectedTable = `┏━━━━┳━━━━┳━━━━┓
┃ 1┃ 2┃ 3┃
┣━━━━╋━━━━╋━━━━┫
┃ 1.1┃ 2.1┃ 3.1┃
┃ 1.2┃XX ┃ 3.2┃
┃ 1.3┃ 2.3┃ 3.3┃
┣━━━━┻━━━━┻━━━━┫
┃ Footer┃
┗━━━━━━━━━━━━━━┛`

rendered := model.View()

assert.Equal(t, expectedTable, rendered)
}

func TestPaged3x3WithNoSpecifiedFooter(t *testing.T) {
model := New([]Column{
NewColumn("1", "1", 4),
Expand Down

0 comments on commit 4423af9

Please sign in to comment.