diff options
| author | Paul Bütow <1224732+snonux@users.noreply.github.com> | 2025-06-20 18:32:58 +0300 |
|---|---|---|
| committer | Paul Bütow <1224732+snonux@users.noreply.github.com> | 2025-06-20 18:32:58 +0300 |
| commit | 4f073ba6a8cbd95368b8e0e7b9340b04448f0b4d (patch) | |
| tree | e16affb2a27cd88ccd61984453995c16cbeec542 /internal | |
| parent | cf2299f9d3a1d2095141d4c1b80b1e7632252ed0 (diff) | |
Add annotate hotkey
Diffstat (limited to 'internal')
| -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 |
3 files changed, 115 insertions, 1 deletions
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) + } +} |
