Skip to content
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

Render missing data for columns with format string #155

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -434,6 +434,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 @@ -484,6 +527,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 @@ -530,6 +616,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