[WIP] Editing

This commit is contained in:
Martin Pander
2024-05-23 16:01:25 +02:00
parent 7712711736
commit fe00170a5c
6 changed files with 133 additions and 24 deletions

View File

@ -1,3 +1,6 @@
// TODO: error handling
// TODO: split combinedOutput and handle stderr differently
// TODO: reorder functions
package taskwarrior
import (
@ -90,6 +93,8 @@ type TaskWarrior interface {
AddTask(task *Task) error
ImportTask(task *Task)
SetTaskDone(task *Task)
Undo()
}
type TaskSquire struct {
@ -138,6 +143,7 @@ func (ts *TaskSquire) GetTasks(report *Report, filter ...string) Tasks {
for _, context := range ts.contexts {
if context.Active && context.Name != "none" {
args = append(args, context.ReadFilter)
break
}
}
}
@ -160,13 +166,39 @@ func (ts *TaskSquire) GetTasks(report *Report, filter ...string) Tasks {
return nil
}
for _, task := range tasks {
if task.Depends != nil && len(task.Depends) > 0 {
ids := make([]string, len(task.Depends))
for i, dependUuid := range task.Depends {
ids[i] = ts.getIds([]string{fmt.Sprintf("uuid:%s", dependUuid)})
}
task.DependsIds = strings.Join(ids, " ")
}
}
return tasks
}
func (ts *TaskSquire) getIds(filter []string) string {
cmd := exec.Command(twBinary, append(append(ts.defaultArgs, filter...), "_ids")...)
out, err := cmd.CombinedOutput()
if err != nil {
slog.Error("Failed getting field:", err)
return ""
}
return strings.TrimSpace(string(out))
}
func (ts *TaskSquire) GetContext(context string) *Context {
ts.mutex.Lock()
defer ts.mutex.Unlock()
if context == "" {
context = "none"
}
if context, ok := ts.contexts[context]; ok {
return context
} else {
@ -276,6 +308,10 @@ func (ts *TaskSquire) SetContext(context *Context) error {
ts.mutex.Lock()
defer ts.mutex.Unlock()
if context.Name == "none" && ts.contexts["none"].Active {
return nil
}
cmd := exec.Command(twBinary, []string{"context", context.Name}...)
if err := cmd.Run(); err != nil {
slog.Error("Failed setting context:", err)
@ -355,6 +391,17 @@ func (ts *TaskSquire) SetTaskDone(task *Task) {
}
}
func (ts *TaskSquire) Undo() {
ts.mutex.Lock()
defer ts.mutex.Unlock()
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"undo"}...)...)
err := cmd.Run()
if err != nil {
slog.Error("Failed undoing task:", err)
}
}
func (ts *TaskSquire) extractConfig() *TWConfig {
cmd := exec.Command(twBinary, append(ts.defaultArgs, []string{"_show"}...)...)
output, err := cmd.CombinedOutput()