diff options
| author | Paul Bütow <1224732+snonux@users.noreply.github.com> | 2025-06-20 18:33:23 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-20 18:33:23 +0300 |
| commit | fd6ac6bf1c2269093ba9fa0b20365a31d12a7bfa (patch) | |
| tree | 3d127d6205b9c6b49197a0a45fd51269ace6fed8 | |
| parent | 20417e3024916a23a0378f72c1336ccbb56f0bae (diff) | |
| parent | 4f073ba6a8cbd95368b8e0e7b9340b04448f0b4d (diff) | |
Merge pull request #24 from snonux/codex/add--a--hotkey-for-task-annotation
Add annotation hotkey
| -rw-r--r-- | go.mod | 1 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | internal/task/task_test.go | 7 | ||||
| -rw-r--r-- | internal/ui/table.go | 45 | ||||
| -rw-r--r-- | internal/ui/table_test.go | 64 |
5 files changed, 118 insertions, 1 deletions
@@ -10,6 +10,7 @@ require ( ) require ( + github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect @@ -1,3 +1,5 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8= diff --git a/internal/task/task_test.go b/internal/task/task_test.go index e48011a..22233db 100644 --- a/internal/task/task_test.go +++ b/internal/task/task_test.go @@ -2,10 +2,14 @@ package task import ( "os" + "os/exec" "testing" ) func TestAddAndExport(t *testing.T) { + if _, err := exec.LookPath("task"); err != nil { + t.Skip("task command not available") + } tmp := t.TempDir() if err := os.Setenv("TASKDATA", tmp); err != nil { t.Fatal(err) @@ -56,6 +60,9 @@ func TestAddAndExport(t *testing.T) { } func TestModifyHelpers(t *testing.T) { + if _, err := exec.LookPath("task"); err != nil { + t.Skip("task command not available") + } tmp := t.TempDir() if err := os.Setenv("TASKDATA", tmp); err != nil { t.Fatal(err) diff --git a/internal/ui/table.go b/internal/ui/table.go index 51bd9e4..a3f6581 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -8,6 +8,7 @@ import ( "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" @@ -20,6 +21,10 @@ type Model struct { tbl atable.Model showHelp bool + annotating bool + annotateID int + annotateInput textinput.Model + filter string tasks []task.Task @@ -41,6 +46,8 @@ func editCmd(id int) tea.Cmd { // New creates a new UI model with the provided rows. func New(filter string) (Model, error) { m := Model{filter: filter} + m.annotateInput = textinput.New() + m.annotateInput.Prompt = "annotation: " if err := m.reload(); err != nil { return Model{}, err @@ -119,6 +126,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.reload() return m, nil case tea.KeyMsg: + if m.annotating { + switch msg.Type { + case tea.KeyEnter: + 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.annotateInput.Blur() + return m, nil + } + var cmd tea.Cmd + m.annotateInput, cmd = m.annotateInput.Update(msg) + return m, cmd + } switch msg.String() { case "?": m.showHelp = true @@ -155,6 +179,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.reload() } } + 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.annotateInput.SetValue("") + m.annotateInput.Focus() + return m, nil + } + } } } @@ -174,14 +209,22 @@ func (m Model) View() string { m.tbl.HelpView(), "E: edit task", "s: toggle start/stop", + "a: annotate task", "q: quit", "?: help", // show help toggle line ) } - return lipgloss.JoinVertical(lipgloss.Left, + view := lipgloss.JoinVertical(lipgloss.Left, m.tbl.View(), m.statusLine(), ) + if m.annotating { + view = lipgloss.JoinVertical(lipgloss.Left, + view, + m.annotateInput.View(), + ) + } + return view } func (m Model) statusLine() string { diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go new file mode 100644 index 0000000..d7b2da8 --- /dev/null +++ b/internal/ui/table_test.go @@ -0,0 +1,64 @@ +package ui + +import ( + "os" + "path/filepath" + "strings" + "testing" + + tea "github.com/charmbracelet/bubbletea" +) + +func TestAnnotateHotkey(t *testing.T) { + tmp := t.TempDir() + taskPath := filepath.Join(tmp, "task") + annoFile := filepath.Join(tmp, "anno.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\":[]}'\n" + + " exit 0\n" + + "fi\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 "note" { + 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)) != "note" { + t.Fatalf("annotation not recorded: %q", data) + } +} |
