package main
import (
"fmt"
"strings"
)
// addItem appends a new item built from user input.
func addItem(items []Item, title string, priority int) []Item {
return append(items, Item{Title: title, Priority: priority})
}
// listItems prints every item, numbered, with a done checkbox.
func listItems(items []Item) {
if len(items) == 0 {
fmt.Println("No items yet.")
return
}
for i, it := range items {
status := " "
if it.Done {
status = "x"
}
fmt.Printf("%2d. [%s] %s (priority %d)\n", i+1, status, it.Title, it.Priority)
}
}
// markDone flips one item in place — it needs a POINTER so it changes the
// original item, not a copy.
func markDone(it *Item) {
it.Done = true
}
// searchItems returns the items whose title contains the keyword
// (case-insensitive).
func searchItems(items []Item, keyword string) []Item {
var found []Item
for _, it := range items {
if strings.Contains(strings.ToLower(it.Title), strings.ToLower(keyword)) {
found = append(found, it)
}
}
return found
}
// indexByTitle builds a map for direct lookup of an item by its exact title.
func indexByTitle(items []Item) map[string]Item {
m := make(map[string]Item, len(items))
for _, it := range items {
m[it.Title] = it
}
return m
}
// summary prints the "X of Y done" stat plus the high-priority count.
func summary(items []Item) {
done, high := 0, 0
for _, it := range items {
if it.Done {
done++
}
if it.Priority == 3 {
high++
}
}
fmt.Printf("%d of %d done, %d high-priority\n", done, len(items), high)
}