summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Bütow <1224732+snonux@users.noreply.github.com>2025-06-20 18:43:18 +0300
committerGitHub <noreply@github.com>2025-06-20 18:43:18 +0300
commita9e322ff7e83364868dc56fd9ac1d8acbc5ce08b (patch)
treef854d7a853229e13728368985bdba463b379d0c0
parent6acfce72b3ed9d814e546dc35ebfb45c8f833f41 (diff)
parentbf30a97b8abefd550d795479af1781a53ad635c6 (diff)
Merge pull request #26 from snonux/codex/add-a-hotkey-for-annotations-and-help
Add annotation replacement hotkey
-rw-r--r--internal/task/task.go23
-rw-r--r--internal/ui/table.go29
-rw-r--r--internal/ui/table_test.go65
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 4c5aa3d..755e077 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
@@ -130,13 +131,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
}
@@ -186,6 +193,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
@@ -211,6 +231,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)
+ }
+}