init code

This commit is contained in:
Christoph K.
2026-02-25 07:30:06 +01:00
parent 270eb56ac8
commit 797657c56b
7 changed files with 403 additions and 0 deletions

59
prd/parser.go Normal file
View File

@@ -0,0 +1,59 @@
package prd
import (
"bufio"
"os"
"strings"
)
type Task struct {
Title string
Completed bool
Index int
}
func ParseTasks(filepath string) ([]Task, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()
var tasks []Task
index := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "- [ ] ") {
tasks = append(tasks, Task{
Title: strings.TrimPrefix(line, "- [ ] "),
Completed: false,
Index: index,
})
index++
} else if strings.HasPrefix(line, "- [x] ") {
tasks = append(tasks, Task{
Title: strings.TrimPrefix(line, "- [x] "),
Completed: true,
Index: index,
})
index++
}
}
return tasks, scanner.Err()
}
func MarkTaskComplete(filepath string, taskTitle string) error {
content, err := os.ReadFile(filepath)
if err != nil {
return err
}
updated := strings.ReplaceAll(
string(content),
"- [ ] "+taskTitle,
"- [x] "+taskTitle,
)
return os.WriteFile(filepath, []byte(updated), 0644)
}