Skip to content

Commit d9f4bb2

Browse files
authored
Merge pull request #50 from jrodriigues/master
Add feature to allow editing an existing task
2 parents 9f4e5f5 + 2743c0f commit d9f4bb2

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

model/todo.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ func (tl *TodoList) findIndexByID(id int) int {
102102
return -1
103103
}
104104

105+
func (tl *TodoList) Edit(id int, newTitle string) bool {
106+
idx := tl.findIndexByID(id)
107+
if idx == -1 {
108+
return false
109+
}
110+
tl.Todos[idx].Title = newTitle
111+
return true
112+
}
113+
105114
func (tl *TodoList) Toggle(id int) bool {
106115
idx := tl.findIndexByID(id)
107116
if idx == -1 {

ui/model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
ModeDeleteConfirm
1515
ModeArchiveConfirm
1616
ModeAddTask
17+
ModeEditTask
1718
)
1819

1920
type TodoTableModel struct {
@@ -23,6 +24,7 @@ type TodoTableModel struct {
2324
confirmAction string
2425
actionTitle string
2526
viewTaskID int
27+
editTaskID int
2628
width int
2729
height int
2830
selectedTodoIDs map[int]bool

ui/update.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ func (m TodoTableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
113113
}
114114
m.textInput, cmd = m.textInput.Update(msg)
115115
return m, cmd
116+
case ModeEditTask:
117+
switch msg := msg.(type) {
118+
case tea.KeyMsg:
119+
switch msg.String() {
120+
case "enter":
121+
title := strings.TrimSpace(m.textInput.Value())
122+
if title != "" {
123+
m.todoList.Edit(m.editTaskID, title)
124+
m.updateRows()
125+
m.SetStatusMessage("Task updated")
126+
}
127+
m.textInput.Reset()
128+
m.mode = ModeNormal
129+
return m, m.forceRelayoutCmd()
130+
case "esc":
131+
m.textInput.Reset()
132+
m.mode = ModeNormal
133+
return m, nil
134+
}
135+
}
136+
m.textInput, cmd = m.textInput.Update(msg)
137+
return m, cmd
116138
case ModeNormal:
117139
switch msg := msg.(type) {
118140
case tea.KeyMsg:
@@ -249,6 +271,22 @@ func (m TodoTableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
249271
}
250272
}
251273
}
274+
case "e":
275+
if len(m.table.Rows()) > 0 {
276+
selectedTitle := m.table.SelectedRow()[1]
277+
cleanTitle := strings.ReplaceAll(selectedTitle, archivedStyle.Render(""), "")
278+
279+
for _, todo := range m.todoList.Todos {
280+
if strings.Contains(selectedTitle, todo.Title) || todo.Title == cleanTitle {
281+
m.editTaskID = todo.ID
282+
m.textInput.SetValue(todo.Title)
283+
m.textInput.Focus()
284+
m.mode = ModeEditTask
285+
return m, textinput.Blink
286+
}
287+
}
288+
}
289+
return m, nil
252290
case "a":
253291
m.mode = ModeAddTask
254292
m.textInput.Focus()

ui/view.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ func (m TodoTableModel) View() string {
5555
helpStyle.Render("Press Enter to save, Esc to cancel"))
5656
return fullScreenStyle.Width(m.width).Height(m.height).Render(inputView)
5757
}
58+
if m.mode == ModeEditTask {
59+
inputView := inputStyle.Render(
60+
inputPromptStyle.Render("Edit Task") + "\n\n" +
61+
m.textInput.View() + "\n\n" +
62+
helpStyle.Render("Press Enter to save, Esc to cancel"))
63+
return fullScreenStyle.Width(m.width).Height(m.height).Render(inputView)
64+
}
5865
if len(m.todoList.Todos) == 0 {
5966
return baseStyle.Render("No tasks found. Press 'a' to add a new task!")
6067
}
@@ -107,6 +114,7 @@ func (m TodoTableModel) View() string {
107114
"→ " + confirmBtnStyle.Render("t") + ": toggle completion" +
108115
"\n→ " + confirmBtnStyle.Render("n") + ": toggle archive/unarchive" +
109116
"\n→ " + confirmBtnStyle.Render("d") + ": delete" +
117+
"\n→ " + confirmBtnStyle.Render("e") + ": edit task" +
110118
"\n→ " + confirmBtnStyle.Render("space") + ": select" +
111119
"\n→ " + confirmBtnStyle.Render("enter") + ": view details" +
112120
"\n→ " + confirmBtnStyle.Render("a") + ": add new task" +

0 commit comments

Comments
 (0)