// Package tasks keeps a to-do list and its file format. It knows nothing about // the command line: main.go does the arguments and the printing. package tasks import ( "bufio" "errors" "fmt" "os" "slices" "strings" "time" ) // dateLayout is how a creation date is written in the file. const dateLayout = "2006-01-02" // ErrEmptyText means a task had no text. var ErrEmptyText = errors.New("a task needs some text") // ErrNoTask means a task number did not match any task. var ErrNoTask = errors.New("no such task") // Task is one thing to do. type Task struct { Text string Done bool Created time.Time } // List holds the tasks in order. Task numbers start at 1. type List struct { tasks []Task } // Tasks returns a copy of the tasks, so a caller cannot change the list by // accident. func (l *List) Tasks() []Task { copied := make([]Task, len(l.tasks)) copy(copied, l.tasks) return copied } // Add puts a new task at the end and returns its number. now is the time to // record as its creation date. func (l *List) Add(text string, now time.Time) (int, error) { text = strings.TrimSpace(text) if text == "" { return 0, ErrEmptyText } if strings.ContainsAny(text, "\t\n\r") { return 0, errors.New("a task cannot hold a tab or a line break") } l.tasks = append(l.tasks, Task{Text: text, Created: calendarDay(now)}) return len(l.tasks), nil } // Done marks task n as done and returns it. func (l *List) Done(n int) (Task, error) { err := l.check(n) if err != nil { return Task{}, err } if l.tasks[n-1].Done { return Task{}, fmt.Errorf("task %d is already done", n) } l.tasks[n-1].Done = true return l.tasks[n-1], nil } // Remove takes task n out of the list and returns it. The tasks after it // move up by one number. func (l *List) Remove(n int) (Task, error) { err := l.check(n) if err != nil { return Task{}, err } removed := l.tasks[n-1] l.tasks = slices.Delete(l.tasks, n-1, n) return removed, nil } // check returns an error that wraps ErrNoTask if n is not a task number. func (l *List) check(n int) error { if n < 1 || n > len(l.tasks) { return fmt.Errorf("task %d: %w", n, ErrNoTask) } return nil } // Age describes how long ago a task was created, as seen from now: "today", // "yesterday" or "3 days ago". func Age(created time.Time, now time.Time) string { days := int(calendarDay(now).Sub(calendarDay(created)).Hours() / 24) switch { case days <= 0: return "today" case days == 1: return "yesterday" default: return fmt.Sprintf("%d days ago", days) } } // calendarDay keeps only the year, month and day of t, at midnight UTC. A day // in UTC is always exactly 24 hours long, so days can be counted by dividing. func calendarDay(t time.Time) time.Time { return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC) } // Load reads a list from a file. A file that does not exist yet gives an // empty list. func Load(filename string) (*List, error) { file, err := os.Open(filename) if errors.Is(err, os.ErrNotExist) { return &List{}, nil } if err != nil { return nil, err } defer file.Close() list := &List{} scanner := bufio.NewScanner(file) lineNumber := 0 for scanner.Scan() { lineNumber++ line := scanner.Text() if strings.TrimSpace(line) == "" { continue } task, err := parseLine(line) if err != nil { return nil, fmt.Errorf("%s line %d: %w", filename, lineNumber, err) } list.tasks = append(list.tasks, task) } err = scanner.Err() if err != nil { return nil, err } return list, nil } // Save writes the list to a file, one task per line. func (l *List) Save(filename string) error { text := "" for _, task := range l.tasks { text += formatLine(task) + "\n" } return os.WriteFile(filename, []byte(text), 0644) } // formatLine turns a task into one line of the file: the state, the creation // date and the text, separated by tabs. // // todo 2025-08-17 buy milk func formatLine(task Task) string { state := "todo" if task.Done { state = "done" } return state + "\t" + task.Created.Format(dateLayout) + "\t" + task.Text } // parseLine is the opposite of formatLine. func parseLine(line string) (Task, error) { parts := strings.Split(line, "\t") if len(parts) != 3 { return Task{}, fmt.Errorf("want 3 parts separated by tabs, found %d", len(parts)) } var task Task switch parts[0] { case "todo": task.Done = false case "done": task.Done = true default: return Task{}, fmt.Errorf("the state %q is not todo or done", parts[0]) } created, err := time.Parse(dateLayout, parts[1]) if err != nil { return Task{}, fmt.Errorf("the date %q is not like 2025-08-17", parts[1]) } task.Created = created task.Text = strings.TrimSpace(parts[2]) if task.Text == "" { return Task{}, ErrEmptyText } return task, nil }