fix: move tasks to own file

This commit is contained in:
Anish Lakhwara 2024-05-25 02:00:57 -07:00
parent ffb5389f6b
commit bb9f171579
2 changed files with 62 additions and 52 deletions

60
main.go
View file

@ -4,14 +4,13 @@ import (
"fmt" "fmt"
table "git.sealight.xyz/aynish/poms/table" table "git.sealight.xyz/aynish/poms/table"
task "git.sealight.xyz/aynish/poms/task" task "git.sealight.xyz/aynish/poms/task"
timer "git.sealight.xyz/aynish/poms/timer" // timer "git.sealight.xyz/aynish/poms/timer"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/looplab/fsm"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"strconv"
"strings"
"time" "time"
) )
@ -31,22 +30,18 @@ var (
) )
type model struct { type model struct {
FSM *fsm.FSM
state string state string
taskList table.Model taskList table.Model
} }
func TasksToRows(tasks []task.Task) []table.Row { type LoadedTasksMsg struct {
rows := make([]table.Row, len(tasks)) TaskList table.Model
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 CreateTable() table.Model { func loadTasks() tea.Msg {
var t table.Model var t table.Model
rows := TasksToRows(tasks) rows := task.TasksToRows(tasks)
t = table.New( t = table.New(
table.WithColumns(columns), table.WithColumns(columns),
table.WithRows(rows), table.WithRows(rows),
@ -73,45 +68,6 @@ func CreateTable() table.Model {
type editorFinishedMsg struct{ err error } type editorFinishedMsg struct{ err error }
func (m model) rowsToFile() []string {
var rows []string
for index, row := range m.taskList.Rows() {
if index < m.taskList.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) {
f, err := os.Create(note_path)
if err != nil {
log.Fatal(err)
}
path := time.Now().Format(time.DateOnly)
tags := strings.Replace(row[2], " ", ":", -1)
tags = ":" + row[1] + ":" + tags + ":"
fmt.Fprintln(f, "%title "+row[3])
fmt.Fprintln(f, tags)
fmt.Fprintln(f, "%date "+path)
fmt.Fprintln(f, "")
f.Close()
}
// link to task-note from pom-note
rowClone := make(table.Row, len(row))
_ = copy(rowClone, row)
rowClone[3] = "[[../task-notes/" + uuid + ".wiki|" + row[3] + "]]"
rows = append(rows, strings.Join(row[1:], " - "))
}
}
return rows
}
func (m model) writeFile() string { func (m model) writeFile() string {
t := time.Now() t := time.Now()
var path, full_path string var path, full_path string
@ -129,7 +85,7 @@ func (m model) writeFile() string {
fmt.Fprintln(f, "%date "+path) fmt.Fprintln(f, "%date "+path)
fmt.Fprintln(f, "") fmt.Fprintln(f, "")
rows := m.rowsToFile() rows := task.RowsToFile(m.taskList.Rows(), m.taskList.ActiveIndex())
for _, value := range rows { for _, value := range rows {
fmt.Fprintln(f, value) // print values to f, one per line fmt.Fprintln(f, value) // print values to f, one per line
} }

View file

@ -3,8 +3,14 @@ package task
import ( import (
"encoding/json" "encoding/json"
"sort" "sort"
"time"
"fmt"
"strconv"
"strings"
"log" "log"
"os"
"os/exec" "os/exec"
table "git.sealight.xyz/aynish/poms/table"
) )
// Task representation. // Task representation.
@ -38,3 +44,51 @@ func GetTasks() []Task {
}) })
return tasks 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) {
f, err := os.Create(note_path)
if err != nil {
log.Fatal(err)
}
path := time.Now().Format(time.DateOnly)
tags := strings.Replace(row[2], " ", ":", -1)
tags = ":" + row[1] + ":" + tags + ":"
fmt.Fprintln(f, "%title "+row[3])
fmt.Fprintln(f, tags)
fmt.Fprintln(f, "%date "+path)
fmt.Fprintln(f, "")
f.Close()
}
// link to task-note from pom-note
rowClone := make(table.Row, len(row))
_ = copy(rowClone, row)
rowClone[3] = "[[../task-notes/" + uuid + ".wiki|" + row[3] + "]]"
rows = append(rows, strings.Join(row[1:], " - "))
}
}
return rows
}