more features!

main
simonkellet 1 year ago
parent d08834e39e
commit 694b4c2cdb
  1. 91
      main.go
  2. BIN
      todo

@ -1,49 +1,77 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"os"
) )
type Todo struct { func PrintMenu() {
Title string fmt.Printf("### TODO APP ###\n")
Content string fmt.Printf("1: List Todo\n")
} fmt.Printf("2: Add Todo\n")
fmt.Printf("3: Remove Todo (via index)\n")
type Todos struct { fmt.Printf("\n\n0: List menu again\n")
Items []Todo fmt.Printf("4: Quit\n\n> ")
}
func (todo *Todos) AddTodo(newTodo Todo) {
todo.Items = append(todo.Items, newTodo)
} }
func (todo *Todos) RemoveTodo(index int) { func main() {
//remove todo via index fmt.Print("\033[H\033[2J") //clear
todo.Items = append(todo.Items[:index], todo.Items[index+1:]...) PrintMenu()
} list := Todos{}
for {
func (todo *Todos) ListTodosFull() { var input int
//List all the contents n, err := fmt.Scanln(&input)
if len(todo.Items) == 0 { if n < 1 || err != nil {
fmt.Printf("empty\n") fmt.Print("[ERR] Invalid input\n")
} }
for k, v := range todo.Items {
fmt.Printf("%d:%s\n", k, v) switch input {
case 0:
PrintMenu()
case 1:
list.ListTodo()
PrintMenu()
case 2:
in := bufio.NewReader(os.Stdin)
fmt.Print("Add Title:\n> ")
title, _ := in.ReadString('\n')
fmt.Print("Add Content:\n> ")
content, _ := in.ReadString('\n')
list.AddTodo(Todo{Title: title, Content: content})
fmt.Printf("Added %s to the list!\n", title)
PrintMenu()
case 3:
var index int
fmt.Printf("Which TODO to remove?\n> ")
if _, err := fmt.Scanf("%d", &index); err != nil {
fmt.Printf("Invalid input\n> ")
} }
}
func ListTodosSmall() { if err := list.RemoveTodo(index); err != nil {
fmt.Println("ListTodosSmall func. not implemented") fmt.Print(err)
//List all the contents }
} PrintMenu()
func main() { case 4:
fmt.Println("TODO APP") fmt.Print("Quitting, goodbye!\n")
os.Exit(0)
default:
PrintMenu()
}
}
/*
list := Todos{} list := Todos{}
//todos := []Todo{} //todos := []Todo{}
//list := Todos{todos} //list := Todos{todos}
fmt.Println("Empty list: ") fmt.Println("Empty list: ")
list.ListTodosFull() list.ListTodo()
fmt.Println("Adding entries...") fmt.Println("Adding entries...")
items := []Todo{ items := []Todo{
@ -67,12 +95,13 @@ func main() {
} }
fmt.Println("Listing...") fmt.Println("Listing...")
list.ListTodosFull() list.ListTodo()
fmt.Println("Listing again...") fmt.Println("Listing again...")
list.ListTodosFull() list.ListTodo()
fmt.Println("Removing item2 via index 1") fmt.Println("Removing item2 via index 1")
list.RemoveTodo(1) list.RemoveTodo(1)
fmt.Println("Listing again...") fmt.Println("Listing again...")
list.ListTodosFull() list.ListTodo()
*/
} }

BIN
todo

Binary file not shown.