poms/task/task.go
2024-05-25 03:22:17 -07:00

104 lines
2.6 KiB
Go

package task
import (
"encoding/json"
"sort"
"time"
"fmt"
"strconv"
"strings"
"log"
"os"
"os/exec"
table "git.sealight.xyz/aynish/poms/table"
)
// Task representation.
type Task struct {
Id int `json:"id"`
Description string `json:"description"`
Tags []string `json:"tags"`
Project string `json:"project"`
Status string `json:"status"`
Uuid string `json:"uuid"`
Urgency float32 `json:"urgency"`
Priority string `json:"priority"`
Due string `json:"due"`
End string `json:"end"`
Entry string `json:"entry"`
Modified string `json:"modified"`
}
func GetTasks() []Task {
out, err := exec.Command("task", "(status:pending -backlog -BLOCKED -idea)", "export").Output()
if err != nil {
log.Fatal(err)
}
var tasks []Task
err = json.Unmarshal([]byte(out), &tasks)
if err != nil {
log.Fatal(err)
}
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].Urgency > tasks[j].Urgency
})
return tasks
}
func TasksToRows(tasks []Task) []table.Row {
rows := make([]table.Row, len(tasks))
for i := 0; i < len(tasks); i++ {
t := tasks[i]
rows[i] = []string{strconv.Itoa(t.Id), t.Project, strings.Join(t.Tags[:], " "), t.Description} // fmt.Sprintf("%f", t.Urgency)
}
return rows
}
func RowsToFile(taskList []table.Row, activeIndex int) []string {
var rows []string
for index, row := range taskList {
if index < activeIndex {
// get uuid
id := row[0]
uuid_byte, err := exec.Command("task", "_get", id+".uuid").Output()
if err != nil {
log.Fatal(err)
}
uuid := string(uuid_byte[:len(uuid_byte)-1])
// create task-note if it doesn't exist
note_path := "/home/anish/kitaab/vimwiki/task-notes/" + uuid + ".wiki"
if _, err := os.Stat(note_path); os.IsNotExist(err) {
CreateTaskNote(note_path, row)
}
// link to task-note from pom-note
rowClone := make(table.Row, len(row))
_ = copy(rowClone, row)
// TODO add links to indexs of EACH tag
rowClone[2] = ""
for _, tag := range strings.Split(row[2], " ") {
rowClone[2] += "[[../indexs/" + tag + ".wiki|" + tag + "]]"
}
rowClone[3] = "[[../task-notes/" + uuid + ".wiki|" + row[3] + "]]"
rows = append(rows, strings.Join(rowClone[1:], " - "))
}
}
return rows
}
func CreateTaskNote(note_path string, row table.Row) {
t := time.Now()
f, err := os.Create(note_path)
if err != nil {
log.Fatal(err)
}
tags := strings.Replace(row[2], " ", ":", -1)
tags = ":" + row[1] + ":" + tags + ":"
fmt.Fprintln(f, "%title "+row[3])
fmt.Fprintln(f, tags)
fmt.Fprintln(f, "%date "+t.Format("2006-01-02 15:06"))
fmt.Fprintln(f, "")
defer f.Close()
}