2024-02-26 23:08:42 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-17 17:56:38 -07:00
|
|
|
table "git.sealight.xyz/aynish/poms/table"
|
|
|
|
task "git.sealight.xyz/aynish/poms/task"
|
2024-05-19 17:32:30 -07:00
|
|
|
timer "git.sealight.xyz/aynish/poms/timer"
|
2024-05-17 17:56:38 -07:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2024-02-26 23:30:07 -08:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2024-05-17 17:56:38 -07:00
|
|
|
"log"
|
2024-02-26 23:30:07 -08:00
|
|
|
"os"
|
2024-05-17 17:56:38 -07:00
|
|
|
"os/exec"
|
2024-02-27 00:30:21 -08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-03-07 18:21:52 -08:00
|
|
|
"time"
|
2024-02-26 23:08:42 -08:00
|
|
|
)
|
|
|
|
|
2024-03-02 01:44:48 -08:00
|
|
|
var (
|
2024-05-17 17:56:38 -07:00
|
|
|
tasks = task.GetTasks()
|
2024-03-02 01:44:48 -08:00
|
|
|
columns = []table.Column{
|
2024-02-27 00:30:21 -08:00
|
|
|
{Title: "ID", Width: 4},
|
2024-05-08 14:50:32 -07:00
|
|
|
{Title: "Project", Width: 20},
|
2024-05-17 17:56:38 -07:00
|
|
|
{Title: "Tags", Width: 20},
|
|
|
|
{Title: "Description", Width: 35},
|
2024-02-27 00:30:21 -08:00
|
|
|
// {Title: "Urgency", Width: 10},
|
2024-02-26 23:30:07 -08:00
|
|
|
}
|
|
|
|
|
2024-03-02 01:44:48 -08:00
|
|
|
baseStyle = lipgloss.NewStyle().
|
2024-05-17 17:56:38 -07:00
|
|
|
BorderStyle(lipgloss.NormalBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("240"))
|
2024-03-02 01:44:48 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type model struct {
|
2024-05-17 17:56:38 -07:00
|
|
|
state string
|
2024-05-08 14:50:32 -07:00
|
|
|
taskList table.Model
|
2024-03-02 01:44:48 -08:00
|
|
|
}
|
|
|
|
|
2024-05-04 16:28:24 -07:00
|
|
|
func TasksToRows(tasks []task.Task) []table.Row {
|
2024-02-26 23:30:07 -08:00
|
|
|
rows := make([]table.Row, len(tasks))
|
2024-05-17 17:56:38 -07:00
|
|
|
for i := 0; i < len(tasks); i++ {
|
2024-02-27 00:30:21 -08:00
|
|
|
t := tasks[i]
|
2024-05-04 19:35:45 -07:00
|
|
|
rows[i] = []string{strconv.Itoa(t.Id), t.Project, strings.Join(t.Tags[:], " "), t.Description} // fmt.Sprintf("%f", t.Urgency)
|
2024-02-26 23:30:07 -08:00
|
|
|
}
|
2024-03-02 01:44:48 -08:00
|
|
|
return rows
|
|
|
|
}
|
2024-02-26 23:30:07 -08:00
|
|
|
|
2024-03-03 00:02:23 -08:00
|
|
|
func CreateTable() table.Model {
|
|
|
|
var t table.Model
|
|
|
|
rows := TasksToRows(tasks)
|
|
|
|
t = table.New(
|
|
|
|
table.WithColumns(columns),
|
|
|
|
table.WithRows(rows),
|
|
|
|
table.WithFocused(true),
|
|
|
|
table.WithHeight(15),
|
|
|
|
)
|
|
|
|
|
2024-02-26 23:30:07 -08:00
|
|
|
s := table.DefaultStyles()
|
|
|
|
s.Header = s.Header.
|
|
|
|
BorderStyle(lipgloss.NormalBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("240")).
|
|
|
|
BorderBottom(true).
|
|
|
|
Bold(false)
|
|
|
|
s.Selected = s.Selected.
|
|
|
|
Foreground(lipgloss.Color("229")).
|
|
|
|
Background(lipgloss.Color("57")).
|
|
|
|
Bold(false)
|
2024-05-04 19:06:55 -07:00
|
|
|
s.Active = s.Active.
|
|
|
|
Foreground(lipgloss.Color("0")).
|
|
|
|
Background(lipgloss.Color("120"))
|
2024-02-26 23:30:07 -08:00
|
|
|
t.SetStyles(s)
|
2024-03-02 01:44:48 -08:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2024-03-07 18:21:52 -08:00
|
|
|
type editorFinishedMsg struct{ err error }
|
|
|
|
|
|
|
|
func (m model) rowsToFile() []string {
|
|
|
|
var rows []string
|
2024-05-08 14:50:32 -07:00
|
|
|
for index, row := range m.taskList.Rows() {
|
|
|
|
if index < m.taskList.ActiveIndex() {
|
2024-03-07 18:21:52 -08:00
|
|
|
// get uuid
|
2024-05-04 19:35:45 -07:00
|
|
|
id := row[0]
|
2024-05-17 17:56:38 -07:00
|
|
|
uuid_byte, err := exec.Command("task", "_get", id+".uuid").Output()
|
2024-03-07 18:21:52 -08:00
|
|
|
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)
|
2024-05-04 19:35:45 -07:00
|
|
|
tags := strings.Replace(row[2], " ", ":", -1)
|
|
|
|
tags = ":" + row[1] + ":" + tags + ":"
|
2024-05-17 17:56:38 -07:00
|
|
|
fmt.Fprintln(f, "%title "+row[3])
|
2024-03-07 18:21:52 -08:00
|
|
|
fmt.Fprintln(f, tags)
|
2024-05-17 17:56:38 -07:00
|
|
|
fmt.Fprintln(f, "%date "+path)
|
2024-03-07 18:21:52 -08:00
|
|
|
fmt.Fprintln(f, "")
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// link to task-note from pom-note
|
2024-05-04 19:35:45 -07:00
|
|
|
rowClone := make(table.Row, len(row))
|
|
|
|
_ = copy(rowClone, row)
|
|
|
|
rowClone[3] = "[[../task-notes/" + uuid + ".wiki|" + row[3] + "]]"
|
|
|
|
rows = append(rows, strings.Join(row[1:], " - "))
|
2024-03-07 18:21:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) writeFile() string {
|
|
|
|
t := time.Now()
|
|
|
|
var path, full_path string
|
|
|
|
|
|
|
|
// create pom-note
|
|
|
|
path = t.Format(time.DateOnly)
|
|
|
|
full_path = "/home/anish/kitaab/vimwiki/poms/" + string(path) + ".wiki"
|
|
|
|
f, err := os.Create(full_path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2024-05-08 14:50:32 -07:00
|
|
|
fmt.Fprintln(f, "%title ")
|
|
|
|
fmt.Fprintln(f, ":pom:priv:plan:")
|
2024-05-17 17:56:38 -07:00
|
|
|
fmt.Fprintln(f, "%date "+path)
|
2024-05-08 14:50:32 -07:00
|
|
|
fmt.Fprintln(f, "")
|
2024-03-07 18:21:52 -08:00
|
|
|
|
|
|
|
rows := m.rowsToFile()
|
2024-05-17 17:56:38 -07:00
|
|
|
for _, value := range rows {
|
|
|
|
fmt.Fprintln(f, value) // print values to f, one per line
|
|
|
|
}
|
2024-03-07 18:21:52 -08:00
|
|
|
return full_path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) openEditor() tea.Cmd {
|
|
|
|
path := m.writeFile()
|
|
|
|
editor := os.Getenv("EDITOR")
|
|
|
|
if editor == "" {
|
|
|
|
editor = "vim"
|
|
|
|
}
|
|
|
|
c := exec.Command(editor, path) //nolint:gosec
|
|
|
|
return tea.ExecProcess(c, func(err error) tea.Msg {
|
|
|
|
return editorFinishedMsg{err}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-19 17:32:30 -07:00
|
|
|
// TODO
|
|
|
|
// This is where the data should be loaded...
|
2024-03-02 01:44:48 -08:00
|
|
|
func (m model) Init() tea.Cmd { return nil }
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmd tea.Cmd
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
2024-05-04 16:28:24 -07:00
|
|
|
case "q", "ctrl+c", "esc":
|
2024-03-02 01:44:48 -08:00
|
|
|
return m, tea.Quit
|
2024-03-07 18:21:52 -08:00
|
|
|
case "enter":
|
|
|
|
return m, m.openEditor()
|
2024-03-02 01:44:48 -08:00
|
|
|
}
|
2024-05-08 14:50:32 -07:00
|
|
|
case tea.WindowSizeMsg:
|
|
|
|
//top, right, bottom, left := baseStyle.GetMargin()
|
|
|
|
m.taskList.SetWidth(msg.Width - 5)
|
|
|
|
m.taskList.SetHeight(msg.Height - 5)
|
2024-03-02 01:44:48 -08:00
|
|
|
}
|
2024-05-08 14:50:32 -07:00
|
|
|
m.taskList, cmd = m.taskList.Update(msg)
|
2024-03-02 01:44:48 -08:00
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) View() string {
|
2024-05-08 14:50:32 -07:00
|
|
|
return baseStyle.Render(m.taskList.View())
|
2024-03-02 01:44:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-05-08 14:50:32 -07:00
|
|
|
var m model
|
|
|
|
m.taskList = CreateTable()
|
|
|
|
if _, err := tea.NewProgram(m).Run(); err != nil {
|
2024-02-26 23:30:07 -08:00
|
|
|
fmt.Println("Error running program:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-02-26 23:08:42 -08:00
|
|
|
}
|