60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
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)
|
|
}
|