Compare commits

..

No commits in common. "694b4c2cdb9e0e61a44899fcab572ecf2d7bf3f8" and "5e5e0ee7326144350533a70cba43ad4f9164a4d6" have entirely different histories.

3 changed files with 62 additions and 134 deletions

99
main.go
View File

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

BIN
todo

Binary file not shown.

43
todo.go
View File

@ -1,43 +0,0 @@
package main
import (
"errors"
"fmt"
)
type Todo struct {
Title string
Content string
}
type Todos struct {
Items []Todo
}
func (todo *Todos) AddTodo(newTodo Todo) {
todo.Items = append(todo.Items, newTodo)
}
func (todo *Todos) RemoveTodo(index int) (err error) {
//remove todo via index
if len(todo.Items) == 0 {
return errors.New("[ERR] Todo list is empty!\n")
}
if index > len(todo.Items) {
return errors.New("[ERR] Index out of bounds")
}
todo.Items = append(todo.Items[:index], todo.Items[index+1:]...)
return nil
}
func (todo *Todos) ListTodo() int {
if len(todo.Items) == 0 {
return 0
}
for k, v := range todo.Items {
fmt.Printf("%d:%s\n", k, v)
}
return 1
}