69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
store := NewStore()
|
|
fmt.Println("taskcli — commands: add <title>, done <id>, list, quit")
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
for {
|
|
fmt.Print("> ")
|
|
if !scanner.Scan() {
|
|
return
|
|
}
|
|
line := strings.TrimSpace(scanner.Text())
|
|
if line == "" {
|
|
continue
|
|
}
|
|
command, rest, _ := strings.Cut(line, " ")
|
|
switch command {
|
|
case "add":
|
|
if rest == "" {
|
|
fmt.Println("usage: add <title>")
|
|
continue
|
|
}
|
|
task := store.Add(rest)
|
|
fmt.Printf("added #%d %s\n", task.ID, task.Title)
|
|
case "done":
|
|
id, err := ParseID(rest)
|
|
if err != nil {
|
|
fmt.Println("error:", err)
|
|
continue
|
|
}
|
|
if err := store.Complete(id); err != nil {
|
|
fmt.Println("error:", err)
|
|
continue
|
|
}
|
|
fmt.Printf("completed #%d\n", id)
|
|
case "list":
|
|
printList(store)
|
|
case "quit":
|
|
return
|
|
default:
|
|
fmt.Println("unknown command:", command)
|
|
}
|
|
}
|
|
}
|
|
|
|
func printList(store *Store) {
|
|
widest := 0
|
|
for _, task := range store.All() {
|
|
if w := task.TitleWidth(); w > widest {
|
|
widest = w
|
|
}
|
|
}
|
|
for _, task := range store.All() {
|
|
mark := " "
|
|
if task.Done {
|
|
mark = "x"
|
|
}
|
|
padding := strings.Repeat(" ", widest-task.TitleWidth())
|
|
fmt.Printf("#%d %s%s [%s]\n", task.ID, task.Title, padding, mark)
|
|
}
|
|
}
|