package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
items, err := loadItems()
if err != nil {
fmt.Println("could not load saved items:", err)
items = []Item{}
}
in := bufio.NewReader(os.Stdin)
// The menu loop: repeats until "q". Every branch validates its input
// and continues instead of crashing.
for {
fmt.Println("\n1) Add 2) List 3) Mark done 4) Search 5) Lookup 6) Summary q) Quit")
fmt.Print("> ")
switch readLine(in) {
case "1":
fmt.Print("Title: ")
title := readLine(in)
fmt.Print("Priority (1-3): ")
p, err := strconv.Atoi(readLine(in))
if err != nil || p < 1 || p > 3 {
fmt.Println("Priority must be 1, 2 or 3.")
continue
}
items = addItem(items, title, p)
case "2":
listItems(items)
case "3":
fmt.Print("Item number: ")
n, err := strconv.Atoi(readLine(in))
if err != nil || n < 1 || n > len(items) {
fmt.Println("No item with that number.")
continue
}
markDone(&items[n-1])
fmt.Println("Done:", items[n-1].Title)
case "4":
fmt.Print("Keyword: ")
listItems(searchItems(items, readLine(in)))
case "5":
fmt.Print("Exact title: ")
if it, ok := indexByTitle(items)[readLine(in)]; ok {
fmt.Printf("Found: %s (priority %d, done %v)\n", it.Title, it.Priority, it.Done)
} else {
fmt.Println("Not found.")
}
case "6":
summary(items)
case "q":
if err := saveItems(items); err != nil {
fmt.Println("save failed:", err)
}
fmt.Println("Saved. Bye!")
return
default:
fmt.Println("Unknown choice — try again.")
}
}
}
// readLine reads one trimmed line of user input.
func readLine(in *bufio.Reader) string {
line, _ := in.ReadString('\n')
return strings.TrimSpace(line)
}