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

Fix bug where selecting a row while filtered would lose all other rows #171

Merged
merged 10 commits into from
May 3, 2024
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
13 changes: 9 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
# For whatever reason, 1.21.9 blows up because it can't
# find 'max' in some go lib... pinning to 1.21.4 fixes this
- name: Install Go
uses: actions/setup-go@v4
with:
version: v1.51.1
go-version: 1.21.4
- name: Checkout code
uses: actions/checkout@v4
- name: Lint
run: make lint

1 change: 1 addition & 0 deletions examples/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewModel() Model {
Filtered(true).
Focused(true).
WithPageSize(10).
SelectableRows(true).
WithRows([]table.Row{
table.NewRow(table.RowData{
columnKeyTitle: "Computer Systems : A Programmer's Perspective",
Expand Down
9 changes: 9 additions & 0 deletions table/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package table

import (
"fmt"
"sync/atomic"

"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wordwrap"
Expand All @@ -21,14 +22,22 @@ type Row struct {
Data RowData

selected bool

// id is an internal unique ID to match rows after they're copied
id uint32
}

var lastRowID uint32 = 1

// NewRow creates a new row and copies the given row data.
func NewRow(data RowData) Row {
row := Row{
Data: make(map[string]interface{}),
id: lastRowID,
}

atomic.AddUint32(&lastRowID, 1)

for key, val := range data {
// Doesn't deep copy val, but close enough for now...
row.Data[key] = val
Expand Down
15 changes: 10 additions & 5 deletions table/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ func (m *Model) toggleSelect() {
return
}

rows := make([]Row, len(m.GetVisibleRows()))
copy(rows, m.GetVisibleRows())
rows := m.GetVisibleRows()

currentSelectedState := rows[m.rowCursorIndex].selected
rowID := rows[m.rowCursorIndex].id

rows[m.rowCursorIndex].selected = !currentSelectedState
currentSelectedState := false

for i := range m.rows {
if m.rows[i].id == rowID {
currentSelectedState = m.rows[i].selected
m.rows[i].selected = !m.rows[i].selected
}
}

m.rows = rows
m.visibleRowCacheUpdated = false

m.appendUserEvent(UserEventRowSelectToggled{
Expand Down
79 changes: 79 additions & 0 deletions table/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,82 @@ func TestFilterWithKeypresses(t *testing.T) {

assert.Len(t, visible, 2)
}

// This is a long test with a lot of movement keys pressed, that's okay because
// it's simply repetitive and tracking the same kind of state change many times
//
//nolint:funlen
func TestSelectOnFilteredTableDoesntLoseRows(t *testing.T) {
// Issue: https://github.com/Evertras/bubble-table/issues/170
//
// Basically, if you filter a table and then select a row, then
// clear the filter, then all the other rows should still exist.

cols := []Column{
NewColumn("name", "Name", 10).WithFiltered(true),
}

model := New(cols).WithRows([]Row{
NewRow(RowData{"name": "Charmander"}),
NewRow(RowData{"name": "Pikachu"}),
}).Focused(true).Filtered(true).SelectableRows(true)

hitKey := func(key rune) {
model, _ = model.Update(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune{key},
})
}

hitEnter := func() {
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyEnter})
}

hitEscape := func() {
model, _ = model.Update(tea.KeyMsg{Type: tea.KeyEscape})
}

hitSpacebar := func() {
model, _ = model.Update(tea.KeyMsg{Type: tea.KeySpace})
}

// First, apply the filter
//
// Note that we try and filter for the second row, "Pikachu"
// so that we can better ensure everything is stably intact
visible := model.GetVisibleRows()

assert.Len(t, visible, 2)
hitKey(rune(model.KeyMap().Filter.Keys()[0][0]))
assert.Len(t, visible, 2)
hitKey('p')
hitKey('i')
hitKey('k')

visible = model.GetVisibleRows()

assert.Len(t, visible, 1)

hitEnter()

// Now apply the selection toggle
hitSpacebar()

visible = model.GetVisibleRows()
assert.Len(t, visible, 1)
assert.True(t, visible[0].selected)

// Now clear the filter and make sure everything is intact
hitEscape()

visible = model.GetVisibleRows()

assert.Len(t, visible, 2)

if t.Failed() {
return
}

assert.False(t, visible[0].selected)
assert.True(t, visible[1].selected)
}
Loading