From 4dd9b8a6340bb7d4d4d77599a42ee09e17c9bb92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Fri, 20 Jun 2025 09:51:52 +0300 Subject: Add external editor support --- internal/task/task.go | 11 ++++++++++- internal/ui/table.go | 21 +++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'internal') diff --git a/internal/task/task.go b/internal/task/task.go index f7a3f73..12671a9 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -142,6 +142,15 @@ func Denotate(id int, annoID int) error { } // Edit opens the task in an editor for manual modification. +// EditCmd returns an exec.Cmd that edits the task with the given id. +// The caller is responsible for running the command, typically via +// tea.ExecProcess so that the terminal state is properly managed. +func EditCmd(id int) *exec.Cmd { + return exec.Command("task", strconv.Itoa(id), "edit") +} + +// Edit opens the task in an editor for manual modification. +// This is a convenience wrapper around EditCmd. func Edit(id int) error { - return run(strconv.Itoa(id), "edit") + return EditCmd(id).Run() } diff --git a/internal/ui/table.go b/internal/ui/table.go index 4293500..bd61525 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -26,6 +26,16 @@ type Model struct { due int } +// editDoneMsg is emitted when the external editor process finishes. +type editDoneMsg struct{ err error } + +// editCmd returns a command that edits the task and sends an +// editDoneMsg once the process is complete. +func editCmd(id int) tea.Cmd { + c := task.EditCmd(id) + return tea.ExecProcess(c, func(err error) tea.Msg { return editDoneMsg{err: err} }) +} + // New creates a new UI model with the provided rows. func New(filter string) (Model, error) { m := Model{filter: filter} @@ -102,6 +112,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.tbl.SetWidth(msg.Width) m.tbl.SetHeight(msg.Height - 2) return m, nil + case editDoneMsg: + // Ignore any error and reload tasks once editing completes. + _ = msg.err + m.reload() + return m, nil case tea.KeyMsg: switch msg.String() { case "?": @@ -115,10 +130,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit case "E": if row := m.tbl.SelectedRow(); row != nil { - id, err := strconv.Atoi(row[0]) - if err == nil { - _ = task.Edit(id) - m.reload() + if id, err := strconv.Atoi(row[0]); err == nil { + return m, editCmd(id) } } } -- cgit v1.2.3