fix: selected works as intended

This commit is contained in:
Anish Lakhwara 2024-05-04 18:21:17 -07:00
parent d920020d24
commit 9aeb85a771

View file

@ -325,26 +325,31 @@ func (m *Model) SetColumns(c []Column) {
// Update row to selected // Update row to selected
func (m *Model) SwitchRow() { func (m *Model) SwitchRow() {
// Get selected row // Get selected row
selected := m.SelectedRow() selectedRow := m.SelectedRow()
if (selected[0] == "❌") { if (selectedRow[0] == "❌") {
selected[0] = "✅" selectedRow[0] = "✅"
} else { } else {
selected[0] = "❌" selectedRow[0] = "❌"
} }
removedRows := remove(m.rows, m.cursor) removedRows := remove(m.rows, m.cursor)
m.cursor = clamp(m.cursor - 1, 0, len(removedRows) + 1)
// If cursor outside selection (adding selection) // If cursor outside selection (adding selection)
// We check equals because if it's on the border, we just moved into selection // We check equals because if it's on the border, we just moved into selection
// and are still adding // and are still adding
if m.cursor >= m.selected { if m.cursor >= m.selected {
newSelection := append(removedRows[:m.selected], selected) newSelection := make([]Row, len(removedRows) + 1)
m.rows = append(newSelection, removedRows[m.selected+1:]...) newSelection = append(removedRows[:m.selected+1], removedRows[m.selected:]...)
newSelection[m.selected] = selectedRow
m.rows = newSelection
m.selected += 1 m.selected += 1
} else { } else {
m.selected -= 1 m.selected -= 1
m.rows = append(removedRows, selected) newSelection := make([]Row, len(removedRows) + 1)
newSelection = append(removedRows[:m.selected+1], removedRows[m.selected:]...)
newSelection[m.selected] = selectedRow
m.rows = newSelection
} }
m.UpdateViewport() m.UpdateViewport()