more features!

main
simonkellet 1 year ago
parent d08834e39e
commit 694b4c2cdb
  1. 85
      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 {
var input int
n, err := fmt.Scanln(&input)
if n < 1 || err != nil {
fmt.Print("[ERR] Invalid input\n")
} }
func (todo *Todos) ListTodosFull() { switch input {
//List all the contents case 0:
if len(todo.Items) == 0 { PrintMenu()
fmt.Printf("empty\n") 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> ")
} }
for k, v := range todo.Items {
fmt.Printf("%d:%s\n", k, v) if err := list.RemoveTodo(index); err != nil {
fmt.Print(err)
} }
PrintMenu()
case 4:
fmt.Print("Quitting, goodbye!\n")
os.Exit(0)
default:
PrintMenu()
} }
func ListTodosSmall() {
fmt.Println("ListTodosSmall func. not implemented")
//List all the contents
} }
func main() {
fmt.Println("TODO APP")
/*
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.