First commit
This commit is contained in:
68
part3-taskcli/main.go
Normal file
68
part3-taskcli/main.go
Normal file
@ -0,0 +1,68 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user