fix panic when listing and restoring/cleaning wrong file

This commit is contained in:
Lilian Jónsdóttir 2024-07-30 19:44:02 -07:00
parent 0d952302d6
commit 8b01bffffe

View file

@ -262,7 +262,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keys.mark): case key.Matches(msg, m.keys.mark):
m.toggleItem(m.table.Cursor()) m.toggleItem(m.table.Cursor())
case key.Matches(msg, m.keys.doit): case key.Matches(msg, m.keys.doit):
if !m.readonly && m.mode != modes.Interactive { if !m.readonly && m.mode != modes.Interactive && len(m.fltrfiles) > 1 {
return m.quit(false) return m.quit(false)
} }
case key.Matches(msg, m.keys.nada): case key.Matches(msg, m.keys.nada):
@ -284,8 +284,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keys.fltr): case key.Matches(msg, m.keys.fltr):
m.filtering = true m.filtering = true
case key.Matches(msg, m.keys.clfl): case key.Matches(msg, m.keys.clfl):
if m.filter != "" {
m.filter = "" m.filter = ""
m.applyFilter() m.applyFilter()
}
case key.Matches(msg, m.keys.quit): case key.Matches(msg, m.keys.quit):
return m.quit(true) return m.quit(true)
} }
@ -297,10 +299,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m model) View() string { func (m model) View() string {
var panels []string
if !m.once {
panels = append(panels, m.header())
}
panels = append(panels, style.Render(m.table.View()), m.footer())
return lipgloss.JoinVertical(lipgloss.Top, return lipgloss.JoinVertical(lipgloss.Top,
m.header(), panels...,
style.Render(m.table.View()),
m.footer(),
) )
} }
@ -371,7 +378,7 @@ func (m model) header() string {
if m.filter != "" || m.filtering { if m.filter != "" || m.filtering {
title += " (filtered)" title += " (filtered)"
} }
right = fmt.Sprintf(" %s %d files in trash", title, len(m.table.Rows())) right = fmt.Sprintf(" %s %d files in trash", title, len(m.fltrfiles))
left = "" left = ""
} }
@ -403,7 +410,7 @@ func (m model) quit(unselectAll bool) (model, tea.Cmd) {
} }
func (m model) execute(mode modes.Mode) (model, tea.Cmd) { func (m model) execute(mode modes.Mode) (model, tea.Cmd) {
if m.mode != modes.Interactive || len(m.selected) <= 0 { if m.mode != modes.Interactive || len(m.selected) <= 0 || len(m.fltrfiles) <= 0 {
var cmd tea.Cmd var cmd tea.Cmd
return m, cmd return m, cmd
} }
@ -415,7 +422,7 @@ func (m model) execute(mode modes.Mode) (model, tea.Cmd) {
} }
func (m model) selectedFiles() (outfile files.Files) { func (m model) selectedFiles() (outfile files.Files) {
for _, file := range m.files { for _, file := range m.fltrfiles {
if m.selected[file.String()] { if m.selected[file.String()] {
outfile = append(outfile, file) outfile = append(outfile, file)
} }
@ -492,8 +499,8 @@ func (m *model) toggleItem(index int) (selected bool) {
return false return false
} }
name := m.files[index].String() name := m.fltrfiles[index].String()
size := m.files[index].Filesize() size := m.fltrfiles[index].Filesize()
// select the thing // select the thing
if v, ok := m.selected[name]; v && ok { if v, ok := m.selected[name]; v && ok {
@ -545,8 +552,8 @@ func (m *model) invertSelection() {
var newrows []table.Row var newrows []table.Row
for index, row := range m.table.Rows() { for index, row := range m.table.Rows() {
name := m.files[index].String() name := m.fltrfiles[index].String()
size := m.files[index].Filesize() size := m.fltrfiles[index].Filesize()
if v, ok := m.selected[name]; v && ok { if v, ok := m.selected[name]; v && ok {
delete(m.selected, name) delete(m.selected, name)
m.selectsize -= size m.selectsize -= size
@ -582,15 +589,19 @@ func (m *model) applyFilter() {
m.fltrfiles = m.filteredFiles() m.fltrfiles = m.filteredFiles()
var rows = []table.Row{} var rows = []table.Row{}
for _, file := range m.fltrfiles { for _, file := range m.fltrfiles {
r := newRow(file, m.workdir) row := newRow(file, m.workdir)
if !m.readonly { if !m.readonly {
r = append(r, getCheck(m.selected[file.String()])) row = append(row, getCheck(m.selected[file.String()]))
} }
rows = append(rows, r) rows = append(rows, row)
} }
if len(rows) == 0 { if len(rows) < 1 {
rows = append(rows, table.Row{"no files matched filter!", bar, bar, bar, uncheck}) row := table.Row{"no files matched filter!", bar, bar, bar}
if !m.readonly {
row = append(row, uncheck)
}
rows = append(rows, row)
} }
m.table.SetRows(rows) m.table.SetRows(rows)
m.updateTableHeight() m.updateTableHeight()