Tasks can be manually reordered
Tim Izzo tim@5ika.ch
Fri, 16 May 2025 18:15:09 +0200
M
keymap.go
→
keymap.go
@@ -13,6 +13,8 @@ Check key.Binding
Clean key.Binding Add key.Binding Edit key.Binding + OrderUp key.Binding + OrderDown key.Binding SortByPriority key.Binding SortByDate key.Binding }@@ -27,8 +29,8 @@ // FullHelp returns keybindings for the expanded help view. It's part of the
// key.Map interface. func (k keyMap) FullHelp() [][]key.Binding { return [][]key.Binding{ - {k.Check, k.Priority, k.Up}, - {k.Add, k.ClearPriority, k.Clean, k.Down}, + {k.Check, k.Priority, k.Up, k.Down, k.OrderUp, k.OrderDown}, + {k.Add, k.ClearPriority, k.Clean}, {k.SortByPriority, k.SortByDate, k.Quit}, } }@@ -44,7 +46,7 @@ key.WithHelp("↓|j", "Move down"),
), Priority: key.NewBinding( key.WithKeys("a", "b", "c", "d"), - key.WithHelp("a|b|c", "Set priority"), + key.WithHelp("a|b|c|d", "Set priority"), ), ClearPriority: key.NewBinding( key.WithKeys("z"),@@ -57,6 +59,14 @@ ),
Edit: key.NewBinding( key.WithKeys("e", "enter"), key.WithHelp("e|Enter", "Edit task"), + ), + OrderUp: key.NewBinding( + key.WithKeys("shift+up"), + key.WithHelp("shift+↑", "Move up task"), + ), + OrderDown: key.NewBinding( + key.WithKeys("shift+down"), + key.WithHelp("shift+↓", "Move down task"), ), SortByPriority: key.NewBinding( key.WithKeys("p"),
M
main.go
→
main.go
@@ -35,7 +35,6 @@ textInput textinput.Model
} func initialModel(config Config) model { - // New task input ti := textinput.New() ti.Focus()@@ -72,7 +71,6 @@ help: help.New(),
} } - } func (m model) Init() tea.Cmd {@@ -134,6 +132,25 @@ }
m.SaveTasks() + case key.Matches(msg, m.keys.OrderUp): + newCursor := m.cursor - 1 + if newCursor < 0 { + newCursor += len(m.tasks) + } + focusedTask := m.tasks[m.cursor] + m.tasks[m.cursor] = m.tasks[newCursor] + m.tasks[newCursor] = focusedTask + m.cursor = newCursor + m.SaveTasks() + + case key.Matches(msg, m.keys.OrderDown): + newCursor := (m.cursor + 1) % len(m.tasks) + focusedTask := m.tasks[m.cursor] + m.tasks[m.cursor] = m.tasks[newCursor] + m.tasks[newCursor] = focusedTask + m.cursor = newCursor + m.SaveTasks() + case key.Matches(msg, m.keys.SortByPriority): m.tasks.Sort(todo.SortCompletedDateAsc, todo.SortPriorityAsc) m.selected = make(map[int]struct{})@@ -242,7 +259,6 @@ switch m.interfaceState {
case List: if len(m.tasks) > 0 { - for i, task := range m.tasks { cursor := " "@@ -255,7 +271,6 @@ _, checked := m.selected[i]
styles := NewTextStyle() output += fmt.Sprintf("%s %s\n", cursor, styles.getTaskStyle(task, checked)) } - } else { output += "No tasks in file" }@@ -272,7 +287,6 @@ output += "\n\n Press ESC to go back.\n"
} return output - } func main() {