From 74fc699fabd50e3072d81242a9ade62ef8af34bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Sun, 22 Jun 2025 14:04:53 +0300 Subject: Fix task modification marking done and swap hotkeys --- README.md | 6 ++--- internal/ui/table.go | 61 ++++++++++++++++++++++++----------------------- internal/ui/table_test.go | 6 ++--- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 05b03ca..f388dbb 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,9 @@ 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 - `r`: random due date - `R`: edit recurrence - `a`: annotate task diff --git a/internal/ui/table.go b/internal/ui/table.go index 3d26f41..a5c3e8e 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -78,10 +78,11 @@ type Model struct { editID int - blinkID int - blinkRow int - blinkOn bool - blinkCount int + blinkID int + blinkRow int + blinkOn bool + blinkCount int + blinkMarkDone bool cellExpanded bool @@ -124,8 +125,9 @@ func blinkCmd() tea.Cmd { return tea.Tick(blinkInterval, func(time.Time) tea.Msg { return blinkMsg{} }) } -func (m *Model) startBlink(id int) tea.Cmd { +func (m *Model) startBlink(id int, markDone bool) tea.Cmd { m.blinkID = id + m.blinkMarkDone = markDone m.blinkRow = -1 for i, tsk := range m.tasks { if tsk.ID == id { @@ -264,7 +266,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // Ignore any error and reload tasks once editing completes. _ = msg.err m.reload() - cmd := m.startBlink(m.editID) + cmd := m.startBlink(m.editID, false) m.editID = 0 return m, cmd case blinkMsg: @@ -274,16 +276,20 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.updateBlinkRow() if m.blinkCount >= blinkCycles { id := m.blinkID + mark := m.blinkMarkDone m.blinkID = 0 m.blinkOn = false m.blinkCount = 0 - for _, tsk := range m.tasks { - if tsk.ID == id { - m.undoStack = append(m.undoStack, tsk.UUID) - break + m.blinkMarkDone = false + if mark { + for _, tsk := range m.tasks { + if tsk.ID == id { + m.undoStack = append(m.undoStack, tsk.UUID) + break + } } + task.Done(id) } - task.Done(id) m.reload() return m, nil } @@ -303,7 +309,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.annotating = false m.annotateInput.Blur() m.reload() - cmd := m.startBlink(m.annotateID) + cmd := m.startBlink(m.annotateID, false) m.updateTableHeight() return m, cmd case tea.KeyEsc: @@ -324,7 +330,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.descEditing = false m.descInput.Blur() m.reload() - cmd := m.startBlink(m.descID) + cmd := m.startBlink(m.descID, false) m.updateTableHeight() return m, cmd case tea.KeyEsc: @@ -365,7 +371,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.tagsEditing = false m.tagsInput.Blur() m.reload() - cmd := m.startBlink(m.tagsID) + cmd := m.startBlink(m.tagsID, false) m.updateTableHeight() return m, cmd case tea.KeyEsc: @@ -384,7 +390,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { task.SetDueDate(m.dueID, m.dueDate.Format("2006-01-02")) m.dueEditing = false m.reload() - cmd := m.startBlink(m.dueID) + cmd := m.startBlink(m.dueID, false) m.updateTableHeight() return m, cmd case tea.KeyEsc: @@ -411,7 +417,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.recurEditing = false m.recurInput.Blur() m.reload() - cmd := m.startBlink(m.recurID) + cmd := m.startBlink(m.recurID, false) m.updateTableHeight() return m, cmd case tea.KeyEsc: @@ -430,7 +436,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { task.SetPriority(m.priorityID, priorityOptions[m.priorityIndex]) m.prioritySelecting = false m.reload() - cmd := m.startBlink(m.priorityID) + cmd := m.startBlink(m.priorityID, false) m.updateTableHeight() return m, cmd case tea.KeyEsc: @@ -547,20 +553,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { task.Start(id) } m.reload() - cmd := m.startBlink(id) + cmd := m.startBlink(id, false) return m, cmd } } - case "D": + case "d": if row := m.tbl.SelectedRow(); row != nil { idStr := ansi.Strip(row[1]) if id, err := strconv.Atoi(idStr); err == nil { - m.blinkID = id - m.blinkRow = m.tbl.Cursor() - m.blinkOn = true - m.blinkCount = 0 - m.updateBlinkRow() - return m, blinkCmd() + return m, m.startBlink(id, true) } } case "U": @@ -576,10 +577,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { break } } - cmd := m.startBlink(id) + cmd := m.startBlink(id, false) return m, cmd } - case "d": + case "D": if row := m.tbl.SelectedRow(); row != nil { idStr := ansi.Strip(row[1]) if id, err := strconv.Atoi(idStr); err == nil { @@ -598,7 +599,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { due := time.Now().AddDate(0, 0, days).Format("2006-01-02") task.SetDueDate(id, due) m.reload() - cmd := m.startBlink(id) + cmd := m.startBlink(id, false) return m, cmd } } @@ -801,9 +802,9 @@ func (m Model) View() string { "enter/i: edit or expand cell", "E: edit task", "s: toggle start/stop", - "D: mark task done", + "d: mark task done", "U: undo done", - "d: set due date", + "D: set due date", "r: random due date", "R: edit recurrence", "a: annotate task", diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go index 4e2679d..1daf7bf 100644 --- a/internal/ui/table_test.go +++ b/internal/ui/table_test.go @@ -162,7 +162,7 @@ func TestDoneHotkey(t *testing.T) { t.Fatalf("New: %v", err) } - mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'D'}}) + mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}}) m = mv.(Model) for i := 0; i < blinkCycles; i++ { mv, _ = m.Update(blinkMsg{}) @@ -211,7 +211,7 @@ func TestUndoHotkey(t *testing.T) { t.Fatalf("New: %v", err) } - mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'D'}}) + mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}}) m = mv.(Model) for i := 0; i < blinkCycles; i++ { mv, _ = m.Update(blinkMsg{}) @@ -269,7 +269,7 @@ func TestDueDateHotkey(t *testing.T) { t.Fatalf("New: %v", err) } - mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}}) + mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'D'}}) m = mv.(Model) for i := 0; i < 3; i++ { mv, _ = m.Update(tea.KeyMsg{Type: tea.KeyRight}) -- cgit v1.2.3 From e42bb79ed74cb975db0c4b8128b9014f0f6acf33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Sun, 22 Jun 2025 14:16:15 +0300 Subject: Add task creation hotkey --- README.md | 9 ++++--- internal/ui/table.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++- internal/ui/table_test.go | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f388dbb..bc8c7d6 100644 --- a/README.md +++ b/README.md @@ -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") -- cgit v1.2.3 From dc1a6e83669dd22be4542833970d3251741ba1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Sun, 22 Jun 2025 16:39:56 +0300 Subject: Fix add task hotkey --- go.mod | 1 + go.sum | 2 ++ internal/task/task.go | 23 +++++++++++++++++++++-- internal/ui/table.go | 2 +- internal/ui/table_test.go | 4 ++-- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 9086482..d05118a 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect github.com/charmbracelet/x/term v0.2.1 // indirect github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-localereader v0.0.1 // indirect diff --git a/go.sum b/go.sum index c89ca1f..5375c80 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,8 @@ github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQ github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= diff --git a/internal/task/task.go b/internal/task/task.go index 603002d..b20eef0 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -12,6 +12,8 @@ import ( "strconv" "strings" "time" + + "github.com/google/shlex" ) // Task represents a taskwarrior task as returned by `task export`. @@ -54,7 +56,7 @@ func SetDebugLog(path string) error { // Add creates a new task with the given description and tags. func Add(description string, tags []string) error { - args := []string{"add"} + var args []string for _, t := range tags { if len(t) > 0 && t[0] != '+' { t = "+" + t @@ -62,11 +64,28 @@ func Add(description string, tags []string) error { args = append(args, t) } args = append(args, description) + return AddArgs(args) +} - cmd := exec.Command("task", args...) +// AddArgs runs "task add" with the provided arguments. Each element in args +// is passed as a separate command-line argument, allowing the caller to +// specify additional modifiers like due dates or tags. +func AddArgs(args []string) error { + cmd := exec.Command("task", append([]string{"add"}, args...)...) return cmd.Run() } +// AddLine splits the given line into shell words and runs "task add" with the +// resulting arguments. This allows users to pass raw Taskwarrior parameters +// such as "due:today" directly. +func AddLine(line string) error { + fields, err := shlex.Split(line) + if err != nil { + return err + } + return AddArgs(fields) +} + // Export retrieves all tasks using `task export rc.json.array=off` and parses // the JSON output into a slice of Task structs. // Export retrieves tasks using `task export rc.json.array=off` and parses diff --git a/internal/ui/table.go b/internal/ui/table.go index 482d89e..c3e1748 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -484,7 +484,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { for _, tsk := range m.tasks { oldIDs[tsk.ID] = struct{}{} } - task.Add(m.addInput.Value(), nil) + task.AddLine(m.addInput.Value()) m.addingTask = false m.addInput.Blur() m.reload() diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go index f378973..365180c 100644 --- a/internal/ui/table_test.go +++ b/internal/ui/table_test.go @@ -476,7 +476,7 @@ func TestAddHotkey(t *testing.T) { mv, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'+'}}) m = mv.(Model) - for _, r := range "task" { + for _, r := range "foo due:today" { mv, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}}) m = mv.(Model) } @@ -488,7 +488,7 @@ func TestAddHotkey(t *testing.T) { t.Fatalf("read add: %v", err) } - if strings.TrimSpace(string(data)) != "add task" { + if strings.TrimSpace(string(data)) != "add foo due:today" { t.Fatalf("add not called: %q", data) } } -- cgit v1.2.3 From b140c36cd8bb3d20c7211fe672b9d5674c9f0d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Sun, 22 Jun 2025 16:50:46 +0300 Subject: Add generic task command hotkey --- README.md | 2 ++ internal/task/task.go | 18 ++++++++++++++ internal/ui/table.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++- internal/ui/table_test.go | 51 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bc8c7d6..8899bb9 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Task Samurai invokes the `task` command to read and modify tasks. The tasks are - `d`: mark task done - `U`: undo last done - `D`: set due date +- `T`: run task command - `+`: add task - `r`: random due date - `R`: edit recurrence @@ -57,4 +58,5 @@ Task Samurai invokes the `task` command to read and modify tasks. The tasks are - `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". +Example: press `T`, type `+bg modify +huhu` to tag all `+bg` tasks with `+huhu`. diff --git a/internal/task/task.go b/internal/task/task.go index b20eef0..5f19482 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -86,6 +86,24 @@ func AddLine(line string) error { return AddArgs(fields) } +// RunArgs executes "task" with the given arguments. Each item in args is +// passed as a separate command-line argument. +func RunArgs(args []string) error { + cmd := exec.Command("task", args...) + return cmd.Run() +} + +// RunLine splits the provided line into shell words and executes "task" with +// the resulting arguments. This allows callers to run arbitrary Taskwarrior +// commands directly. +func RunLine(line string) error { + fields, err := shlex.Split(line) + if err != nil { + return err + } + return RunArgs(fields) +} + // Export retrieves all tasks using `task export rc.json.array=off` and parses // the JSON output into a slice of Task structs. // Export retrieves tasks using `task export rc.json.array=off` and parses diff --git a/internal/ui/table.go b/internal/ui/table.go index c3e1748..480936c 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -64,6 +64,9 @@ type Model struct { addingTask bool addInput textinput.Model + runningTask bool + taskInput textinput.Model + searching bool searchInput textinput.Model searchRegex *regexp.Regexp @@ -167,6 +170,9 @@ func New(filters []string) (Model, error) { m.addInput = textinput.New() m.addInput.Prompt = "add: " + m.taskInput = textinput.New() + m.taskInput.Prompt = "task: " + m.defaultTheme = DefaultTheme() m.theme = m.defaultTheme @@ -517,6 +523,46 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.addInput, cmd = m.addInput.Update(msg) return m, cmd } + if m.runningTask { + switch msg.Type { + case tea.KeyEnter: + oldIDs := make(map[int]struct{}) + for _, tsk := range m.tasks { + oldIDs[tsk.ID] = struct{}{} + } + task.RunLine(m.taskInput.Value()) + m.runningTask = false + m.taskInput.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.runningTask = false + m.taskInput.Blur() + m.updateTableHeight() + return m, nil + } + var cmd tea.Cmd + m.taskInput, cmd = m.taskInput.Update(msg) + return m, cmd + } if m.searching { switch msg.Type { case tea.KeyEnter: @@ -710,6 +756,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.addInput.Focus() m.updateTableHeight() return m, nil + case "T": + m.runningTask = true + m.taskInput.SetValue("") + m.taskInput.Focus() + m.updateTableHeight() + return m, nil case "t": if row := m.tbl.SelectedRow(); row != nil { idStr := ansi.Strip(row[1]) @@ -853,6 +905,7 @@ func (m Model) View() string { m.tbl.HelpView(), "enter/i: edit or expand cell", "E: edit task", + "T: run task command", "+: add task", "s: toggle start/stop", "d: mark task done", @@ -934,6 +987,12 @@ func (m Model) View() string { m.addInput.View(), ) } + if m.runningTask { + view = lipgloss.JoinVertical(lipgloss.Left, + view, + m.taskInput.View(), + ) + } if m.searching { view = lipgloss.JoinVertical(lipgloss.Left, view, @@ -1247,7 +1306,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 || m.addingTask { + if m.annotating || m.dueEditing || m.prioritySelecting || m.searching || m.descEditing || m.tagsEditing || m.recurEditing || m.filterEditing || m.addingTask || m.runningTask { h-- } if h < 1 { diff --git a/internal/ui/table_test.go b/internal/ui/table_test.go index 365180c..d94243b 100644 --- a/internal/ui/table_test.go +++ b/internal/ui/table_test.go @@ -493,6 +493,57 @@ func TestAddHotkey(t *testing.T) { } } +func TestTaskHotkey(t *testing.T) { + tmp := t.TempDir() + taskPath := filepath.Join(tmp, "task") + cmdFile := filepath.Join(tmp, "cmd.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 \"$@\" > " + cmdFile + "\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{'T'}}) + m = mv.(Model) + for _, r := range "+bg modify +huhu" { + 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(cmdFile) + if err != nil { + t.Fatalf("read cmd: %v", err) + } + + if strings.TrimSpace(string(data)) != "+bg modify +huhu" { + t.Fatalf("task not called: %q", data) + } +} + func TestNavigationHotkeys(t *testing.T) { tmp := t.TempDir() taskPath := filepath.Join(tmp, "task") -- cgit v1.2.3 From 8a83fbf747bda27f5859ad84c2f20a9afb753e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Sun, 22 Jun 2025 16:58:00 +0300 Subject: Clean up unused RunArgs comments --- README.md | 2 -- internal/task/task.go | 18 -------------- internal/ui/table.go | 61 +---------------------------------------------- internal/ui/table_test.go | 51 --------------------------------------- 4 files changed, 1 insertion(+), 131 deletions(-) diff --git a/README.md b/README.md index 8899bb9..bc8c7d6 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,6 @@ Task Samurai invokes the `task` command to read and modify tasks. The tasks are - `d`: mark task done - `U`: undo last done - `D`: set due date -- `T`: run task command - `+`: add task - `r`: random due date - `R`: edit recurrence @@ -58,5 +57,4 @@ Task Samurai invokes the `task` command to read and modify tasks. The tasks are - `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". -Example: press `T`, type `+bg modify +huhu` to tag all `+bg` tasks with `+huhu`. diff --git a/internal/task/task.go b/internal/task/task.go index 5f19482..b20eef0 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -86,24 +86,6 @@ func AddLine(line string) error { return AddArgs(fields) } -// RunArgs executes "task" with the given arguments. Each item in args is -// passed as a separate command-line argument. -func RunArgs(args []string) error { - cmd := exec.Command("task", args...) - return cmd.Run() -} - -// RunLine splits the provided line into shell words and executes "task" with -// the resulting arguments. This allows callers to run arbitrary Taskwarrior -// commands directly. -func RunLine(line string) error { - fields, err := shlex.Split(line) - if err != nil { - return err - } - return RunArgs(fields) -} - // Export retrieves all tasks using `task export rc.json.array=off` and parses // the JSON output into a slice of Task structs. // Export retrieves tasks using `task export rc.json.array=off` and parses diff --git a/internal/ui/table.go b/internal/ui/table.go index 480936c..c3e1748 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -64,9 +64,6 @@ type Model struct { addingTask bool addInput textinput.Model - runningTask bool - taskInput textinput.Model - searching bool searchInput textinput.Model searchRegex *regexp.Regexp @@ -170,9 +167,6 @@ func New(filters []string) (Model, error) { m.addInput = textinput.New() m.addInput.Prompt = "add: " - m.taskInput = textinput.New() - m.taskInput.Prompt = "task: " - m.defaultTheme = DefaultTheme() m.theme = m.defaultTheme @@ -523,46 +517,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.addInput, cmd = m.addInput.Update(msg) return m, cmd } - if m.runningTask { - switch msg.Type { - case tea.KeyEnter: - oldIDs := make(map[int]struct{}) - for _, tsk := range m.tasks { - oldIDs[tsk.ID] = struct{}{} - } - task.RunLine(m.taskInput.Value()) - m.runningTask = false - m.taskInput.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.runningTask = false - m.taskInput.Blur() - m.updateTableHeight() - return m, nil - } - var cmd tea.Cmd - m.taskInput, cmd = m.taskInput.Update(msg) - return m, cmd - } if m.searching { switch msg.Type { case tea.KeyEnter: @@ -756,12 +710,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.addInput.Focus() m.updateTableHeight() return m, nil - case "T": - m.runningTask = true - m.taskInput.SetValue("") - m.taskInput.Focus() - m.updateTableHeight() - return m, nil case "t": if row := m.tbl.SelectedRow(); row != nil { idStr := ansi.Strip(row[1]) @@ -905,7 +853,6 @@ func (m Model) View() string { m.tbl.HelpView(), "enter/i: edit or expand cell", "E: edit task", - "T: run task command", "+: add task", "s: toggle start/stop", "d: mark task done", @@ -987,12 +934,6 @@ func (m Model) View() string { m.addInput.View(), ) } - if m.runningTask { - view = lipgloss.JoinVertical(lipgloss.Left, - view, - m.taskInput.View(), - ) - } if m.searching { view = lipgloss.JoinVertical(lipgloss.Left, view, @@ -1306,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 || m.addingTask || m.runningTask { + 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 d94243b..365180c 100644 --- a/internal/ui/table_test.go +++ b/internal/ui/table_test.go @@ -493,57 +493,6 @@ func TestAddHotkey(t *testing.T) { } } -func TestTaskHotkey(t *testing.T) { - tmp := t.TempDir() - taskPath := filepath.Join(tmp, "task") - cmdFile := filepath.Join(tmp, "cmd.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 \"$@\" > " + cmdFile + "\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{'T'}}) - m = mv.(Model) - for _, r := range "+bg modify +huhu" { - 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(cmdFile) - if err != nil { - t.Fatalf("read cmd: %v", err) - } - - if strings.TrimSpace(string(data)) != "+bg modify +huhu" { - t.Fatalf("task not called: %q", data) - } -} - func TestNavigationHotkeys(t *testing.T) { tmp := t.TempDir() taskPath := filepath.Join(tmp, "task") -- cgit v1.2.3 From 90f2d2efbf374f6ea82a46d2d701a099dbae1c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Sun, 22 Jun 2025 17:05:01 +0300 Subject: Center help screen --- internal/ui/table.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/ui/table.go b/internal/ui/table.go index c3e1748..47ab784 100644 --- a/internal/ui/table.go +++ b/internal/ui/table.go @@ -849,7 +849,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // View renders the table UI. func (m Model) View() string { if m.showHelp { - return lipgloss.JoinVertical(lipgloss.Left, + lines := []string{ m.tbl.HelpView(), "enter/i: edit or expand cell", "E: edit task", @@ -872,7 +872,11 @@ func (m Model) View() string { "esc: close help/search", "q: quit", "H: help", // show help toggle line - ) + } + for i, l := range lines { + lines[i] = centerLines(l, m.tbl.Width()) + } + return lipgloss.JoinVertical(lipgloss.Top, lines...) } view := lipgloss.JoinVertical(lipgloss.Left, m.topStatusLine(), @@ -1363,3 +1367,12 @@ func (m *Model) applyTheme() { m.tblStyles.Highlight = m.tblStyles.Highlight.Background(lipgloss.Color(m.theme.RowBG)).Foreground(lipgloss.Color(m.theme.RowFG)) m.tbl.SetStyles(m.tblStyles) } + +func centerLines(s string, width int) string { + lines := strings.Split(strings.TrimRight(s, "\n"), "\n") + style := lipgloss.NewStyle().Width(width).Align(lipgloss.Center) + for i, l := range lines { + lines[i] = style.Render(l) + } + return strings.Join(lines, "\n") +} -- cgit v1.2.3