cleanup model Update() a bit

and repeat less
This commit is contained in:
Lilian Jónsdóttir 2024-07-28 23:31:38 -07:00
parent 73a63db198
commit 1a03f7f23f

View file

@ -203,15 +203,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keys.invr): case key.Matches(msg, m.keys.invr):
m.invertSelection() m.invertSelection()
case key.Matches(msg, m.keys.clen): case key.Matches(msg, m.keys.clen):
if m.mode == modes.Interactive && len(m.selected) > 0 { return m.execute(modes.Cleaning)
m.mode = modes.Cleaning
return m.quit(false)
}
case key.Matches(msg, m.keys.rstr): case key.Matches(msg, m.keys.rstr):
if m.mode == modes.Interactive && len(m.selected) > 0 { return m.execute(modes.Restoring)
m.mode = modes.Restoring
return m.quit(false)
}
case key.Matches(msg, m.keys.sort): case key.Matches(msg, m.keys.sort):
m.sorting = m.sorting.Next() m.sorting = m.sorting.Next()
m.sort() m.sort()
@ -319,6 +313,18 @@ func (m model) quit(unselect_all bool) (model, tea.Cmd) {
return m, tea.Quit return m, tea.Quit
} }
func (m model) execute(mode modes.Mode) (model, tea.Cmd) {
if m.mode != modes.Interactive || len(m.selected) <= 0 {
var cmd tea.Cmd
return m, cmd
}
m.mode = mode
m.onlySelected()
m.table.SetStyles(makeUnselectedStyle())
return m, tea.Quit
}
func (m model) selectedFiles() (outfile files.Files) { func (m model) selectedFiles() (outfile files.Files) {
for _, file := range m.files { for _, file := range m.files {
if m.selected[file.String()] { if m.selected[file.String()] {