poms/task/task.go

104 lines
2.6 KiB
Go
Raw Normal View History

2024-05-04 16:28:24 -07:00
package task
import (
"encoding/json"
"sort"
2024-05-25 02:00:57 -07:00
"time"
"fmt"
"strconv"
"strings"
2024-05-04 16:28:24 -07:00
"log"
2024-05-25 02:00:57 -07:00
"os"
2024-05-04 16:28:24 -07:00
"os/exec"
2024-05-25 02:00:57 -07:00
table "git.sealight.xyz/aynish/poms/table"
2024-05-04 16:28:24 -07:00
)
// 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
}
2024-05-25 02:00:57 -07:00
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)
2024-05-25 02:00:57 -07:00
}
// 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], " ") {
2024-05-25 03:20:34 -07:00
rowClone[2] += "[[../indexs/" + tag + ".wiki|" + tag + "]]"
}
2024-05-25 02:00:57 -07:00
rowClone[3] = "[[../task-notes/" + uuid + ".wiki|" + row[3] + "]]"
rows = append(rows, strings.Join(rowClone[1:], " - "))
2024-05-25 02:00:57 -07:00
}
}
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()
}