Skip to content

Commit

Permalink
fix: Fix a bug where editing while filtered cleared the displayed ite…
Browse files Browse the repository at this point in the history
…ms (#35)

* refactor: update dependencies

* go version upgrade

* fix: Fix a bug where editing while filtered cleared the displayed items

* _

* Update model.go

* Update model.go

* Update model.go

* _
  • Loading branch information
g026r authored Oct 9, 2024
1 parent e59e050 commit d63d832
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Better error handling? (Add a status bar to screens displaying last error?)
- Is the entry magic number really needed?
- Would `WriteAt` be useful? Probably not.
- What value does it save to the .sta file after it's been edited?
- Doesn't seem to affect the association, so I assume it's using cart signature for that.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/g026r/pocket-toolkit

go 1.23.1
go 1.23.2

require (
github.com/charmbracelet/bubbles v0.20.0
Expand Down
14 changes: 13 additions & 1 deletion pkg/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ import (
)

var EntrySort = func(a, b Entry) int {
return cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
if val := cmp.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)); val != 0 {
return val
}
if val := cmp.Compare(a.Crc32, b.Crc32); val != 0 {
return val
}
if val := cmp.Compare(a.Sig, b.Sig); val != 0 {
return val
}
if val := cmp.Compare(a.Magic, b.Magic); val != 0 {
return val
}
return cmp.Compare(a.System, b.System) // Technically system & magic number should be tied together. But you never know.
}

type Thumbnails struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ui/menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func generateSubMenu(l list.Model, items []list.Item, title string, width, heigh
l.Title = title
l.ResetSelected()
l.ResetFilter()
l.SetItems(items)
_ = l.SetItems(items) // Don't care that it potentially returns a FilterMatchMsg command as we're resetting everything.
// Need to reset height & width or else it doesn't display right the first time since the number of items in the list
// has changed from the initial WindowSizeMsg that's fired on startup
// It's overkill after it's displayed once, but an issue until then.
Expand Down
7 changes: 6 additions & 1 deletion pkg/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.gameList.SetHeight(msg.Height - 1)
m.gameList.SetWidth(msg.Width - 1)
return m, nil
case list.FilterMatchesMsg:
// Only m.gameList has filtering enabled, so don't need to check the current screen
listModel, cmd := m.gameList.Update(msg)
m.gameList = listModel
return m, cmd
case initDoneMsg:
m.initialized = true
m.Clear()
Expand Down Expand Up @@ -805,7 +810,7 @@ func (m *Model) saveEntry() (tea.Model, tea.Cmd) {
slices.SortFunc(m.entries, models.EntrySort)
var cmd tea.Cmd
if m.Peek() == EditScreen {
// Only reset the items rather than the whole list so we can keep our position in the list + the active filter
// Only reset the items rather than re-initialize the whole list so that we can keep our position in the list + the active filter
tmp := make([]list.Item, len(m.entries))
for i := range m.entries {
tmp[i] = m.entries[i]
Expand Down

0 comments on commit d63d832

Please sign in to comment.