create pom-note after selection

This commit is contained in:
Anish Lakhwara 2024-03-07 18:21:52 -08:00
parent 3967be6516
commit 0ec0e42210

81
main.go
View file

@ -13,6 +13,7 @@ import (
"os"
"strconv"
"strings"
"time"
)
// Task representation.
@ -76,6 +77,7 @@ func TasksToRows(tasks []Task) []table.Row {
return rows
}
// We needed to overwrite "space" being page down
func NewKeyMap() table.KeyMap {
return table.KeyMap{
LineUp: key.NewBinding(
@ -151,6 +153,81 @@ func (m model) SwitchRow() table.Model {
return m.all
}
type editorFinishedMsg struct{ err error }
func (m model) rowsToFile() []string {
var rows []string
for _, row := range m.all.Rows() {
if row[0] == "✅" {
// get uuid
id := row[1]
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[3], " ", ":", -1)
tags = ":" + row[2] + ":" + tags + ":"
fmt.Fprintln(f, "%title " + row[4])
fmt.Fprintln(f, tags)
fmt.Fprintln(f, "%date " + path)
fmt.Fprintln(f, "")
f.Close()
}
// link to task-note from pom-note
row[4] = "[[../task-notes/" + uuid + ".wiki|" + row[4] + "]]"
rows = append(rows, strings.Join(row[2:], " - "))
}
}
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()
fmt.Fprintln(f, "%title ")
fmt.Fprintln(f, ":pom:priv:plan:")
fmt.Fprintln(f, "%date " + path)
fmt.Fprintln(f, "")
rows := m.rowsToFile()
for _, value := range rows {
fmt.Fprintln(f, value) // print values to f, one per line
}
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}
})
}
func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@ -169,6 +246,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case " ":
m.all = m.SwitchRow()
return m, nil
case "enter":
return m, m.openEditor()
}
}
m.all, cmd = m.all.Update(msg)
@ -181,7 +260,7 @@ func (m model) View() string {
func main() {
m := model{CreateTable()}
if _, err := tea.NewProgram(m).Run(); err != nil {
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}