From bf30a97b8abefd550d795479af1781a53ad635c6 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 18:42:43 +0300 Subject: Add A hotkey to replace annotations --- internal/task/task.go | 23 +++++++++++++++++ internal/ui/table.go | 29 ++++++++++++++++++--- internal/ui/table_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 4 deletions(-) diff --git a/internal/task/task.go b/internal/task/task.go index 9b59cfb..f328838 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -175,6 +175,29 @@ func Denotate(id int, annoID int) error { return run(strconv.Itoa(id), "denotate", strconv.Itoa(annoID)) } +// ReplaceAnnotations removes all existing annotations from the task with the +// given id and sets a single annotation with the provided text. If text is +// empty, all annotations are simply removed. +func ReplaceAnnotations(id int, text string) error { + tasks, err := Export(strconv.Itoa(id)) + if err != nil { + return err + } + if len(tasks) == 0 { + return fmt.Errorf("task %d not found", id) + } + anns := tasks[0].Annotations + for i := len(anns); i >= 1; i-- { + if err := Denotate(id, i); err != nil { + return err + } + } + if text == "" { + return nil + } + return Annotate(id, text) +} + // 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 diff --git a/internal/ui/table.go b/internal/ui/table.go index a3f6581..a208d58 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -21,9 +21,10 @@ type Model struct { tbl atable.Model showHelp bool - annotating bool - annotateID int - annotateInput textinput.Model + annotating bool + annotateID int + annotateInput textinput.Model + replaceAnnotations bool filter string tasks []task.Task @@ -129,13 +130,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.annotating { switch msg.Type { case tea.KeyEnter: - task.Annotate(m.annotateID, m.annotateInput.Value()) + if m.replaceAnnotations { + task.ReplaceAnnotations(m.annotateID, m.annotateInput.Value()) + m.replaceAnnotations = false + } else { + task.Annotate(m.annotateID, m.annotateInput.Value()) + } m.annotating = false m.annotateInput.Blur() m.reload() return m, nil case tea.KeyEsc: m.annotating = false + m.replaceAnnotations = false m.annotateInput.Blur() return m, nil } @@ -185,6 +192,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if id, err := strconv.Atoi(idStr); err == nil { m.annotateID = id m.annotating = true + m.replaceAnnotations = false + m.annotateInput.SetValue("") + m.annotateInput.Focus() + return m, nil + } + } + case "A": + if row := m.tbl.SelectedRow(); row != nil { + idStr := ansi.Strip(row[0]) + if id, err := strconv.Atoi(idStr); err == nil { + m.annotateID = id + m.annotating = true + m.replaceAnnotations = true m.annotateInput.SetValue("") m.annotateInput.Focus() return m, nil @@ -210,6 +230,7 @@ func (m Model) View() string { "E: edit task", "s: toggle start/stop", "a: annotate task", + "A: replace annotations", "q: quit", "?: help", // show help toggle line ) diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go index d7b2da8..527eba6 100644 --- a/internal/ui/table_test.go +++ b/internal/ui/table_test.go @@ -62,3 +62,68 @@ func TestAnnotateHotkey(t *testing.T) { t.Fatalf("annotation not recorded: %q", data) } } + +func TestReplaceAnnotationHotkey(t *testing.T) { + tmp := t.TempDir() + taskPath := filepath.Join(tmp, "task") + annoFile := filepath.Join(tmp, "anno.txt") + logFile := filepath.Join(tmp, "log.txt") + + script := "#!/bin/sh\n" + + "if echo \"$@\" | grep -q export; then\n" + + " echo '{\"id\":1,\"uuid\":\"x\",\"description\":\"d\",\"status\":\"pending\",\"entry\":\"\",\"priority\":\"\",\"urgency\":0,\"annotations\":[{\"entry\":\"\",\"description\":\"old\"}]}'\n" + + " exit 0\n" + + "fi\n" + + "echo \"$@\" >> " + logFile + "\n" + + "if [ \"$1\" = \"1\" ] && [ \"$2\" = \"annotate\" ]; then\n" + + " echo \"$3\" > " + annoFile + "\n" + + " exit 0\n" + + "fi\n" + + if err := os.WriteFile(taskPath, []byte(script), 0o755); err != nil { + t.Fatal(err) + } + + origPath := os.Getenv("PATH") + os.Setenv("PATH", tmp+":"+origPath) + t.Cleanup(func() { os.Setenv("PATH", origPath) }) + + os.Setenv("TASKDATA", tmp) + os.Setenv("TASKRC", "/dev/null") + t.Cleanup(func() { + os.Unsetenv("TASKDATA") + os.Unsetenv("TASKRC") + }) + + m, err := New("") + if err != nil { + t.Fatalf("New: %v", err) + } + + mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'A'}}) + m = mv.(Model) + for _, r := range "new" { + mv, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}}) + m = mv.(Model) + } + mv, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + m = mv.(Model) + + data, err := os.ReadFile(annoFile) + if err != nil { + t.Fatalf("read ann: %v", err) + } + + if strings.TrimSpace(string(data)) != "new" { + t.Fatalf("annotation not recorded: %q", data) + } + + logData, err := os.ReadFile(logFile) + if err != nil { + t.Fatalf("read log: %v", err) + } + + if !strings.Contains(string(logData), "denotate") { + t.Fatalf("denotate not called: %s", logData) + } +} -- cgit v1.2.3