Browse Source

this is getting messy really fast

aeth 1 month ago
parent
commit
8a03c6c2ae
4 changed files with 86 additions and 10 deletions
  1. 49 0
      pkg/options.go
  2. 16 0
      pkg/savestate.go
  3. 1 0
      pkg/taskedit.go
  4. 20 10
      pkg/userinterface.go

+ 49 - 0
pkg/options.go

@@ -0,0 +1,49 @@
+package itashi
+
+import "text/template"
+
+type ShelfHome struct {
+	Items     []Option
+	Tasks     TaskShelf
+	TaskTempl *template.Template
+}
+
+// Return a list of the options as strings for the UI to render
+func (s ShelfHome) OptionList() []string {
+	var optnames []string
+	for i := range s.Items {
+		optnames = append(optnames, s.Items[i].Name)
+	}
+	return optnames
+
+}
+
+type Option struct {
+	Name     string             // the display name in the UI
+	Template *template.Template // The template to render in the Render() func
+
+}
+
+// Render the template stored in the Template struct field
+func (o Option) Render() string {
+	return "This is a placeholder"
+}
+
+// Create the task shelf homepage
+func GetShelfHome(save string) ShelfHome {
+	return ShelfHome{
+		Items: GetOptions(),
+		Tasks: NewFilesystemShelf(save),
+	}
+}
+
+// Removing this from GetShelfHome to allow for indirecting the data feed
+func GetOptions() []Option {
+	var opts []Option
+	opts = append(opts, Option{Name: "Add task to your shelf"})
+	opts = append(opts, Option{Name: "Edit Task"})
+	opts = append(opts, Option{Name: "Move task to done pile"})
+	opts = append(opts, Option{Name: "View my shelf"})
+	return opts
+
+}

+ 16 - 0
pkg/savestate.go

@@ -70,9 +70,19 @@ func GetTaskNames(t []Task) []string {
 type FilesystemShelf struct {
 	SaveLocation string
 	Template     *template.Template
+	TaskTempl    *template.Template
 	Tasks        []Task
 }
 
+func (t *FilesystemShelf) RenderTask(task Task) string {
+	var bw bytes.Buffer
+	err := t.TaskTempl.Execute(&bw, task)
+	if err != nil {
+		log.Fatal("Had a problem rendering this task.", task, err)
+	}
+	return bw.String()
+}
+
 /*
 Create a new filesystem shelf struct to reflect the filesystem shelf
 
@@ -84,9 +94,15 @@ func NewFilesystemShelf(save string) *FilesystemShelf {
 	if err != nil {
 		log.Fatal("Could not parse the shelf template! ", err)
 	}
+	tasktmpl, err := template.New("task").Parse(TASK_ITEM)
+	if err != nil {
+		log.Fatal("Couldnt parse task template. ", err)
+	}
+
 	shelf := &FilesystemShelf{
 		SaveLocation: save,
 		Template:     tmpl,
+		TaskTempl:    tasktmpl,
 		Tasks:        []Task{},
 	}
 	shelf.Tasks = shelf.GetAll()

+ 1 - 0
pkg/taskedit.go

@@ -0,0 +1 @@
+package itashi

+ 20 - 10
pkg/userinterface.go

@@ -7,6 +7,7 @@ import (
 	"bytes"
 	"fmt"
 	"log"
+	"math"
 	"os"
 	"strconv"
 	"text/template"
@@ -36,7 +37,7 @@ const HEADER_TEMPLATE = `
 const TASK_ITEM = `
 Title: {{.Title}}
 -------------------------
-   {{.Desc}}
+{{.Desc}}
 
 Due: {{.Due}}
 Priority: {{.Priority}}
@@ -46,6 +47,10 @@ Done?: {{.Priority}}
 
 const TIME_TO_TEMPLATE = `{{.Hours}}H, {{.Minutes}}M`
 
+const HOME_TEMPLATE = ``
+
+// TODO: put all templates in their own file
+
 type HeaderData struct {
 	Date          string
 	Season        string
@@ -145,8 +150,11 @@ func (u UserImplementation) getTimeToEod(ts time.Time) TimeToSunShift {
 	}
 	out := time.Date(ts.Year(), ts.Month(), ts.Day(), 17, 0, ts.Second(), ts.Nanosecond(), ts.Location())
 	dur := time.Until(out)
+	hours := dur.Minutes() / 60
+	hours = math.Floor(hours)
+	minutes := (hours * 60) - dur.Minutes()
 
-	return TimeToSunShift{Hours: int(dur.Hours()), Minutes: int(dur.Minutes())}
+	return TimeToSunShift{Hours: int(hours), Minutes: int(minutes)}
 }
 
 func (u UserImplementation) getTimeToSunShift(ts time.Time) TimeToSunShift {
@@ -195,15 +203,8 @@ type model struct {
 
 func InitialModel() model {
 	shelf := NewFilesystemShelf(FS_SAVE_LOCATION)
-	//	shelf.AddTask(Task{Id: 1, Title: "This is a sample task", Desc: "Quick sample task that im testing the storage with",
-	//		Due: time.Now().AddDate(0, 0, 2), Done: false, Priority: 1})
 	return model{
-		// Our to-do list is a grocery list
-		choices: GetTaskNames(shelf.GetAll()),
-
-		// A map which indicates which choices are selected. We're using
-		// the  map like a mathematical set. The keys refer to the indexes
-		// of the `choices` slice, above.
+		choices:  GetTaskNames(shelf.GetAll()),
 		selected: make(map[int]struct{}),
 	}
 }
@@ -247,6 +248,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			} else {
 				m.selected[m.cursor] = struct{}{}
 			}
+
 		}
 	}
 
@@ -261,6 +263,8 @@ func (m model) View() string {
 	if err != nil {
 		log.Fatal("Couldnt parse the header template.. sorry. ", err)
 	}
+	shelf := NewFilesystemShelf(FS_SAVE_LOCATION)
+
 	s := getHeader(UserImplementation{}, tmpl)
 
 	// Iterate over our choices
@@ -275,7 +279,13 @@ func (m model) View() string {
 		// Is this choice selected?
 		checked := " " // not selected
 		if _, ok := m.selected[i]; ok {
+			for x := range shelf.Tasks {
+				if shelf.Tasks[x].Title == choice {
+					return shelf.RenderTask(shelf.Tasks[x])
+				}
+			}
 			checked = "x" // selected!
+
 		}
 
 		// Render the row