Compare commits

..

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

3 changed files with 62 additions and 134 deletions

153
main.go
View File

@ -1,107 +1,78 @@
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{} list := Todos{}
for { //todos := []Todo{}
var input int //list := Todos{todos}
n, err := fmt.Scanln(&input) fmt.Println("Empty list: ")
if n < 1 || err != nil { list.ListTodosFull()
fmt.Print("[ERR] Invalid input\n")
}
switch input { fmt.Println("Adding entries...")
case 0: items := []Todo{
PrintMenu() {
case 1: Title: "test",
list.ListTodo() Content: "Here is some content",
PrintMenu() },
case 2: {
in := bufio.NewReader(os.Stdin) Title: "test1",
Content: "Here is some more content",
fmt.Print("Add Title:\n> ") },
title, _ := in.ReadString('\n') {
Title: "test2",
fmt.Print("Add Content:\n> ") Content: "Here is even MORE content",
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()
}
} }
/* //Add the items for testing
list := Todos{} for item := range items {
//todos := []Todo{} list.AddTodo(items[item])
//list := Todos{todos} }
fmt.Println("Empty list: ")
list.ListTodo()
fmt.Println("Adding entries...") fmt.Println("Listing...")
items := []Todo{ list.ListTodosFull()
{ fmt.Println("Listing again...")
Title: "test", list.ListTodosFull()
Content: "Here is some content",
},
{
Title: "test1",
Content: "Here is some more content",
},
{
Title: "test2",
Content: "Here is even MORE content",
},
}
//Add the items for testing fmt.Println("Removing item2 via index 1")
for item := range items { list.RemoveTodo(1)
list.AddTodo(items[item]) fmt.Println("Listing again...")
} list.ListTodosFull()
fmt.Println("Listing...")
list.ListTodo()
fmt.Println("Listing again...")
list.ListTodo()
fmt.Println("Removing item2 via index 1")
list.RemoveTodo(1)
fmt.Println("Listing again...")
list.ListTodo()
*/
} }

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
}