feat: :sparkles: Add task edition
Tim Izzo tim@octree.ch
Sat, 12 Apr 2025 17:54:31 +0200
M
keymap.go
→
keymap.go
@@ -12,6 +12,7 @@ Quit key.Binding
Check key.Binding Clean key.Binding Add key.Binding + Edit key.Binding SortByPriority key.Binding SortByDate key.Binding }@@ -35,11 +36,11 @@
var keys = keyMap{ Up: key.NewBinding( key.WithKeys("up", "k"), - key.WithHelp("↑/k", "Move up"), + key.WithHelp("↑|k", "Move up"), ), Down: key.NewBinding( key.WithKeys("down", "j"), - key.WithHelp("↓/j", "Move down"), + key.WithHelp("↓|j", "Move down"), ), Priority: key.NewBinding( key.WithKeys("a", "b", "c", "d"),@@ -53,6 +54,10 @@ Add: key.NewBinding(
key.WithKeys("A", "+"), key.WithHelp("A|+", "Add new task"), ), + Edit: key.NewBinding( + key.WithKeys("e", "enter"), + key.WithHelp("e|Enter", "Edit task"), + ), SortByPriority: key.NewBinding( key.WithKeys("p"), key.WithHelp("p", "Sort by priority"),@@ -66,7 +71,7 @@ key.WithKeys("?"),
key.WithHelp("?", "More help"), ), Check: key.NewBinding( - key.WithKeys(" ", "enter"), + key.WithKeys(" ", "x"), key.WithHelp("space", "Check task"), ), Clean: key.NewBinding(
M
main.go
→
main.go
@@ -18,6 +18,7 @@
const ( List InterfaceState = iota Add + Edit ) type model struct {@@ -177,6 +178,10 @@ // Interface
case key.Matches(msg, m.keys.Add): m.interfaceState = Add + case key.Matches(msg, m.keys.Edit): + m.textInput.SetValue(m.tasks[m.cursor].Original) + m.interfaceState = Edit + case key.Matches(msg, m.keys.Quit): m.SaveTasks() return m, tea.Quit@@ -191,7 +196,7 @@
case Add: switch msg.Type { case tea.KeyCtrlC, tea.KeyEsc: - return m, tea.Quit + m.interfaceState = List case tea.KeyEnter: inputValue := m.textInput.Value() if inputValue != "" {@@ -207,7 +212,26 @@ m.textInput, cmd = m.textInput.Update(msg)
return m, cmd + case Edit: + switch msg.Type { + case tea.KeyCtrlC, tea.KeyEsc: + m.interfaceState = List + + case tea.KeyEnter: + inputValue := m.textInput.Value() + if inputValue != "" { + editedTask, _ := todo.ParseTask(inputValue) + m.tasks[m.cursor] = *editedTask + } + m.textInput.Reset() + m.interfaceState = List + } + + m.textInput, cmd = m.textInput.Update(msg) + + return m, cmd } + } return m, nil@@ -241,8 +265,12 @@
output += "\n" output += m.help.View(m.keys) case Add: - output += fmt.Sprintf("Nouvelle tâche:\n\n%s", m.textInput.View()) - output += "\n\n Press ESC to quit.\n" + output += fmt.Sprintf("New task:\n\n%s", m.textInput.View()) + output += "\n\n Press ESC to go back.\n" + + case Edit: + output += fmt.Sprintf("Edit task:\n\n%s", m.textInput.View()) + output += "\n\n Press ESC to go back.\n" } return output