bug(table): index out of range with varying row widths in StyleFunc #499
-
|
if you do a conditional check in func TestTableWithStyleFuncLinks(t *testing.T) {
headers := []string{"Package", "Version", "Link"}
data := [][]string{
{"sourcegit", "0.19", "https://aur.archlinux.org/packages/sourcegit-bin", ""},
{},
{"Welcome", "いらっしゃいませ", "مرحباً"},
{"Goodbye", "さようなら", "مع السلامة"},
}
table := New().
Headers(headers...).
Rows(data...).
StyleFunc(func(row, col int) lipgloss.Style {
if row == HeaderRow {
return lipgloss.NewStyle()
}
if strings.Contains(data[row][col], "https://") {
return lipgloss.NewStyle().Foreground(lipgloss.Color("#31BB71"))
}
return lipgloss.NewStyle()
}).
Width(60).
Wrap(true)
golden.RequireEqual(t, []byte(table.String()))
} |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
I'm not sure how we could help the user to avoid if i < len(data) && j < len(data[i]) {
// do something
} else {
// do nothing, index would overflow
}To improve, we might have to consider a whole different function signature to |
Beta Was this translation helpful? Give feedback.
-
|
I wonder if we should maybe fill data with empty content to have an equal # of columns for all rows. We render empty cells anyway in the table. e.g. data := [][]string{
{"sourcegit", "0.19", "https://aur.archlinux.org/packages/sourcegit-bin", ""},
{},
{"Welcome", "いらっしゃいませ", "مرحباً"},
{"Goodbye", "さようなら", "مع السلامة"},
}becomes data := [][]string{
{"sourcegit", "0.19", "https://aur.archlinux.org/packages/sourcegit-bin", ""},
{"", "", "", ""},
{"Welcome", "いらっしゃいませ", "مرحباً", ""},
{"Goodbye", "さようなら", "مع السلامة", ""},
}💭 |
Beta Was this translation helpful? Give feedback.
-
|
We would be mutating the data the user is providing. I don't think it's a good idea. |
Beta Was this translation helpful? Give feedback.
-
|
@andreynering ah true! We can keep it in the back of our minds then |
Beta Was this translation helpful? Give feedback.
-
|
I'm going to make this a discussion until we decide on a solution. Will keep it labelled as a bug though. |
Beta Was this translation helpful? Give feedback.
I'm not sure how we could help the user to avoid
panics here. In theory, this is something the user has to address:To improve, we might have to consider a whole different function signature to
styleFunc, but then it'd be a breaking change.