dramatically speed up inverting selection on huge amounts of files

was okay on 10k-40k files, but 800k took 3 minutes
now 800k files is barely perceptibly longer than instantly
This commit is contained in:
Lilian Jónsdóttir 2024-06-19 21:17:55 -07:00
parent d79f0109fc
commit c509d8fa1f

View file

@ -51,7 +51,7 @@ var (
type model struct { type model struct {
table table.Model table table.Model
keys keyMap keys keyMap
selected []int selected map[int]bool
readonly bool readonly bool
termheight int termheight int
mode modes.Mode mode modes.Mode
@ -73,6 +73,7 @@ func newInfosModel(is trash.Infos, width, height int, readonly, preselected bool
readonly: readonly, readonly: readonly,
termheight: height, termheight: height,
mode: mode, mode: mode,
selected: map[int]bool{},
} }
) )
slices.SortStableFunc(is, trash.SortByTrashedReverse) slices.SortStableFunc(is, trash.SortByTrashedReverse)
@ -97,7 +98,7 @@ func newInfosModel(is trash.Infos, width, height int, readonly, preselected bool
r = append(r, getCheck(preselected)) r = append(r, getCheck(preselected))
} }
if preselected { if preselected {
m.selected = append(m.selected, j) m.selected[j] = true
} }
rows = append(rows, r) rows = append(rows, r)
} }
@ -132,6 +133,7 @@ func newFilesModel(fs files.Files, width, height int, readonly, preselected bool
keys: defaultKeyMap(), keys: defaultKeyMap(),
readonly: readonly, readonly: readonly,
mode: modes.Trashing, mode: modes.Trashing,
selected: map[int]bool{},
} }
) )
@ -157,7 +159,7 @@ func newFilesModel(fs files.Files, width, height int, readonly, preselected bool
r = append(r, getCheck(preselected)) r = append(r, getCheck(preselected))
} }
if preselected { if preselected {
m.selected = append(m.selected, j) m.selected[j] = true
} }
rows = append(rows, r) rows = append(rows, r)
} }
@ -381,13 +383,13 @@ func (m *model) toggle_item(index int) (selected bool) {
} }
// select the thing // select the thing
if slices.Contains(m.selected, index) { if v, ok := m.selected[index]; v && ok {
// already selected // already selected
m.selected = slices.DeleteFunc(m.selected, func(other int) bool { return index == other }) delete(m.selected, index)
selected = false selected = false
} else { } else {
// not selected // not selected
m.selected = append(m.selected, index) m.selected[index] = true
selected = true selected = true
} }
@ -401,9 +403,9 @@ func (m *model) select_all() {
return return
} }
m.selected = []int{} m.selected = map[int]bool{}
for i := range len(m.table.Rows()) { for i := range len(m.table.Rows()) {
m.selected = append(m.selected, i) m.selected[i] = true
} }
m.update_rows(true) m.update_rows(true)
} }
@ -413,7 +415,7 @@ func (m *model) unselect_all() {
return return
} }
m.selected = []int{} m.selected = map[int]bool{}
m.update_rows(false) m.update_rows(false)
} }
@ -421,8 +423,8 @@ func (m *model) invert_selection() {
var newrows []table.Row var newrows []table.Row
for index, row := range m.table.Rows() { for index, row := range m.table.Rows() {
if slices.Contains(m.selected, index) { if v, ok := m.selected[index]; v && ok {
m.selected = slices.DeleteFunc(m.selected, func(other int) bool { return index == other }) delete(m.selected, index)
newrows = append(newrows, table.Row{ newrows = append(newrows, table.Row{
row[0], row[0],
row[1], row[1],
@ -431,7 +433,7 @@ func (m *model) invert_selection() {
getCheck(false), getCheck(false),
}) })
} else { } else {
m.selected = append(m.selected, index) m.selected[index] = true
newrows = append(newrows, table.Row{ newrows = append(newrows, table.Row{
row[0], row[0],
row[1], row[1],
@ -451,7 +453,12 @@ func InfoTable(is trash.Infos, width, height int, readonly, preselected bool, mo
} else { } else {
m, ok := endmodel.(model) m, ok := endmodel.(model)
if ok { if ok {
return m.selected, m.mode, nil selected := make([]int, 0, len(m.selected))
for k := range m.selected {
selected = append(selected, k)
}
return selected, m.mode, nil
} else { } else {
return []int{}, 0, fmt.Errorf("model isn't the right type??") return []int{}, 0, fmt.Errorf("model isn't the right type??")
} }
@ -464,7 +471,12 @@ func FilesTable(fs files.Files, width, height int, readonly, preselected bool) (
} else { } else {
m, ok := endmodel.(model) m, ok := endmodel.(model)
if ok { if ok {
return m.selected, nil selected := make([]int, 0, len(m.selected))
for k := range m.selected {
selected = append(selected, k)
}
return selected, nil
} else { } else {
return []int{}, fmt.Errorf("model isn't the right type??") return []int{}, fmt.Errorf("model isn't the right type??")
} }