Browse Source

added a cleaning function

aeth 1 month ago
parent
commit
6fc15f2734
4 changed files with 25 additions and 1 deletions
  1. 4 0
      cmd/main.go
  2. 18 0
      pkg/savestate.go
  3. 2 0
      pkg/sundown.go
  4. 1 1
      pkg/userinterface.go

+ 4 - 0
cmd/main.go

@@ -14,6 +14,7 @@ import (
 const ADD_TASK = "add"
 const DEL_TASK = "del"
 const DONE_TASK = "done"
+const TIDY_TASK = "tidy"
 
 //go:embed banner.txt
 var banner []byte
@@ -42,6 +43,9 @@ func main() {
 			shelf.MarkDone(id)
 			fmt.Print("よくできた! Good job!\n")
 			os.Exit(0)
+		case TIDY_TASK:
+			fmt.Printf("Shelf tidied, removed %v completed tasks.\n", shelf.Clean())
+			os.Exit(0)
 
 		}
 	}

+ 18 - 0
pkg/savestate.go

@@ -67,6 +67,8 @@ type TaskShelf interface {
 	GetAll() []Task
 	// Render a task to a task template
 	RenderTask(task Task) string
+	// Clean the shelf of all completed tasks
+	Clean() int
 }
 
 /*
@@ -193,6 +195,22 @@ func (f *FilesystemShelf) DeleteTask(id int) {
 
 }
 
+// Clean the filesystem shelf of all completed tasks
+func (f *FilesystemShelf) Clean() int {
+	replTasks := []Task{}
+	var cleaned int
+	cleaned = 0
+	for i := range f.Tasks {
+		if f.Tasks[i].Done {
+			cleaned += 1
+			continue
+		}
+		replTasks = append(replTasks, f.Tasks[i])
+	}
+	os.WriteFile(f.SaveLocation, marshalTaskToShelf(replTasks, f.Template), os.ModePerm)
+	return cleaned
+}
+
 /*
 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

+ 2 - 0
pkg/sundown.go

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

+ 1 - 1
pkg/userinterface.go

@@ -42,7 +42,7 @@ const TASK_ITEM = `
 |  {{.Desc}}|
 |  Due: {{.Due}}
 |  Priority: {{.Priority}}
-|  Done?: {{.Done}}
+|  Done: {{.Done}}
 |
 +---------------------------
 `