Skip to content

Commit

Permalink
fix: ctrl+a to reverse selection
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 5, 2024
1 parent ffd1e7d commit d3e5dd9
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "g", "home":
m.index = 0
m.paginator.Page = 0
case "a":
case "a", "A", "ctrl+a":
if m.limit <= 1 {
break
}
for i := range m.items {
if m.numSelected >= m.limit {
break // do not exceed given limit
}
if m.items[i].selected {
continue
}
m.items[i].selected = true
m.items[i].order = m.currentOrder
m.numSelected++
m.currentOrder++
if m.numSelected < len(m.items) && m.numSelected < m.limit {
m = m.selectAll()
} else {
m = m.deselectAll()
}
case "A":
if m.limit <= 1 {
break
}
for i := range m.items {
m.items[i].selected = false
m.items[i].order = 0
}
m.numSelected = 0
m.currentOrder = 0
case "ctrl+c", "esc":
m.aborted = true
m.quitting = true
Expand Down Expand Up @@ -165,6 +148,32 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

func (m model) selectAll() model {
for i := range m.items {
if m.numSelected >= m.limit {
break // do not exceed given limit
}
if m.items[i].selected {
continue
}
m.items[i].selected = true
m.items[i].order = m.currentOrder
m.numSelected++
m.currentOrder++
}
return m
}

func (m model) deselectAll() model {
for i := range m.items {
m.items[i].selected = false
m.items[i].order = 0
}
m.numSelected = 0
m.currentOrder = 0
return m
}

func (m model) View() string {
if m.quitting {
return ""
Expand Down

0 comments on commit d3e5dd9

Please sign in to comment.