diff options
| author | Paul Bütow <1224732+snonux@users.noreply.github.com> | 2025-06-22 14:16:15 +0300 |
|---|---|---|
| committer | Paul Bütow <1224732+snonux@users.noreply.github.com> | 2025-06-22 14:16:15 +0300 |
| commit | e42bb79ed74cb975db0c4b8128b9014f0f6acf33 (patch) | |
| tree | 87e9e3e45325d1a0758e1b1a3088fac17ec1740b | |
| parent | 74fc699fabd50e3072d81242a9ade62ef8af34bf (diff) | |
Add task creation hotkey
| -rw-r--r-- | README.md | 9 | ||||
| -rw-r--r-- | internal/ui/table.go | 61 | ||||
| -rw-r--r-- | internal/ui/table_test.go | 51 |
3 files changed, 117 insertions, 4 deletions
@@ -32,9 +32,10 @@ Task Samurai invokes the `task` command to read and modify tasks. The tasks are - `e` or `E`: edit task - `s`: toggle start/stop - - `d`: mark task done - - `U`: undo last done - - `D`: set due date +- `d`: mark task done +- `U`: undo last done +- `D`: set due date +- `+`: add task - `r`: random due date - `R`: edit recurrence - `a`: annotate task @@ -55,3 +56,5 @@ Task Samurai invokes the `task` command to read and modify tasks. The tasks are - `H`: toggle help - `q` or `esc`: close search/help or quit (press `q` when nothing is open) +Example: press `+`, type `Buy milk` and hit Enter to add a new task called "Buy milk". + diff --git a/internal/ui/table.go b/internal/ui/table.go index a5c3e8e..482d89e 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -61,6 +61,9 @@ type Model struct { filterEditing bool filterInput textinput.Model + addingTask bool + addInput textinput.Model + searching bool searchInput textinput.Model searchRegex *regexp.Regexp @@ -161,6 +164,9 @@ func New(filters []string) (Model, error) { m.filterInput = textinput.New() m.filterInput.Prompt = "filter: " + m.addInput = textinput.New() + m.addInput.Prompt = "add: " + m.defaultTheme = DefaultTheme() m.theme = m.defaultTheme @@ -471,6 +477,46 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.filterInput, cmd = m.filterInput.Update(msg) return m, cmd } + if m.addingTask { + switch msg.Type { + case tea.KeyEnter: + oldIDs := make(map[int]struct{}) + for _, tsk := range m.tasks { + oldIDs[tsk.ID] = struct{}{} + } + task.Add(m.addInput.Value(), nil) + m.addingTask = false + m.addInput.Blur() + m.reload() + var newID int + row := -1 + for i, tsk := range m.tasks { + if _, ok := oldIDs[tsk.ID]; !ok { + newID = tsk.ID + row = i + break + } + } + m.updateTableHeight() + if row >= 0 { + prevRow := m.tbl.Cursor() + prevCol := m.tbl.ColumnCursor() + m.tbl.SetCursor(row) + m.tbl.SetColumnCursor(7) + m.updateSelectionHighlight(prevRow, m.tbl.Cursor(), prevCol, m.tbl.ColumnCursor()) + return m, m.startBlink(newID, false) + } + return m, nil + case tea.KeyEsc: + m.addingTask = false + m.addInput.Blur() + m.updateTableHeight() + return m, nil + } + var cmd tea.Cmd + m.addInput, cmd = m.addInput.Update(msg) + return m, cmd + } if m.searching { switch msg.Type { case tea.KeyEnter: @@ -658,6 +704,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.filterInput.Focus() m.updateTableHeight() return m, nil + case "+": + m.addingTask = true + m.addInput.SetValue("") + m.addInput.Focus() + m.updateTableHeight() + return m, nil case "t": if row := m.tbl.SelectedRow(); row != nil { idStr := ansi.Strip(row[1]) @@ -801,6 +853,7 @@ func (m Model) View() string { m.tbl.HelpView(), "enter/i: edit or expand cell", "E: edit task", + "+: add task", "s: toggle start/stop", "d: mark task done", "U: undo done", @@ -875,6 +928,12 @@ func (m Model) View() string { m.filterInput.View(), ) } + if m.addingTask { + view = lipgloss.JoinVertical(lipgloss.Left, + view, + m.addInput.View(), + ) + } if m.searching { view = lipgloss.JoinVertical(lipgloss.Left, view, @@ -1188,7 +1247,7 @@ func (m *Model) updateTableHeight() { if m.cellExpanded { h-- } - if m.annotating || m.dueEditing || m.prioritySelecting || m.searching || m.descEditing || m.tagsEditing || m.recurEditing || m.filterEditing { + if m.annotating || m.dueEditing || m.prioritySelecting || m.searching || m.descEditing || m.tagsEditing || m.recurEditing || m.filterEditing || m.addingTask { h-- } if h < 1 { diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go index 1daf7bf..f378973 100644 --- a/internal/ui/table_test.go +++ b/internal/ui/table_test.go @@ -442,6 +442,57 @@ func TestPriorityHotkey(t *testing.T) { } } +func TestAddHotkey(t *testing.T) { + tmp := t.TempDir() + taskPath := filepath.Join(tmp, "task") + addFile := filepath.Join(tmp, "add.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}'\n" + + " exit 0\n" + + "fi\n" + + "echo \"$@\" > " + addFile + "\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(nil) + if err != nil { + t.Fatalf("New: %v", err) + } + + mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'+'}}) + m = mv.(Model) + for _, r := range "task" { + 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(addFile) + if err != nil { + t.Fatalf("read add: %v", err) + } + + if strings.TrimSpace(string(data)) != "add task" { + t.Fatalf("add not called: %q", data) + } +} + func TestNavigationHotkeys(t *testing.T) { tmp := t.TempDir() taskPath := filepath.Join(tmp, "task") |
