Browse Source

got some add modify and delete functionality

aeth 1 month ago
parent
commit
42d12bfb36
5 changed files with 125 additions and 26 deletions
  1. 6 0
      cmd/banner.txt
  2. 37 7
      cmd/main.go
  3. 66 7
      pkg/savestate.go
  4. 0 0
      pkg/sundown.go
  5. 16 12
      pkg/userinterface.go

+ 6 - 0
cmd/banner.txt

@@ -0,0 +1,6 @@
+    _ __             __    _ __
+   (_) /_____ ______/ /_  (_) /
+  / / __/ __ `/ ___/ __ \/ / / 
+ / / /_/ /_/ (__  ) / / / /_/  
+/_/\__/\__,_/____/_/ /_/_(_)   
+                               

+ 37 - 7
cmd/main.go

@@ -1,22 +1,52 @@
 package main
 
 import (
-	"flag"
+	_ "embed"
 	"fmt"
+	"log"
 	"os"
+	"strconv"
 
 	itashi "git.aetherial.dev/aeth/itashi/pkg"
 	tea "github.com/charmbracelet/bubbletea"
 )
 
+const ADD_TASK = "add"
+const DEL_TASK = "del"
+const DONE_TASK = "done"
+
+//go:embed banner.txt
+var banner []byte
+
 func main() {
-	addtask := flag.Bool("add", false, "add a task to the shelf")
-	flag.Parse()
-	shelf := itashi.NewFilesystemShelf(itashi.GetDefualtSave())
-	if *addtask {
-		itashi.AddTaskPrompt(shelf)
-		os.Exit(0)
+	args := os.Args
+	if len(args) >= 2 {
+		action := args[1]
+		shelf := itashi.NewFilesystemShelf(itashi.GetDefualtSave())
+		switch action {
+		case ADD_TASK:
+			itashi.AddTaskPrompt(shelf)
+			os.Exit(0)
+		case DEL_TASK:
+			id, err := strconv.Atoi(args[2])
+			if err != nil {
+				log.Fatal("ID passed was not a valid integer: ", err)
+			}
+			shelf.DeleteTask(id)
+			os.Exit(0)
+		case DONE_TASK:
+			id, err := strconv.Atoi(args[2])
+			if err != nil {
+				log.Fatal("ID passed was not a valid integer: ", err)
+			}
+			shelf.MarkDone(id)
+			fmt.Print("よくできた! Good job!\n")
+			os.Exit(0)
+
+		}
 	}
+	fmt.Printf("%+v\n", string(banner))
+
 	p := tea.NewProgram(itashi.InitialModel())
 
 	if _, err := p.Run(); err != nil {

+ 66 - 7
pkg/savestate.go

@@ -62,7 +62,7 @@ type TaskShelf interface {
 	// hopefully you dont need to call this! ;)
 	ResetDone(id int)
 	// Add a task to the shelf
-	AddTask(t Task)
+	AddTask(title string, desc string, priority int, due time.Time)
 	// Retrieve all tasks in the shelf
 	GetAll() []Task
 	// Render a task to a task template
@@ -145,10 +145,30 @@ func (f *FilesystemShelf) GetAll() []Task {
 /*
 Add a task to the filesystem shelf
 
-	:param t: the Task struct to add
+	    :param title: the title to give the task
+		:param desc: the description to give the task
+		:param priority: the priority to give the task
+		:param due: the due date for the task
+		:returns: Nothing
 */
-func (f *FilesystemShelf) AddTask(t Task) {
-	f.Tasks = append(f.Tasks, t)
+func (f *FilesystemShelf) AddTask(title string, desc string, priority int, due time.Time) {
+	var inc int
+	inc = 0
+	for i := range f.Tasks {
+		if f.Tasks[i].Id > inc {
+			inc = f.Tasks[i].Id
+		}
+	}
+	inc += 1
+	task := Task{
+		Id:       inc,
+		Title:    title,
+		Desc:     desc,
+		Due:      due,
+		Done:     false,
+		Priority: priority,
+	}
+	f.Tasks = append(f.Tasks, task)
 
 	err := os.WriteFile(f.SaveLocation, marshalTaskToShelf(f.Tasks, f.Template), os.ModePerm)
 	if err != nil {
@@ -161,9 +181,48 @@ func (f *FilesystemShelf) ModifyDue(id int, due time.Time)  {}
 func (f *FilesystemShelf) ModifyDesc(id int, desc string)   {}
 func (f *FilesystemShelf) ModifyPriority(id int, pri int)   {}
 func (f *FilesystemShelf) ModifyTitle(id int, title string) {}
-func (f *FilesystemShelf) DeleteTask(id int)                {}
-func (f *FilesystemShelf) MarkDone(id int)                  {}
-func (f *FilesystemShelf) ResetDone(id int)                 {}
+func (f *FilesystemShelf) DeleteTask(id int) {
+	replTasks := []Task{}
+	for i := range f.Tasks {
+		if f.Tasks[i].Id == id {
+			continue
+		}
+		replTasks = append(replTasks, f.Tasks[i])
+	}
+	os.WriteFile(f.SaveLocation, marshalTaskToShelf(replTasks, f.Template), os.ModePerm)
+
+}
+
+/*
+Mark task as done and write the shelf to disk. since the Tasks within FilesystemShelf are
+values and not pointers, we need to copy the entirety of the shelf over to a new set
+and write it, as opposed to just modifying the pointer and then writing.
+
+	    :param id: the ID of the task to mark as done
+		:returns: Nothing
+*/
+func (f *FilesystemShelf) MarkDone(id int) {
+	replTasks := []Task{}
+	for i := range f.Tasks {
+		if f.Tasks[i].Id == id {
+			doneTask := Task{
+				Id:       f.Tasks[i].Id,
+				Title:    f.Tasks[i].Title,
+				Desc:     f.Tasks[i].Desc,
+				Due:      f.Tasks[i].Due,
+				Done:     true,
+				Priority: f.Tasks[i].Priority,
+			}
+			replTasks = append(replTasks, doneTask)
+			continue
+		}
+		replTasks = append(replTasks, f.Tasks[i])
+	}
+	os.WriteFile(f.SaveLocation, marshalTaskToShelf(replTasks, f.Template), os.ModePerm)
+
+}
+
+func (f *FilesystemShelf) ResetDone(id int) {}
 
 // private function for parsing the byte stream from the filesystem
 func parseFilesystemShelf(data []byte) []Task {

+ 0 - 0
pkg/sundown.go


+ 16 - 12
pkg/userinterface.go

@@ -10,6 +10,7 @@ import (
 	"math"
 	"os"
 	"strconv"
+	"strings"
 	"text/template"
 	"time"
 
@@ -36,12 +37,12 @@ const HEADER_TEMPLATE = `
 
 const TASK_ITEM = `
 +------------------------------------
-|  
-|  Title: {{.Title}} 
-|  {{.Desc}}
+|  Task ID: {{.Id}} 
+|  Title: {{.Title}}|
+|  {{.Desc}}|
 |  Due: {{.Due}}
 |  Priority: {{.Priority}}
-|  Done?: {{.Priority}}
+|  Done?: {{.Done}}
 |
 +---------------------------
 `
@@ -310,20 +311,23 @@ func (m model) View() string {
 Add task to the shelf
 */
 func AddTaskPrompt(shelf TaskShelf) {
-	task := &Task{}
+	var title string
+	var desc string
+	var priority string
+	var due time.Time
+
 	var reader *bufio.Reader
 	reader = bufio.NewReader(os.Stdout)
 	fmt.Print("Enter Task Title: ")
-	task.Title, _ = reader.ReadString('\n')
+	title, _ = reader.ReadString('\n')
 	fmt.Print("Task description: ")
-	task.Desc, _ = reader.ReadString('\n')
+	desc, _ = reader.ReadString('\n')
 	fmt.Print("Priority: ")
-	priority, _ := reader.ReadString('\n')
-	pri, err := strconv.Atoi(priority)
+	priority, _ = reader.ReadString('\n')
+	priorityInt, err := strconv.Atoi(strings.TrimSpace(priority))
 	if err != nil {
-		fmt.Print("non-real number sry\n")
+		fmt.Print("We couldnt parse the priority value given. :(\n")
 	}
-	task.Priority = pri
 
-	shelf.AddTask(*task)
+	shelf.AddTask(title, desc, priorityInt, due)
 }