2 Commits fa1d2b5dab ... 8a03c6c2ae

Author SHA1 Message Date
  aeth 8a03c6c2ae this is getting messy really fast 1 month ago
  aeth 8e5583536c add task to todo list 1 month ago
6 changed files with 138 additions and 12 deletions
  1. 6 0
      Makefile
  2. 8 0
      cmd/main.go
  3. 49 0
      pkg/options.go
  4. 19 3
      pkg/savestate.go
  5. 1 0
      pkg/taskedit.go
  6. 55 9
      pkg/userinterface.go

+ 6 - 0
Makefile

@@ -0,0 +1,6 @@
+.PHONY: build
+
+
+
+build:
+	go build -o ./build/itashi ./cmd/main.go 

+ 8 - 0
cmd/main.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"flag"
 	"fmt"
 	"os"
 
@@ -9,6 +10,13 @@ import (
 )
 
 func main() {
+	addtask := flag.Bool("add", false, "add a task to the shelf")
+	flag.Parse()
+	shelf := itashi.NewFilesystemShelf(itashi.FS_SAVE_LOCATION)
+	if *addtask {
+		itashi.AddTaskPrompt(shelf)
+		os.Exit(0)
+	}
 	p := tea.NewProgram(itashi.InitialModel())
 
 	if _, err := p.Run(); err != nil {

+ 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
+
+}

+ 19 - 3
pkg/savestate.go

@@ -2,7 +2,6 @@ package itashi
 
 import (
 	"bytes"
-	"fmt"
 	"log"
 	"os"
 	"strconv"
@@ -71,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
 
@@ -85,11 +94,19 @@ func NewFilesystemShelf(save string) *FilesystemShelf {
 	if err != nil {
 		log.Fatal("Could not parse the shelf template! ", err)
 	}
-	return &FilesystemShelf{
+	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()
+	return shelf
 
 }
 
@@ -134,7 +151,6 @@ func parseFilesystemShelf(data []byte) []Task {
 	for i := range items {
 		sect := strings.Split(items[i], SHELF_COL_DELIM)
 		if len(sect) < 6 {
-			fmt.Printf("Length of item is not to spec.... length: %v. Item: %v\n", len(sect), sect)
 			continue
 		}
 		var id int

+ 1 - 0
pkg/taskedit.go

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

+ 55 - 9
pkg/userinterface.go

@@ -3,9 +3,13 @@
 package itashi
 
 import (
+	"bufio"
 	"bytes"
 	"fmt"
 	"log"
+	"math"
+	"os"
+	"strconv"
 	"text/template"
 	"time"
 
@@ -30,8 +34,23 @@ const HEADER_TEMPLATE = `
 {{.Time}} {{.Meridiem}} ({{.TtEod.Hours}}H, {{.TtEod.Minutes}}M -> EoD, {{.TtSun.Hours}}H, {{.TtSun.Minutes}}M -> {{.SunCycle}})
 `
 
+const TASK_ITEM = `
+Title: {{.Title}}
+-------------------------
+{{.Desc}}
+
+Due: {{.Due}}
+Priority: {{.Priority}}
+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
@@ -131,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 {
@@ -181,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{}),
 	}
 }
@@ -233,6 +248,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 			} else {
 				m.selected[m.cursor] = struct{}{}
 			}
+
 		}
 	}
 
@@ -247,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
@@ -261,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
@@ -274,3 +298,25 @@ func (m model) View() string {
 	// Send the UI for rendering
 	return s
 }
+
+/*
+Add task to the shelf
+*/
+func AddTaskPrompt(shelf TaskShelf) {
+	task := &Task{}
+	var reader *bufio.Reader
+	reader = bufio.NewReader(os.Stdout)
+	fmt.Print("Enter Task Title: ")
+	task.Title, _ = reader.ReadString('\n')
+	fmt.Print("Task description: ")
+	task.Desc, _ = reader.ReadString('\n')
+	fmt.Print("Priority: ")
+	priority, _ := reader.ReadString('\n')
+	pri, err := strconv.Atoi(priority)
+	if err != nil {
+		fmt.Print("non-real number sry\n")
+	}
+	task.Priority = pri
+
+	shelf.AddTask(*task)
+}